| 1 |
require 'bm_common' |
|---|
| 2 |
|
|---|
| 3 |
url = ARGV.shift or raise |
|---|
| 4 |
proxy = ENV['http_proxy'] || ENV['HTTP_PROXY'] |
|---|
| 5 |
url = URI.parse(url) |
|---|
| 6 |
proxy = URI.parse(proxy) if proxy |
|---|
| 7 |
threads = 2 |
|---|
| 8 |
number = 500 |
|---|
| 9 |
|
|---|
| 10 |
def do_threads(number) |
|---|
| 11 |
threads = [] |
|---|
| 12 |
results = [] |
|---|
| 13 |
number.times do |
|---|
| 14 |
threads << Thread.new { |
|---|
| 15 |
results << yield |
|---|
| 16 |
} |
|---|
| 17 |
end |
|---|
| 18 |
threads.map { |th| th.join } |
|---|
| 19 |
results |
|---|
| 20 |
end |
|---|
| 21 |
|
|---|
| 22 |
Benchmark.bmbm do |bm| |
|---|
| 23 |
=begin |
|---|
| 24 |
# eventmachine client blocks when number > 25 or so... |
|---|
| 25 |
bm.report('EM::Protocols::HttpClient2') do |
|---|
| 26 |
EM.run do |
|---|
| 27 |
query = {} |
|---|
| 28 |
done = false |
|---|
| 29 |
do_threads(threads) { |
|---|
| 30 |
if proxy |
|---|
| 31 |
host, port = proxy.host, proxy.port |
|---|
| 32 |
else |
|---|
| 33 |
host, port = url.host, url.port |
|---|
| 34 |
end |
|---|
| 35 |
path = proxy ? url.to_s : url.path |
|---|
| 36 |
requests = 0 |
|---|
| 37 |
(1..number).collect { |
|---|
| 38 |
client = EM::Protocols::HttpClient2.connect(host, port) |
|---|
| 39 |
req = client.get(proxy ? url.to_s : url.path) |
|---|
| 40 |
query[req] = req |
|---|
| 41 |
req.callback { |
|---|
| 42 |
req.content.size |
|---|
| 43 |
query.delete(req) |
|---|
| 44 |
EM.stop if done && query.empty? |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
done = true |
|---|
| 49 |
end |
|---|
| 50 |
end |
|---|
| 51 |
=end |
|---|
| 52 |
|
|---|
| 53 |
if defined?(Curl) |
|---|
| 54 |
bm.report('curb') do |
|---|
| 55 |
do_threads(threads) { |
|---|
| 56 |
(1..number).collect { |
|---|
| 57 |
Curl::Easy.http_get(url.to_s).body_str.size |
|---|
| 58 |
} |
|---|
| 59 |
} |
|---|
| 60 |
end |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
if defined?(RFuzz) |
|---|
| 64 |
bm.report('RFuzz::HttpClient') do |
|---|
| 65 |
do_threads(threads) { |
|---|
| 66 |
if proxy |
|---|
| 67 |
host, port = proxy.host, proxy.port |
|---|
| 68 |
else |
|---|
| 69 |
host, port = url.host, url.port |
|---|
| 70 |
end |
|---|
| 71 |
path = proxy ? url.to_s : url.path |
|---|
| 72 |
c = RFuzz::HttpClient.new(host, port) |
|---|
| 73 |
result = (1..number).collect { |
|---|
| 74 |
c.get(path).http_body.size |
|---|
| 75 |
} |
|---|
| 76 |
c.reset |
|---|
| 77 |
result |
|---|
| 78 |
} |
|---|
| 79 |
end |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
if defined?(Net::HTTP) |
|---|
| 83 |
bm.report('Net::HTTP') do |
|---|
| 84 |
do_threads(threads) { |
|---|
| 85 |
if proxy |
|---|
| 86 |
c = Net::HTTP::Proxy(proxy.host, proxy.port).new(url.host, url.port) |
|---|
| 87 |
else |
|---|
| 88 |
c = Net::HTTP.new(url.host, url.port) |
|---|
| 89 |
end |
|---|
| 90 |
c.start |
|---|
| 91 |
result = (1..number).collect { |
|---|
| 92 |
c.get(url.path).read_body.size |
|---|
| 93 |
} |
|---|
| 94 |
c.finish |
|---|
| 95 |
result |
|---|
| 96 |
} |
|---|
| 97 |
end |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
if defined?(HTTPClient) |
|---|
| 101 |
bm.report('HTTPClient') do |
|---|
| 102 |
c = HTTPClient.new(proxy) |
|---|
| 103 |
do_threads(threads) { |
|---|
| 104 |
(1..number).collect { |
|---|
| 105 |
c.get_content(url).size |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
c.reset_all |
|---|
| 109 |
end |
|---|
| 110 |
end |
|---|
| 111 |
|
|---|
| 112 |
if defined?(OpenURI) |
|---|
| 113 |
bm.report('open-uri') do |
|---|
| 114 |
do_threads(threads) { |
|---|
| 115 |
(1..number).collect { |
|---|
| 116 |
open(url, :proxy => proxy) { |f| |
|---|
| 117 |
f.read.size |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
end |
|---|
| 122 |
end |
|---|
| 123 |
|
|---|
| 124 |
if defined?(HTTParty) |
|---|
| 125 |
class HTTPartyClient # need to create subclass for http_proxy |
|---|
| 126 |
include HTTParty |
|---|
| 127 |
end |
|---|
| 128 |
bm.report('HTTParty') do |
|---|
| 129 |
HTTPartyClient.http_proxy(proxy.host, proxy.port) if proxy |
|---|
| 130 |
do_threads(threads) { |
|---|
| 131 |
(1..number).collect { |
|---|
| 132 |
# HTTParty should accept URI object like others. |
|---|
| 133 |
HTTPartyClient.get(url.to_s).size |
|---|
| 134 |
} |
|---|
| 135 |
} |
|---|
| 136 |
end |
|---|
| 137 |
end |
|---|
| 138 |
end |
|---|