-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.rb
253 lines (230 loc) · 8.22 KB
/
bench.rb
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
require 'net/http'
require 'uri'
require 'parallel'
require 'concurrent'
require 'benchmark'
require 'ruby-progressbar'
require 'time'
DEFAULT_TIMEOUT = 2000
debug = false
debug_content = false
def show_help
puts """
This script benchmarks proxy requests and display an average.
Usage:
bundle exec ruby #{__FILE__} URL QUANTITY CONCURRENCY [TIMEOUT=2000] [PROXY_LIST_FILE]
Arguments
=========
URL Target URL to use for the benchmark.
QUANTITY How many requests to execute for the test.
CONCURRENCY How many parallel requests should be used to benchamrk the proxy.
The concurrency value cannot be higher than the quantity.
TIMEOUT Optional. Request timeout before failure. Default: 2000.
PROXY_LIST_FILE Optional. Proxy list file containing one proxy address per line.
"""
end
if ARGV.count < 3
show_help
exit 1
end
# parse and validate url list file
raw_url = ARGV.shift
puts "raw url: #{raw_url.inspect}" if debug
url = URI.parse(raw_url)
if url.nil?
STDERR.puts "Invalid url value \"#{url}\""
exit 1
end
# parse and validate quantity param
raw_quantity = ARGV.shift
puts "raw quantity: #{raw_quantity.inspect}" if debug
quantity = Integer(raw_quantity)
if quantity.nil?
STDERR.puts "Invalid quantity value \"#{raw_quantity}\""
exit 1
end
if quantity < 1
STDERR.puts "Invalid quantity value #{quantity}, it must be greater or equal 1"
exit 1
end
# parse and validate concurrency param
raw_concurrency = ARGV.shift
puts "raw concurrency: #{raw_concurrency.inspect}" if debug
concurrency = Integer(raw_concurrency)
if concurrency.nil?
STDERR.puts "Invalid concurrency value \"#{raw_concurrency}\""
exit 1
end
if concurrency < 1
STDERR.puts "Invalid concurrency value #{concurrency}, it must be greater or equal 1"
exit 1
end
# validate quantity vs concurrency
if quantity < concurrency
STDERR.puts "Can't set a higher concurrency than the quantity"
exit 1
end
# parse and validate timeout param
raw_timeout = ARGV.shift
puts "raw timeout: #{raw_timeout.inspect}" if debug
timeout = Integer(raw_timeout)
if timeout.nil?
timeout = DEFAULT_TIMEOUT
end
if timeout < 1
STDERR.puts "Invalid timeout value #{timeout}, it must be greater or equal 1"
exit 1
end
# parse and validate proxy list file path
proxy_list_path = nil
raw_path = ARGV.shift
puts "raw proxy_list_path: #{raw_path.inspect}" if debug
unless raw_path.nil?
unless File.exists?(raw_path)
STDERR.puts "Proxy file \"#{raw_path}\" doesn't exist"
exit 1
end
proxy_list_path = raw_path
end
# load proxy list file if exists
proxy_list = Concurrent::Array.new
unless proxy_list_path.nil?
# read proxy file and load the proxy list
f = File.open(proxy_list_path)
f.each do |line|
line.strip!
next if line == ''
line = "http://#{line}" unless line =~ /^http:\/\/.+/
proxy_list << URI.parse(line)
end
else
# add a fake proxy when no proxy list was provided
fake_proxy = Object.new
fake_proxy.define_singleton_method(:host, lambda{nil})
fake_proxy.define_singleton_method(:port, lambda{nil})
fake_proxy.define_singleton_method(:schema, lambda{nil})
fake_proxy.define_singleton_method(:user, lambda{nil})
fake_proxy.define_singleton_method(:password, lambda{nil})
proxy_list << fake_proxy
end
# run benchmark
semaphore = Mutex.new
data = Concurrent::Array.new
progressbar = ProgressBar.create(total: quantity, length: 80, format: 'Progress %c/%C |%B| %a %e')
progressbar.total = quantity
Parallel.each(0..(concurrency-1), in_threads: concurrency) do |thread_index|
puts "[T#{thread_index}]: Thread Index = #{thread_index}" if debug
# calc limit
target_uri = nil
sums = {}
limit = 0
request_timeout = 0
report_threshold = 0
semaphore.synchronize do
target_uri = url.clone
limit = (quantity.to_f / concurrency.to_f).floor
puts "[T#{thread_index}]: Limit before = #{limit}" if debug
adjustment = quantity - (limit * concurrency)
puts "[T#{thread_index}]: Adjustment = #{adjustment}" if debug
limit += 1 if adjustment > 0 && thread_index < adjustment
puts "[T#{thread_index}]: Limit after = #{limit}" if debug
report_threshold = quantity / (concurrency * 20)
report_threshold = 10 if report_threshold > 10
request_timeout = timeout + 0
end
# perform test
report_count = thread_index
report_count_adjustment = thread_index
report_time = Time.now.to_i
proxy_count = proxy_list.length
limit.times do |index|
proxy = proxy_list[rand(proxy_count)].clone
use_ssl = (target_uri.scheme =~ /https/)
request = Net::HTTP::Get.new(target_uri)
key = nil
time = Benchmark.measure do
begin
Net::HTTP.start(target_uri.host, target_uri.port, proxy.host, proxy.port, proxy.user, proxy.password, use_ssl: use_ssl, verify_mode: OpenSSL::SSL::VERIFY_NONE, read_timeout: request_timeout, open_timeout: request_timeout, ssl_timeout: request_timeout, keep_alive_timeout: 0) do |http|
http.request request do |response|
#puts response.inspect if debug
response.read_body do |chunk|
# show content only on debug
puts chunk if debug_content && debug
end
key = "#{response.code}"
end
end
rescue => ex
STDERR.puts ex.inspect if debug
key = "Failed"
end
end
puts "[T#{thread_index}]: Response status code = #{key} | Time: #{time}" if debug
# sum times depending on response code
sums[key] = {time: Benchmark::Tms::new, count: 0, max: Benchmark::Tms::new, min: Benchmark::Tms::new} unless sums.has_key?(key)
time_data = sums[key]
time_data[:time] += time
time_data[:count] += 1
time_data[:max] = time if time_data[:max].total < time.total
time_data[:min] = time if time_data[:min].total > time.total || time_data[:min].total <= 0
# show progress as long as it's not on debug mode
unless debug
report_count += 1
if report_count > report_threshold
semaphore.synchronize do
progressbar.progress += report_count - report_count_adjustment
end
report_count = 0
report_count_adjustment = 0
end
end
end
# show progress
unless debug
semaphore.synchronize do
progressbar.progress += report_count
end
end
data << sums
end
# join data
count = 0
total_time = Benchmark::Tms::new
total = {}
data.each do |sums|
sums.each do |key, time_data|
total[key] = {time: Benchmark::Tms::new, count: 0, max: Benchmark::Tms::new, min: Benchmark::Tms::new} unless total.has_key?(key)
total_data = total[key]
time = time_data[:time]
total_data[:time] += time.clone
total_data[:count] += time_data[:count]
# calc min/max
total_time += time
max_time = time_data[:max]
min_time = time_data[:min]
total_data[:max] = max_time if total_data[:max].total < max_time.total
total_data[:min] = min_time if total_data[:min].total > min_time.total || total_data[:min].total <= 0
count += time_data[:count]
end
end
puts ""
puts "===============" if debug
puts "Expected quantity: #{quantity}" if debug
puts "Real quantity: #{count}" if debug
puts "Total request time: #{total_time}"
# display time data per status code
total.to_a.sort{|a,b|a[0] <=> b[0]}.each do |key_pair|
key, status_data = key_pair
average = status_data[:time] / status_data[:count]
puts ""
puts "=========================="
puts "Status code #{key}"
puts "=========================="
puts "Total requests: #{status_data[:count]}"
puts "--------------------------"
puts " #{Benchmark::Tms::CAPTION}"
puts "Total time: #{status_data[:time]}"
puts "Max time: #{status_data[:max]}"
puts "Average time: #{average}"
puts "Min time: #{status_data[:min]}"
end