Skip to content

Instantly share code, notes, and snippets.

@timurvafin
Created October 13, 2012 07:49
Show Gist options
  • Save timurvafin/3883733 to your computer and use it in GitHub Desktop.
Save timurvafin/3883733 to your computer and use it in GitHub Desktop.
Get http requests benchmark: sync vs. celluloid vs. em
source :rubygems
gem 'celluloid'
gem 'em-synchrony'
gem 'em-http-request'
user system total real
em: 2.440000 0.510000 2.950000 ( 4.980928)
celluloid: 3.610000 0.910000 4.520000 ( 6.500888)
require 'rubygems'
require 'bundler/setup'
require 'benchmark'
N = 2_000
POOL_SIZE = 50
URLS = ["http://ya.ru"] * N
require 'net/http'
class NetHttpTest
def get(url)
Net::HTTP.get_response(URI(url)).message
end
def self.test
URLS.map {|url| new.get(url) }
end
end
require 'celluloid'
class CelluloidTest
include Celluloid
Celluloid.logger = nil
def get(url)
Net::HTTP.get_response(URI(url)).message
end
def self.test
pool = self.pool(size: POOL_SIZE)
futures = URLS.map {|url| pool.future.get(url) }
futures.map(&:value)
end
end
require 'em-synchrony'
require 'em-synchrony/em-http'
require 'em-synchrony/fiber_iterator'
class EmHttpRequest
def get(url)
EventMachine::HttpRequest.new(url).get.response_header.status
end
def self.test
results = []
EM.synchrony do
EM::Synchrony::FiberIterator.new(URLS, POOL_SIZE).each do |url|
results.push new.get(url)
end
EventMachine.stop
end
results
end
end
Benchmark.bm do |x|
# x.report('sync: ') { NetHttpTest.test }
x.report('em: ') { EmHttpRequest.test }
x.report('celluloid: ') { CelluloidTest.test }
end
@penso
Copy link

penso commented Feb 9, 2013

Is Celluloid that much slower than event-machine for http requests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment