blob: 5e6dd40e306be0ee74d27620fa7fdd3e50347041 [file] [log] [blame]
Kevin Clarkca8a1b32008-06-18 01:17:06 +00001require 'rubygems'
2$:.unshift File.dirname(__FILE__) + '/../lib'
3require 'thrift'
4require 'thrift/server/nonblockingserver'
Kevin Clark2ddd8ed2008-06-18 01:18:35 +00005require 'thrift/transport/unixsocket'
Kevin Clarkca8a1b32008-06-18 01:17:06 +00006require 'stringio'
Kevin Clarkd3cee022008-06-18 01:19:09 +00007
Kevin Clarkca8a1b32008-06-18 01:17:06 +00008HOST = 'localhost'
9PORT = 42587
10
Kevin Clarkca8a1b32008-06-18 01:17:06 +000011###############
12## Server
13###############
14
Kevin Clarkd3cee022008-06-18 01:19:09 +000015class Server
16 attr_accessor :serverclass
17 attr_accessor :interpreter
18 attr_accessor :host
19 attr_accessor :port
Kevin Clarkca8a1b32008-06-18 01:17:06 +000020
Kevin Clarkd3cee022008-06-18 01:19:09 +000021 def initialize(opts)
22 @serverclass = opts.fetch(:class, Thrift::NonblockingServer)
23 @interpreter = opts.fetch(:interpreter, "ruby")
24 @host = opts.fetch(:host, ::HOST)
25 @port = opts.fetch(:port, ::PORT)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000026 end
27
Kevin Clarkd3cee022008-06-18 01:19:09 +000028 def start
29 return if @class == Object
30 @pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/server.rb #{@host} #{@port} #{@serverclass.name}", "r+")
Kevin Clarkca8a1b32008-06-18 01:17:06 +000031 end
32
Kevin Clarkd3cee022008-06-18 01:19:09 +000033 def shutdown
34 return unless @pipe
35 Marshal.dump(:shutdown, @pipe)
36 begin
37 @pipe.read(10) # block until the server shuts down
38 rescue EOFError
Kevin Clarkca8a1b32008-06-18 01:17:06 +000039 end
Kevin Clarkd3cee022008-06-18 01:19:09 +000040 @pipe.close
41 @pipe = nil
Kevin Clarkca8a1b32008-06-18 01:17:06 +000042 end
43end
44
Kevin Clarkca8a1b32008-06-18 01:17:06 +000045class BenchmarkManager
Kevin Clarkd3cee022008-06-18 01:19:09 +000046 def initialize(opts, server)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000047 @socket = opts.fetch(:socket) do
48 @host = opts.fetch(:host, 'localhost')
49 @port = opts.fetch(:port)
50 nil
51 end
Kevin Clarkca8a1b32008-06-18 01:17:06 +000052 @num_processes = opts.fetch(:num_processes, 40)
53 @clients_per_process = opts.fetch(:clients_per_process, 10)
54 @calls_per_client = opts.fetch(:calls_per_client, 50)
Kevin Clarkd3cee022008-06-18 01:19:09 +000055 @interpreter = opts.fetch(:interpreter, "ruby")
56 @server = server
Kevin Clarkca8a1b32008-06-18 01:17:06 +000057 end
58
59 def run
60 @pool = []
61 @benchmark_start = Time.now
62 puts "Spawning benchmark processes..."
63 @num_processes.times do
64 spawn
65 sleep 0.05 # space out spawns
66 end
67 collect_output
68 @benchmark_end = Time.now # we know the procs are done here
69 translate_output
70 analyze_output
71 report_output
72 end
73
74 def spawn
Kevin Clarkd3cee022008-06-18 01:19:09 +000075 pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}")
76 @pool << pipe
Kevin Clarkca8a1b32008-06-18 01:17:06 +000077 end
78
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000079 def socket_class
80 if @socket
81 Thrift::UNIXSocket
82 else
83 Thrift::Socket
84 end
85 end
86
Kevin Clarkca8a1b32008-06-18 01:17:06 +000087 def collect_output
88 puts "Collecting output..."
89 # read from @pool until all sockets are closed
90 @buffers = Hash.new { |h,k| h[k] = '' }
91 until @pool.empty?
92 rd, = select(@pool)
93 next if rd.nil?
94 rd.each do |fd|
95 begin
Kevin Clarkfb5c0eb2008-06-18 01:19:04 +000096 @buffers[fd] << fd.readpartial(4096)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000097 rescue EOFError
98 @pool.delete fd
99 end
100 end
101 end
102 end
103
104 def translate_output
105 puts "Translating output..."
106 @output = []
107 @buffers.each do |fd, buffer|
108 strio = StringIO.new(buffer)
109 logs = []
110 begin
111 loop do
112 logs << Marshal.load(strio)
113 end
114 rescue EOFError
115 @output << logs
116 end
117 end
118 end
119
120 def analyze_output
121 puts "Analyzing output..."
122 call_times = []
123 client_times = []
124 connection_failures = []
125 longest_call = 0
126 longest_client = 0
127 @output.each do |logs|
128 cur_call, cur_client = nil
129 logs.each do |tok, time|
130 case tok
131 when :start
132 cur_client = time
133 when :call_start
134 cur_call = time
135 when :call_end
136 delta = time - cur_call
137 call_times << delta
138 longest_call = delta unless longest_call > delta
139 cur_call = nil
140 when :end
141 delta = time - cur_client
142 client_times << delta
143 longest_client = delta unless longest_client > delta
144 cur_client = nil
145 when :connection_failure
146 connection_failures << time
147 end
148 end
149 end
150 @report = {}
151 @report[:total_calls] = call_times.inject(0.0) { |a,t| a += t }
152 @report[:avg_calls] = @report[:total_calls] / call_times.size
153 @report[:total_clients] = client_times.inject(0.0) { |a,t| a += t }
154 @report[:avg_clients] = @report[:total_clients] / client_times.size
155 @report[:connection_failures] = connection_failures.size
156 @report[:longest_call] = longest_call
157 @report[:longest_client] = longest_client
158 @report[:total_benchmark_time] = @benchmark_end - @benchmark_start
159 @report[:fastthread] = $".include?('fastthread.bundle')
160 end
161
162 def report_output
163 fmt = "%.4f seconds"
164 puts
165 tabulate "%d",
Kevin Clarkd3cee022008-06-18 01:19:09 +0000166 [["Server class", "%s"], @server.serverclass],
167 [["Server interpreter", "%s"], @server.interpreter],
168 [["Client interpreter", "%s"], @interpreter],
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000169 [["Socket class", "%s"], socket_class],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000170 ["Number of processes", @num_processes],
171 ["Clients per process", @clients_per_process],
172 ["Calls per client", @calls_per_client],
173 [["Using fastthread", "%s"], @report[:fastthread] ? "yes" : "no"]
174 puts
175 tabulate fmt,
176 [["Connection failures", "%d"], @report[:connection_failures]],
177 ["Average time per call", @report[:avg_calls]],
178 ["Average time per client (%d calls)" % @calls_per_client, @report[:avg_clients]],
179 ["Total time for all calls", @report[:total_calls]],
180 ["Real time for benchmarking", @report[:total_benchmark_time]],
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000181 ["Shortest call time", @report[:longest_call]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000182 ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]]
183 end
184
185 def tabulate(fmt, *labels_and_values)
186 labels = labels_and_values.map { |(l,)| Array === l ? l.first : l }
187 label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }
188 labels_and_values.each do |(l,v)|
189 f = fmt
190 l, f = l if Array === l
191 puts "%-#{label_width+1}s #{f}" % [l+":", v]
192 end
193 end
194end
195
196def resolve_const(const)
197 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
198end
199
200puts "Starting server..."
Kevin Clarkd3cee022008-06-18 01:19:09 +0000201args = {}
202args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
203args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer
204server = Server.new(args)
205server.start
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000206
207sleep 0.2 # give the server time to start
208
Kevin Clarkd3cee022008-06-18 01:19:09 +0000209args = { :num_processes => 40, :clients_per_process => 5, :host => HOST, :port => PORT }
210args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
211BenchmarkManager.new(args, server).run
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000212
Kevin Clarkd3cee022008-06-18 01:19:09 +0000213server.shutdown