-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhellod.rb
More file actions
126 lines (113 loc) · 2.81 KB
/
hellod.rb
File metadata and controls
126 lines (113 loc) · 2.81 KB
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
require 'yaml'
class Hellod
PORTS = { 'ruby' => 8080,
'node' => 8081,
'go' => 8082,
'clj-aleph' => '8083',
'clj-jetty' => '8084',
'c' => '8085',
'java-nio' => '8086',
'java-netty' => '8087'}
CMDS = { 'ruby' => 'ruby ruby/server.rb',
'node' => 'node node/server.js',
'go' => 'GOMAXPROCS=6; ./go/hellod',
'clj-aleph' => 'cd clj-aleph; lein run',
'clj-jetty' => 'cd clj-jetty; lein run',
'c' => './c/hellod',
'java-nio' => 'cd java-nio; ./hellod',
'java-netty' => 'cd java-netty; ./hellod' }
def initialize(opts = {})
opts = { :requests => 10000, :concurrent => 50 }.merge(opts)
@n = opts[:requests]
@c = opts[:concurrent]
@pids = {}
end
def start(flavor)
read
ports(flavor[0]).each do |f, p|
print "Starting #{f} server on port #{p}..."
if @pids[f]
puts " already running with pid #{@pids[f]}"
else
begin
pipe = IO.popen CMDS[f], 'r'
puts " pid #{pipe.pid}"
@pids[f] = pipe.pid
rescue
puts " failed"
end
end
end
write
end
def stop(flavor)
read
ports(flavor[0]).each do |f, p|
pid = @pids[f]
print "Stopping #{f} server with pid #{pid}..."
if pid
begin
Process.kill 'HUP', pid
Process.kill 'HUP', (pid + 2) if f =~ /^clj/ # crazy ass hack -mike
puts " done"
rescue
puts " not running"
end
@pids.delete f
else
puts " not running"
end
end
write
end
def test(flavor)
read
ports(flavor[0]).each do |f, p|
puts "\tTesting #{f} with -n #{@n} -c #{@c}"
if @pids[f]
test_run p, true
5.times { test_run p }
else
puts "Not started"
end
puts
end
end
private
def ports(flavor)
if flavor == 'all'
PORTS
else
{ flavor => PORTS[flavor] }
end
end
def config_file
"#{ENV['HOME']}/.hellod"
end
def write
File.open config_file, 'w' do |f|
f.write @pids.to_yaml
end
end
def read
@pids = YAML.load( IO.read(config_file) ) if File.exists? config_file
@pids = {} unless @pids
end
def test_run(port, header = false)
h = header ? 0 : 1
failures = 0
values = []
IO.popen "ab -r -n #{@n} -c #{@c} http://localhost:#{port}/ 2>&1", 'r' do |io|
io.readlines.each do |line|
failures = line.split(":")[1].strip.to_i / 2 if line =~ /^Failed requests:/
values << line.split[h] if line =~ /\d+%/
end
end
printf "\t"
values.each do |val|
print sprintf("%6s", val)
end
printf " (#{failures} failures)" if failures > 0 && ! header
puts
end
end