1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
require 'typhoeus'
require 'net/http'
require 'open-uri'
require 'benchmark'
URL = "http://localhost:300"
hydra = Typhoeus::Hydra.new(max_concurrency: 3)
if defined? require_relative
require_relative '../spec/support/localhost_server.rb'
require_relative '../spec/support/server.rb'
else
require '../spec/support/localhost_server.rb'
require '../spec/support/server.rb'
end
LocalhostServer.new(TESTSERVER.new, 3000)
LocalhostServer.new(TESTSERVER.new, 3001)
LocalhostServer.new(TESTSERVER.new, 3002)
def url_for(i)
"#{URL}#{i%3}/"
end
Benchmark.bm do |bm|
[1000].each do |calls|
puts "[ #{calls} requests ]"
bm.report("net/http ") do
calls.times do |i|
uri = URI.parse(url_for(i))
Net::HTTP.get_response(uri)
end
end
bm.report("open ") do
calls.times do |i|
open(url_for(i))
end
end
bm.report("request ") do
calls.times do |i|
Typhoeus::Request.get(url_for(i))
end
end
bm.report("hydra ") do
calls.times do |i|
hydra.queue(Typhoeus::Request.new(url_for(i)))
end
hydra.run
end
bm.report("hydra memoize ") do
Typhoeus::Config.memoize = true
calls.times do |i|
hydra.queue(Typhoeus::Request.new(url_for(i)))
end
hydra.run
Typhoeus::Config.memoize = false
end
end
end
|