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

root/trunk/bench/bm_upload.rb

Revision 248, 3.4 kB (checked in by nahi, 2 years ago)
  • cosmetic.
Line 
1 require 'bm_common'
2 try_require 'multipart'
3
4 url = ARGV.shift or raise
5 proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
6 url = URI.parse(url)
7 proxy = URI.parse(proxy) if proxy
8 threads = 1
9 number = 10
10 msize = 10
11
12 testfile = File.expand_path("testfile", File.dirname(__FILE__))
13 require 'openssl'
14 File.open(testfile, 'wb') do |file|
15   (msize * 1024).times do
16     file.write(OpenSSL::Random.random_bytes(1024))
17   end
18 end
19 upload_size = msize * 1024 * 1024
20
21 def do_threads(number)
22   threads = []
23   results = []
24   number.times do
25     threads << Thread.new {
26       results << yield
27     }
28   end
29   threads.map { |th| th.join }
30   results
31 end
32
33 Benchmark.bmbm do |bm|
34 =begin
35   # eventmachine client blocks when number > 25 or so...
36   bm.report('EM::Protocols::HttpClient2') do
37     EM.run do
38       query = {}
39       done = false
40       do_threads(threads) {
41         if proxy
42           host, port = proxy.host, proxy.port
43         else
44           host, port = url.host, url.port
45         end
46         path = proxy ? url.to_s : url.path
47         requests = 0
48         (1..number).collect {
49           client = EM::Protocols::HttpClient2.connect(host, port)
50           req = client.get(proxy ? url.to_s : url.path)
51           query[req] = req
52           req.callback {
53             req.content.size
54             query.delete(req)
55             EM.stop if done && query.empty?
56           }
57         }
58       }
59       done = true
60     end
61   end
62
63   if defined?(Curl)
64     bm.report('curb') do
65       fields = [Curl::PostField.file('upload', testfile)]
66       do_threads(threads) {
67         (1..number).collect {
68           p Curl::Easy.http_post(url.to_s, *fields).body_str.to_i
69         }
70       }
71     end
72   end
73
74   if defined?(RFuzz)
75     bm.report('RFuzz::HttpClient') do
76       do_threads(threads) {
77         if proxy
78           host, port = proxy.host, proxy.port
79         else
80           host, port = url.host, url.port
81         end
82         path = proxy ? url.to_s : url.path
83         c = RFuzz::HttpClient.new(host, port)
84         result = (1..number).collect {
85           c.get(path).http_body.size
86         }
87         c.reset
88         result
89       }
90     end
91   end
92 =end
93
94   if defined?(Net::HTTP) and defined?(Net::HTTP::FileForPost)
95     bm.report('Net::HTTP + multipart') do
96       do_threads(threads) {
97         if proxy
98           c = Net::HTTP::Proxy(proxy.host, proxy.port).new(url.host, url.port)
99         else
100           c = Net::HTTP.new(url.host, url.port)
101         end
102         c.start
103         result = (1..number).collect {
104           req = Net::HTTP::Post.new(url.path)
105           file = Net::HTTP::FileForPost.new(testfile)
106           req.set_multipart_data('upload' => file)
107           raise if upload_size != c.request(req).read_body.to_i
108         }
109         c.finish
110         result
111       }
112     end
113   end
114
115   if defined?(HTTPClient)
116     bm.report('HTTPClient') do
117       c = HTTPClient.new(proxy)
118       do_threads(threads) {
119         (1..number).collect {
120           File.open(testfile) do |file|
121             raise if upload_size != c.post(url, {'upload' => file}).content.to_i
122           end
123         }
124       }
125       c.reset_all
126     end
127   end
128
129 =begin
130   if defined?(HTTParty)
131     class HTTPartyClient # need to create subclass for http_proxy
132       include HTTParty
133     end
134     bm.report('HTTParty') do
135       HTTPartyClient.http_proxy(proxy.host, proxy.port) if proxy
136       do_threads(threads) {
137         (1..number).collect {
138           # HTTParty should accept URI object like others.
139           HTTPartyClient.get(url.to_s).size
140         }
141       }
142     end
143   end
144 =end
145 end
Note: See TracBrowser for help on using the browser.