Welcome to the "trac"-ing site of http-access2!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

root/trunk/bench/bm_download.rb

Revision 244, 3.1 kB (checked in by nahi, 2 years ago)
Line 
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 = 1
8 number = 10
9 download_size = 10 * 1024 * 1024
10
11 def do_threads(number)
12   threads = []
13   results = []
14   number.times do
15     threads << Thread.new {
16       results << yield
17     }
18   end
19   threads.map { |th| th.join }
20   results
21 end
22
23 Benchmark.bmbm do |bm|
24   if defined?(Curl)
25     bm.report('curb') do
26       do_threads(threads) {
27         (1..number).collect {
28           Curl::Easy.download(url.to_s, 'download_curb')
29           raise if download_size != File.lstat('download_curb').size
30         }
31       }
32     end
33   end
34
35   if defined?(RFuzz)
36     bm.report('RFuzz::HttpClient') do
37       do_threads(threads) {
38         if proxy
39           host, port = proxy.host, proxy.port
40         else
41           host, port = url.host, url.port
42         end
43         path = proxy ? url.to_s : url.path
44         c = RFuzz::HttpClient.new(host, port)
45         result = (1..number).collect {
46           File.open('download_rfuzz', 'wb') do |file|
47             file.write(c.get(path).http_body)
48           end
49           raise if download_size != File.lstat('download_rfuzz').size
50         }
51         c.reset
52         result
53       }
54     end
55   end
56
57   if defined?(Net::HTTP)
58     bm.report('Net::HTTP') do
59       do_threads(threads) {
60         if proxy
61           c = Net::HTTP::Proxy(proxy.host, proxy.port).new(url.host, url.port)
62         else
63           c = Net::HTTP.new(url.host, url.port)
64         end
65         c.start
66         result = (1..number).collect {
67           File.open('download_net_http', 'wb') do |file|
68             c.get(url.path) do |data|
69               file.write(data)
70             end
71           end
72           raise if download_size != File.lstat('download_net_http').size
73         }
74         c.finish
75         result
76       }
77     end
78   end
79
80   if defined?(HTTPClient)
81     bm.report('HTTPClient') do
82       c = HTTPClient.new(proxy)
83       do_threads(threads) {
84         (1..number).collect {
85           File.open('download_httpclient', 'wb') do |file|
86             c.get_content(url) do |data|
87               file.write(data)
88             end
89           end
90           raise if download_size != File.lstat('download_httpclient').size
91         }
92       }
93       c.reset_all
94     end
95   end
96
97   if defined?(OpenURI)
98     bm.report('open-uri') do
99       size = 16 * 1024
100       do_threads(threads) {
101         (1..number).collect {
102           File.open('download_open-uri', 'wb') do |file|
103             open(url, :proxy => proxy) { |f|
104               FileUtils.copy_stream(f, file)
105             }
106           end
107           raise if download_size != File.lstat('download_open-uri').size
108         }
109       }
110     end
111   end
112
113   if defined?(HTTParty)
114     class HTTPartyClient # need to create subclass for http_proxy
115       include HTTParty
116     end
117     bm.report('HTTParty') do
118       HTTPartyClient.http_proxy(proxy.host, proxy.port) if proxy
119       do_threads(threads) {
120         (1..number).collect {
121           File.open('download_httparty', 'wb') do |file|
122             file.write(HTTPartyClient.get(url.to_s))
123           end
124           raise if download_size != File.lstat('download_httparty').size
125         }
126       }
127     end
128   end
129 end
Note: See TracBrowser for help on using the browser.