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 | 77b39b3 | 2008-06-18 01:20:16 +0000 | [diff] [blame] | 8 | HOST = '127.0.0.1' |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 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 |
Kevin Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 29 | return if @serverclass == Object |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 30 | args = (File.basename(@interpreter) == "jruby" ? "-J-server" : "") |
| 31 | @pipe = IO.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{@host} #{@port} #{@serverclass.name}", "r+") |
Kevin Clark | 1a95a1d | 2008-06-18 01:19:28 +0000 | [diff] [blame] | 32 | Marshal.load(@pipe) # wait until the server has started |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 33 | sleep 0.4 # give the server time to actually start spawning sockets |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 34 | end |
| 35 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 36 | def shutdown |
| 37 | return unless @pipe |
| 38 | Marshal.dump(:shutdown, @pipe) |
| 39 | begin |
| 40 | @pipe.read(10) # block until the server shuts down |
| 41 | rescue EOFError |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 42 | end |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 43 | @pipe.close |
| 44 | @pipe = nil |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 45 | end |
| 46 | end |
| 47 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 48 | class BenchmarkManager |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 49 | def initialize(opts, server) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 50 | @socket = opts.fetch(:socket) do |
| 51 | @host = opts.fetch(:host, 'localhost') |
| 52 | @port = opts.fetch(:port) |
| 53 | nil |
| 54 | end |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 55 | @num_processes = opts.fetch(:num_processes, 40) |
| 56 | @clients_per_process = opts.fetch(:clients_per_process, 10) |
| 57 | @calls_per_client = opts.fetch(:calls_per_client, 50) |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 58 | @interpreter = opts.fetch(:interpreter, "ruby") |
| 59 | @server = server |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 60 | @log_exceptions = opts.fetch(:log_exceptions, false) |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 61 | end |
| 62 | |
| 63 | def run |
| 64 | @pool = [] |
| 65 | @benchmark_start = Time.now |
| 66 | puts "Spawning benchmark processes..." |
| 67 | @num_processes.times do |
| 68 | spawn |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 69 | sleep 0.02 # space out spawns |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 70 | end |
| 71 | collect_output |
| 72 | @benchmark_end = Time.now # we know the procs are done here |
| 73 | translate_output |
| 74 | analyze_output |
| 75 | report_output |
| 76 | end |
| 77 | |
| 78 | def spawn |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 79 | pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{"-log-exceptions" if @log_exceptions} #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}") |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 80 | @pool << pipe |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 81 | end |
| 82 | |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 83 | def socket_class |
| 84 | if @socket |
| 85 | Thrift::UNIXSocket |
| 86 | else |
| 87 | Thrift::Socket |
| 88 | end |
| 89 | end |
| 90 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 91 | def collect_output |
| 92 | puts "Collecting output..." |
| 93 | # read from @pool until all sockets are closed |
| 94 | @buffers = Hash.new { |h,k| h[k] = '' } |
| 95 | until @pool.empty? |
| 96 | rd, = select(@pool) |
| 97 | next if rd.nil? |
| 98 | rd.each do |fd| |
| 99 | begin |
Kevin Clark | fb5c0eb | 2008-06-18 01:19:04 +0000 | [diff] [blame] | 100 | @buffers[fd] << fd.readpartial(4096) |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 101 | rescue EOFError |
| 102 | @pool.delete fd |
| 103 | end |
| 104 | end |
| 105 | end |
| 106 | end |
| 107 | |
| 108 | def translate_output |
| 109 | puts "Translating output..." |
| 110 | @output = [] |
| 111 | @buffers.each do |fd, buffer| |
| 112 | strio = StringIO.new(buffer) |
| 113 | logs = [] |
| 114 | begin |
| 115 | loop do |
| 116 | logs << Marshal.load(strio) |
| 117 | end |
| 118 | rescue EOFError |
| 119 | @output << logs |
| 120 | end |
| 121 | end |
| 122 | end |
| 123 | |
| 124 | def analyze_output |
| 125 | puts "Analyzing output..." |
| 126 | call_times = [] |
| 127 | client_times = [] |
| 128 | connection_failures = [] |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 129 | connection_errors = [] |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 130 | shortest_call = 0 |
| 131 | shortest_client = 0 |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 132 | longest_call = 0 |
| 133 | longest_client = 0 |
| 134 | @output.each do |logs| |
| 135 | cur_call, cur_client = nil |
| 136 | logs.each do |tok, time| |
| 137 | case tok |
| 138 | when :start |
| 139 | cur_client = time |
| 140 | when :call_start |
| 141 | cur_call = time |
| 142 | when :call_end |
| 143 | delta = time - cur_call |
| 144 | call_times << delta |
| 145 | longest_call = delta unless longest_call > delta |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 146 | shortest_call = delta if shortest_call == 0 or delta < shortest_call |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 147 | cur_call = nil |
| 148 | when :end |
| 149 | delta = time - cur_client |
| 150 | client_times << delta |
| 151 | longest_client = delta unless longest_client > delta |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 152 | shortest_client = delta if shortest_client == 0 or delta < shortest_client |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 153 | cur_client = nil |
| 154 | when :connection_failure |
| 155 | connection_failures << time |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 156 | when :connection_error |
| 157 | connection_errors << time |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 158 | end |
| 159 | end |
| 160 | end |
| 161 | @report = {} |
| 162 | @report[:total_calls] = call_times.inject(0.0) { |a,t| a += t } |
| 163 | @report[:avg_calls] = @report[:total_calls] / call_times.size |
| 164 | @report[:total_clients] = client_times.inject(0.0) { |a,t| a += t } |
| 165 | @report[:avg_clients] = @report[:total_clients] / client_times.size |
| 166 | @report[:connection_failures] = connection_failures.size |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 167 | @report[:connection_errors] = connection_errors.size |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 168 | @report[:shortest_call] = shortest_call |
| 169 | @report[:shortest_client] = shortest_client |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 170 | @report[:longest_call] = longest_call |
| 171 | @report[:longest_client] = longest_client |
| 172 | @report[:total_benchmark_time] = @benchmark_end - @benchmark_start |
| 173 | @report[:fastthread] = $".include?('fastthread.bundle') |
| 174 | end |
| 175 | |
| 176 | def report_output |
| 177 | fmt = "%.4f seconds" |
| 178 | puts |
| 179 | tabulate "%d", |
Kevin Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 180 | [["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass], |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 181 | [["Server interpreter", "%s"], @server.interpreter], |
| 182 | [["Client interpreter", "%s"], @interpreter], |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 183 | [["Socket class", "%s"], socket_class], |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 184 | ["Number of processes", @num_processes], |
| 185 | ["Clients per process", @clients_per_process], |
| 186 | ["Calls per client", @calls_per_client], |
| 187 | [["Using fastthread", "%s"], @report[:fastthread] ? "yes" : "no"] |
| 188 | puts |
Kevin Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 189 | failures = (@report[:connection_failures] > 0) |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 190 | tabulate fmt, |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 191 | [["Connection failures", "%d", [:red, :bold]], @report[:connection_failures]], |
| 192 | [["Connection errors", "%d", [:red, :bold]], @report[:connection_errors]], |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 193 | ["Average time per call", @report[:avg_calls]], |
| 194 | ["Average time per client (%d calls)" % @calls_per_client, @report[:avg_clients]], |
| 195 | ["Total time for all calls", @report[:total_calls]], |
| 196 | ["Real time for benchmarking", @report[:total_benchmark_time]], |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 197 | ["Shortest call time", @report[:shortest_call]], |
| 198 | ["Longest call time", @report[:longest_call]], |
| 199 | ["Shortest client time (%d calls)" % @calls_per_client, @report[:shortest_client]], |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 200 | ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]] |
| 201 | end |
| 202 | |
Kevin Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 203 | ANSI = { |
| 204 | :reset => 0, |
| 205 | :bold => 1, |
| 206 | :black => 30, |
| 207 | :red => 31, |
| 208 | :green => 32, |
| 209 | :yellow => 33, |
| 210 | :blue => 34, |
| 211 | :magenta => 35, |
| 212 | :cyan => 36, |
| 213 | :white => 37 |
| 214 | } |
| 215 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 216 | def tabulate(fmt, *labels_and_values) |
| 217 | labels = labels_and_values.map { |(l,)| Array === l ? l.first : l } |
| 218 | label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w } |
| 219 | labels_and_values.each do |(l,v)| |
| 220 | f = fmt |
Kevin Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 221 | l, f, c = l if Array === l |
| 222 | fmtstr = "%-#{label_width+1}s #{f}" |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 223 | if STDOUT.tty? and c and v.to_i > 0 |
Kevin Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 224 | fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m" |
| 225 | end |
| 226 | puts fmtstr % [l+":", v] |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 227 | end |
| 228 | end |
| 229 | end |
| 230 | |
| 231 | def resolve_const(const) |
| 232 | const and const.split('::').inject(Object) { |k,c| k.const_get(c) } |
| 233 | end |
| 234 | |
| 235 | puts "Starting server..." |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 236 | args = {} |
| 237 | args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby" |
| 238 | args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer |
Kevin Clark | d8d0d60 | 2008-06-18 01:20:10 +0000 | [diff] [blame] | 239 | args[:host] = ENV['THRIFT_HOST'] || HOST |
| 240 | args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 241 | server = Server.new(args) |
| 242 | server.start |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 243 | |
Kevin Clark | d8d0d60 | 2008-06-18 01:20:10 +0000 | [diff] [blame] | 244 | args = {} |
| 245 | args[:host] = ENV['THRIFT_HOST'] || HOST |
| 246 | args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i |
Kevin Clark | 4b429ad | 2008-06-18 01:20:06 +0000 | [diff] [blame] | 247 | args[:num_processes] = (ENV['THRIFT_NUM_PROCESSES'] || 40).to_i |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 248 | args[:clients_per_process] = (ENV['THRIFT_NUM_CLIENTS'] || 5).to_i |
| 249 | args[:calls_per_client] = (ENV['THRIFT_NUM_CALLS'] || 50).to_i |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 250 | args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby" |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 251 | args[:log_exceptions] = !!ENV['THRIFT_LOG_EXCEPTIONS'] |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 252 | BenchmarkManager.new(args, server).run |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 253 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 254 | server.shutdown |