Created
May 9, 2013 12:09
-
-
Save guiferrpereira/5547088 to your computer and use it in GitHub Desktop.
em-synchrony vs typhoeus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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