Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 1 | require 'rubygems' |
| 2 | $:.unshift File.dirname(__FILE__) + '/../lib' |
| 3 | require 'thrift' |
| 4 | require 'thrift/server/nonblockingserver' |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 5 | require 'thrift/transport/unixsocket' |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 6 | require 'stringio' |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 7 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 8 | HOST = 'localhost' |
| 9 | PORT = 42587 |
| 10 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 11 | ############### |
| 12 | ## Server |
| 13 | ############### |
| 14 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 15 | class Server |
| 16 | attr_accessor :serverclass |
| 17 | attr_accessor :interpreter |
| 18 | attr_accessor :host |
| 19 | attr_accessor :port |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 20 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 21 | 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 Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 26 | end |
| 27 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 28 | def start |
| 29 | return if @class == Object |
| 30 | @pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/server.rb #{@host} #{@port} #{@serverclass.name}", "r+") |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 31 | end |
| 32 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 33 | 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 Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 39 | end |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 40 | @pipe.close |
| 41 | @pipe = nil |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 42 | end |
| 43 | end |
| 44 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 45 | class BenchmarkManager |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 46 | def initialize(opts, server) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 47 | @socket = opts.fetch(:socket) do |
| 48 | @host = opts.fetch(:host, 'localhost') |
| 49 | @port = opts.fetch(:port) |
| 50 | nil |
| 51 | end |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 52 | @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 Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 55 | @interpreter = opts.fetch(:interpreter, "ruby") |
| 56 | @server = server |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 57 | 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 Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 75 | pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}") |
| 76 | @pool << pipe |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 77 | end |
| 78 | |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 79 | def socket_class |
| 80 | if @socket |
| 81 | Thrift::UNIXSocket |
| 82 | else |
| 83 | Thrift::Socket |
| 84 | end |
| 85 | end |
| 86 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 87 | 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 Clark | fb5c0eb | 2008-06-18 01:19:04 +0000 | [diff] [blame] | 96 | @buffers[fd] << fd.readpartial(4096) |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 97 | 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 Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 166 | [["Server class", "%s"], @server.serverclass], |
| 167 | [["Server interpreter", "%s"], @server.interpreter], |
| 168 | [["Client interpreter", "%s"], @interpreter], |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 169 | [["Socket class", "%s"], socket_class], |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 170 | ["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 Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 181 | ["Shortest call time", @report[:longest_call]], |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 182 | ["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 |
| 194 | end |
| 195 | |
| 196 | def resolve_const(const) |
| 197 | const and const.split('::').inject(Object) { |k,c| k.const_get(c) } |
| 198 | end |
| 199 | |
| 200 | puts "Starting server..." |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 201 | args = {} |
| 202 | args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby" |
| 203 | args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer |
| 204 | server = Server.new(args) |
| 205 | server.start |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 206 | |
| 207 | sleep 0.2 # give the server time to start |
| 208 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 209 | args = { :num_processes => 40, :clients_per_process => 5, :host => HOST, :port => PORT } |
| 210 | args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby" |
| 211 | BenchmarkManager.new(args, server).run |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 212 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame^] | 213 | server.shutdown |