Skip to content

Commit e3254d5

Browse files
committed
Merge pull request #54 from maclover7/add-ac-benchmarks
Add ActionCable benchmarks for PostgreSQL and Redis
2 parents 0e47c27 + f74e3f4 commit e3254d5

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

rails/Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ gem 'sqlite3', '1.3.10'
2525
gem 'mysql2', '0.3.18'
2626
gem 'pg', '0.18.1'
2727
gem 'benchmark-ips', '~> 2.2.0'
28+
gem 'em-hiredis', '~> 0.3.0'
29+
gem 'redis', '~> 3.0'
30+
gem 'faye-websocket'
31+
gem 'puma'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'bundler/setup'
2+
require 'rails'
3+
4+
require_relative 'support/benchmark_rails.rb'
5+
require_relative 'support/actioncable_helpers.rb'
6+
7+
PG_FAYE = Faye::WebSocket::Client.new("ws://127.0.0.1:4001/")
8+
9+
pg_tcp_addr = ENV['POSTGRES_PORT_5432_TCP_ADDR'] || 'localhost'
10+
pg_port = ENV['POSTGRES_PORT_5432_TCP_PORT'] || 5432
11+
PG_DB_URL = "postgres://postgres@#{pg_tcp_addr}:#{pg_port}/rubybench",
12+
13+
Benchmark.rails("actioncable_postgres", time: 10) do
14+
pg_config = { adapter: 'postgresql', url: PG_DB_URL }.with_indifferent_access
15+
with_puma_server(ActionCable.server, 4001, pg_config) do |port|
16+
500.times do
17+
PG_FAYE.send(SUB)
18+
PG_FAYE.send(MSG)
19+
end
20+
end
21+
end

rails/benchmarks/actioncable_redis.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'bundler/setup'
2+
require 'rails'
3+
4+
require_relative 'support/benchmark_rails.rb'
5+
require_relative 'support/actioncable_helpers.rb'
6+
7+
REDIS_FAYE = Faye::WebSocket::Client.new("ws://127.0.0.1:4002/")
8+
9+
REDIS_DB_URL = ENV["REDIS_PORT_6379_TCP_ADDR"]
10+
11+
Benchmark.rails("actioncable_redis", time: 10) do
12+
redis_config = { adapter: 'redis', url: REDIS_DB_URL }.with_indifferent_access
13+
with_puma_server(ActionCable.server, 4002, redis_config) do |port|
14+
500.times do
15+
REDIS_FAYE.send(SUB)
16+
REDIS_FAYE.send(MSG)
17+
end
18+
end
19+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'action_cable'
2+
require 'rack/mock'
3+
require 'faye/websocket'
4+
require 'active_support/core_ext/hash/indifferent_access'
5+
require 'pathname'
6+
require 'puma'
7+
8+
module Rails
9+
def self.root
10+
Pathname.pwd
11+
end
12+
end
13+
14+
::Object.const_set(:ApplicationCable, Module.new)
15+
16+
class ApplicationCable::Channel < ActionCable::Channel::Base
17+
end
18+
19+
class ApplicationCable::Connection < ActionCable::Connection::Base
20+
end
21+
22+
ActionCable.instance_variable_set(:@server, nil)
23+
server = ActionCable.server
24+
server.config = ActionCable::Server::Configuration.new
25+
inner_logger = ActiveSupport::Logger.new(STDOUT)
26+
server.config.logger = ActionCable::Connection::TaggedLoggerProxy.new(inner_logger, tags: [])
27+
28+
# and now the "real" setup for our test:
29+
server.config.disable_request_forgery_protection = true
30+
31+
if Dir.pwd.include?('support')
32+
server.config.channel_load_paths = [File.expand_path(Dir.pwd, __dir__)]
33+
else
34+
server.config.channel_load_paths = [File.expand_path('support', __dir__)]
35+
end
36+
37+
def with_puma_server(rack_app = ActionCable.server, port, config)
38+
ActionCable.server.config.cable = config
39+
server = ::Puma::Server.new(rack_app, ::Puma::Events.strings)
40+
server.add_tcp_listener '127.0.0.1', port
41+
server.min_threads = 1
42+
server.max_threads = 4
43+
44+
t = Thread.new { server.run.join }
45+
yield port
46+
47+
ensure
48+
server.stop(true)
49+
t.join
50+
end
51+
52+
MSG = JSON.dump command: 'message', identifier: JSON.dump(channel: 'EchoChannel'), data: JSON.dump(action: 'ding', message: 'hello')
53+
SUB = JSON.dump command: 'subscribe', identifier: JSON.dump(channel: 'EchoChannel')
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class EchoChannel < ApplicationCable::Channel
2+
def subscribed
3+
stream_from "echo_channel"
4+
end
5+
6+
def unsubscribed
7+
# Any cleanup needed when channel is unsubscribed
8+
end
9+
10+
def ding(data)
11+
transmit(dong: data['message'])
12+
end
13+
end

0 commit comments

Comments
 (0)