blob: 0dd75feb08b61305cb74fb96f0843e3a7330a7c7 [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
Kevin Clark75532ee2008-06-18 01:19:14 +000029 return if @serverclass == Object
Kevin Clarkd3cee022008-06-18 01:19:09 +000030 @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 Clark75532ee2008-06-18 01:19:14 +0000166 [["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass],
Kevin Clarkd3cee022008-06-18 01:19:09 +0000167 [["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
Kevin Clark75532ee2008-06-18 01:19:14 +0000175 failures = (@report[:connection_failures] > 0)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000176 tabulate fmt,
Kevin Clark75532ee2008-06-18 01:19:14 +0000177 [["Connection failures", "%d", *(failures ? [[:red, :bold]] : [])], @report[:connection_failures]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000178 ["Average time per call", @report[:avg_calls]],
179 ["Average time per client (%d calls)" % @calls_per_client, @report[:avg_clients]],
180 ["Total time for all calls", @report[:total_calls]],
181 ["Real time for benchmarking", @report[:total_benchmark_time]],
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000182 ["Shortest call time", @report[:longest_call]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000183 ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]]
184 end
185
Kevin Clark75532ee2008-06-18 01:19:14 +0000186 ANSI = {
187 :reset => 0,
188 :bold => 1,
189 :black => 30,
190 :red => 31,
191 :green => 32,
192 :yellow => 33,
193 :blue => 34,
194 :magenta => 35,
195 :cyan => 36,
196 :white => 37
197 }
198
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000199 def tabulate(fmt, *labels_and_values)
200 labels = labels_and_values.map { |(l,)| Array === l ? l.first : l }
201 label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }
202 labels_and_values.each do |(l,v)|
203 f = fmt
Kevin Clark75532ee2008-06-18 01:19:14 +0000204 l, f, c = l if Array === l
205 fmtstr = "%-#{label_width+1}s #{f}"
206 if STDOUT.tty? and c
207 fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m"
208 end
209 puts fmtstr % [l+":", v]
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000210 end
211 end
212end
213
214def resolve_const(const)
215 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
216end
217
218puts "Starting server..."
Kevin Clarkd3cee022008-06-18 01:19:09 +0000219args = {}
220args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
221args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer
222server = Server.new(args)
223server.start
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000224
225sleep 0.2 # give the server time to start
226
Kevin Clarkd3cee022008-06-18 01:19:09 +0000227args = { :num_processes => 40, :clients_per_process => 5, :host => HOST, :port => PORT }
228args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
229BenchmarkManager.new(args, server).run
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000230
Kevin Clarkd3cee022008-06-18 01:19:09 +0000231server.shutdown