Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ gem 'sqlite3', '1.3.10'
gem 'mysql2', '0.3.18'
gem 'pg', '0.18.1'
gem 'benchmark-ips', '~> 2.2.0'
gem 'em-hiredis', '~> 0.3.0'
gem 'redis', '~> 3.0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might need to add faye-websocket in here too

gem 'faye-websocket'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually lock the versions so that we know any performance changes are not due to the gems

gem 'puma'
21 changes: 21 additions & 0 deletions rails/benchmarks/actioncable_postgres.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'bundler/setup'
require 'rails'

require_relative 'support/benchmark_rails.rb'
require_relative 'support/actioncable_helpers.rb'

PG_FAYE = Faye::WebSocket::Client.new("ws://127.0.0.1:4001/")

pg_tcp_addr = ENV['POSTGRES_PORT_5432_TCP_ADDR'] || 'localhost'
pg_port = ENV['POSTGRES_PORT_5432_TCP_PORT'] || 5432
PG_DB_URL = "postgres://postgres@#{pg_tcp_addr}:#{pg_port}/rubybench",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing , 😝


Benchmark.rails("actioncable_postgres", time: 10) do
pg_config = { adapter: 'postgresql', url: PG_DB_URL }.with_indifferent_access
with_puma_server(ActionCable.server, 4001, pg_config) do |port|
500.times do
PG_FAYE.send(SUB)
PG_FAYE.send(MSG)
end
end
end
19 changes: 19 additions & 0 deletions rails/benchmarks/actioncable_redis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'bundler/setup'
require 'rails'

require_relative 'support/benchmark_rails.rb'
require_relative 'support/actioncable_helpers.rb'

REDIS_FAYE = Faye::WebSocket::Client.new("ws://127.0.0.1:4002/")

REDIS_DB_URL = ENV["REDIS_PORT_6379_TCP_ADDR"]

Benchmark.rails("actioncable_redis", time: 10) do
redis_config = { adapter: 'redis', url: REDIS_DB_URL }.with_indifferent_access
with_puma_server(ActionCable.server, 4002, redis_config) do |port|
500.times do
REDIS_FAYE.send(SUB)
REDIS_FAYE.send(MSG)
end
end
end
53 changes: 53 additions & 0 deletions rails/benchmarks/support/actioncable_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'action_cable'
require 'rack/mock'
require 'faye/websocket'
require 'active_support/core_ext/hash/indifferent_access'
require 'pathname'
require 'puma'

module Rails
def self.root
Pathname.pwd
end
end

::Object.const_set(:ApplicationCable, Module.new)

class ApplicationCable::Channel < ActionCable::Channel::Base
end

class ApplicationCable::Connection < ActionCable::Connection::Base
end

ActionCable.instance_variable_set(:@server, nil)
server = ActionCable.server
server.config = ActionCable::Server::Configuration.new
inner_logger = ActiveSupport::Logger.new(STDOUT)
server.config.logger = ActionCable::Connection::TaggedLoggerProxy.new(inner_logger, tags: [])

# and now the "real" setup for our test:
server.config.disable_request_forgery_protection = true

if Dir.pwd.include?('support')
server.config.channel_load_paths = [File.expand_path(Dir.pwd, __dir__)]
else
server.config.channel_load_paths = [File.expand_path('support', __dir__)]
end

def with_puma_server(rack_app = ActionCable.server, port, config)
ActionCable.server.config.cable = config
server = ::Puma::Server.new(rack_app, ::Puma::Events.strings)
server.add_tcp_listener '127.0.0.1', port
server.min_threads = 1
server.max_threads = 4

t = Thread.new { server.run.join }
yield port

ensure
server.stop(true)
t.join
end

MSG = JSON.dump command: 'message', identifier: JSON.dump(channel: 'EchoChannel'), data: JSON.dump(action: 'ding', message: 'hello')
SUB = JSON.dump command: 'subscribe', identifier: JSON.dump(channel: 'EchoChannel')
13 changes: 13 additions & 0 deletions rails/benchmarks/support/echo_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class EchoChannel < ApplicationCable::Channel
def subscribed
stream_from "echo_channel"
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end

def ding(data)
transmit(dong: data['message'])
end
end