Skip to content

Commit

Permalink
Fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
imanel committed Feb 16, 2012
2 parents 91d398a + bfa464e commit 304d12f
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 0.5.0 / 2012-02-16

Stable release
- Performance fixes - sending messages should be now 3-5 times faster(thanks to @piotrmarat)

## 0.5.0.beta1 / 2011-08-01

Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
require File.expand_path(File.join(File.dirname(__FILE__), 'tasks/performance'))

RSpec::Core::RakeTask.new do |t|
t.rspec_opts = ["-c", "-f progress"]
Expand Down
21 changes: 11 additions & 10 deletions lib/socky/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ module Socky
module Server
ROOT = File.expand_path(File.dirname(__FILE__))

autoload :Application, "#{ROOT}/server/application"
autoload :Channel, "#{ROOT}/server/channel"
autoload :Config, "#{ROOT}/server/config"
autoload :Connection, "#{ROOT}/server/connection"
autoload :HTTP, "#{ROOT}/server/http"
autoload :Logger, "#{ROOT}/server/logger"
autoload :Message, "#{ROOT}/server/message"
autoload :Misc, "#{ROOT}/server/misc"
autoload :WebSocket, "#{ROOT}/server/websocket"
autoload :Application, "#{ROOT}/server/application"
autoload :Channel, "#{ROOT}/server/channel"
autoload :Config, "#{ROOT}/server/config"
autoload :Connection, "#{ROOT}/server/connection"
autoload :HTTP, "#{ROOT}/server/http"
autoload :Logger, "#{ROOT}/server/logger"
autoload :Message, "#{ROOT}/server/message"
autoload :Misc, "#{ROOT}/server/misc"
autoload :WebSocket, "#{ROOT}/server/websocket"
autoload :CachedJsonHash, "#{ROOT}/server/cached_json_hash"
end
end
end
16 changes: 16 additions & 0 deletions lib/socky/server/cached_json_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Socky
module Server
class CachedJsonHash < Hash

def to_json
@json ||= super
end

def remove_cache
@json = nil
end

end
end
end

5 changes: 3 additions & 2 deletions lib/socky/server/channel/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def subscribers
end

def send_data(data, except = nil)
cached_json_data = CachedJsonHash[data]
self.subscribers.each do |subscriber_id, subscriber|
subscriber['connection'].send_data(data) unless subscriber_id == except || !subscriber['read']
subscriber['connection'].send_data(cached_json_data) unless subscriber_id == except || !subscriber['read']
end
end

Expand All @@ -51,7 +52,7 @@ def remove_subscriber(connection)

def deliver(connection, message)
return unless connection.nil? || (subscribers[connection.id] && subscribers[connection.id]['write'])
send_data({ 'event' => message.event, 'channel' => self.name, 'data' => message.user_data })
send_data('event' => message.event, 'channel' => self.name, 'data' => message.user_data)
end

protected
Expand Down
26 changes: 26 additions & 0 deletions spec/performance/connectin_performance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'
require 'performance_helper'
require 'benchmark'

describe 'Connecting Performance' do

it "For x listeners" do
[1, 10, 100].each do |count|
results = []
5.times do
@application = Socky::Server::Application.new('test_application', 'test_secret')
@channel = "presence"
results << Benchmark.realtime {count.times { add_listener }}
clean_application
end
print_result count, results
end
end

end






25 changes: 25 additions & 0 deletions spec/performance/send_many_messages_performance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'
require 'performance_helper'
require 'benchmark'

describe 'Sending Many Messages Performance' do

it "For 1 listener and x messages" do
[1, 10, 100].each do |count|
results = []
5.times do
@channel = "private"
@application = application_with_listeners(1)
results << Benchmark.realtime {send_messages(count)}
clean_application
end
print_result count, results
end
end

end





25 changes: 25 additions & 0 deletions spec/performance/send_message_to_many_listeners_performance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'
require 'performance_helper'
require 'benchmark'

describe 'Sending Message To Many Listeners Performance' do

it "For x listeners and 1 message" do
[1, 10, 100].each do |count|
results = []
5.times do
@channel = "private"
@application = application_with_listeners(count)
results << Benchmark.realtime {send_messages(1)}
clean_application
end
print_result count, results
end
end
end






55 changes: 55 additions & 0 deletions spec/performance_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Socky::Server::Channel::Private.class_eval do
protected

def check_auth(connection, message)
true
end

end


def application_with_listeners count
@application = Socky::Server::Application.new('test_application', 'test_secret')
count.times { add_listener }
add_writer
@application
end

def add_listener
websocket = mock_websocket(@application.name)
env = { 'PATH_INFO' => '/websocket/' + @application.name }
websocket.on_open(env)
data = {'event' => 'socky:subscribe', 'channel' => "#{@channel}-channel", 'auth' => 'test_auth'}
websocket.on_message(env, data)
websocket
end

def add_writer
@writer_ws = mock_websocket(@application.name)
env = { 'PATH_INFO' => '/websocket/' + @application.name }
@writer_ws.on_open(env)
data = {'event' => 'socky:subscribe', 'channel' => "#{@channel}-channel", 'write' => 'true', 'auth' => 'test_auth'}
@writer_ws.on_message(env, data)
@writer_ws
end

def send_messages count
env = { 'PATH_INFO' => '/websocket/' + @application.name }
data = {'event' => 'send', 'channel' => "#{@channel}-channel", 'user_data' => "test_message"}
count.times { @writer_ws.on_message(env, data) }
end

def clean_application
@application.connections.values.each {|c| c.destroy}
Socky::Server::Channel::Private.list['test_application'] = Hash.new
Socky::Server::Channel::Private.list['test_application'] = Hash.new
Socky::Server::Application.list.delete('test_application')
end


def print_result label, results
avg = results.inject(0.0) { |sum, el| sum + el } / results.size
puts ["#{label}:".ljust(15), avg].join
end


9 changes: 6 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ def mock_websocket(application)
env = {}
env['PATH_INFO'] = '/websocket/' + application
connection = Socky::Server::WebSocket.new.call(env)
connection.stub!(:send_data)
send_data_method = connection.method(:send_data)
connection.stub!(:send_data).and_return do |*args|
a = send_data_method.call(*args)
end
connection
end

def mock_connection(application)
websocket = mock_websocket(application)

env = { 'PATH_INFO' => '/websocket/' + application }
websocket.on_open(env)
end
Expand All @@ -24,4 +27,4 @@ def auth_token(socket, channel_name, remains = {})
'connection_id' => socket.connection.id,
'channel' => channel_name
}.merge(remains), :allow_changing_rights => true, :secret => 'test_secret')['auth']
end
end
11 changes: 11 additions & 0 deletions tasks/performance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rspec/core'
require 'rspec/core/rake_task'

namespace :spec do
desc "Run the performance tests"
RSpec::Core::RakeTask.new("performance") do |t|
t.pattern = "./spec/**/*_performance.rb"
t.rspec_opts = ["-c", "-f documentation"]
end
end

0 comments on commit 304d12f

Please sign in to comment.