Skip to content

Instantly share code, notes, and snippets.

@guiferrpereira
Created May 9, 2013 12:09
Show Gist options
  • Save guiferrpereira/5547088 to your computer and use it in GitHub Desktop.
Save guiferrpereira/5547088 to your computer and use it in GitHub Desktop.
em-synchrony vs typhoeus
require 'em-synchrony'
require 'em-synchrony/em-http'
require 'em-synchrony/iterator'
require 'typhoeus'
require 'benchmark'
(1..5000).each do urls << "http://example.com" end
puts "EM::Synchrony:\n\n"
batch_size = 300
puts "Batch: #{batch_size}"
results = []
time = Benchmark.realtime do
EM.synchrony do
batch = urls.slice(0...batch_size)
puts batch
results = EM::Synchrony::Iterator.new(batch, 10).map do |url, iter|
puts "#{url}"
http = EventMachine::HttpRequest.new(url).ahead
http.callback { iter.return(http) }
http.errback {
puts http.response_header.status
iter.return(http)
}
end
EventMachine.stop
end
end
printf("Time elapsed %0.3f seconds\n", "#{time}")
puts "Typhoeus:\n\n"
hydra = Typhoeus::Hydra.new(:max_concurrency => 10)
# hydra.disable_memoization
# hydra.clear_cache_callbacks
time = Benchmark.realtime do
batch = urls.slice(0...batch_size)
puts batch
batch.each do |url|
request = Typhoeus::Request.new(url, :method => :head)
request.on_complete do |response|
puts "#{url}"
end
hydra.queue(request)
end
hydra.run
end
printf("Time elapsed %0.3f seconds\n", "#{time}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment