/
jb
executable file
·42 lines (37 loc) · 1.03 KB
/
jb
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
#!/usr/bin/env ruby
begin
require 'benchmark'
require 'rubygems'
require 'activesupport'
require 'socket'
require 'optparse'
require 'ostruct'
end
begin
$options = {
:concurrency => 1,
:count => 100,
:host => "0.0.0.0"
}
OptionParser.new do |opts|
opts.on('-n', "--number COUNT", Integer) { |count| $options[:count] = count }
opts.on('-c', "--concurrency CONCURRENCY", Integer) { |concurrency| $options[:concurrency] = concurrency }
opts.on('-p', "--port PORT", Integer) { |port| $options[:port] = port }
end.parse!
end
count_per_worker = $options[:count] / $options[:concurrency]
benchmark = Benchmark.measure do
threads = []
$options[:concurrency].times do
threads << Thread.new do
socket = TCPSocket.new($options[:host], $options[:port])
count_per_worker.times do |i|
socket.print("\n")
socket.gets
end
end
end
threads.each(&:join)
end
mean = benchmark.real / count_per_worker
puts "Requests per second:\t%2f [#/sec] (mean)" % mean