Created
January 8, 2011 12:28
-
-
Save adriaant/770795 to your computer and use it in GitHub Desktop.
EM::Synchrony::Iterator vs Typhoeus::Hydra benchmark
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 'benchmark' | |
require 'typhoeus' | |
# Sample urls can be found in https://gist.github.com/770794 | |
urls = [] | |
File.open('urls.txt', 'r+') do |f| | |
urls = f.readlines | |
end | |
puts "EM::Synchrony:\n\n" | |
batch_size = 100 | |
puts "Batch: #{batch_size}" | |
results = [] | |
time = Benchmark.realtime do | |
EM.synchrony do | |
batch = urls.slice(0...batch_size) | |
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) | |
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