Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 1 | $:.unshift File.dirname(__FILE__) + '/../lib' |
| 2 | require 'thrift' |
| 3 | require 'thrift/server/nonblockingserver' |
| 4 | $:.unshift File.dirname(__FILE__) + "/gen-rb" |
| 5 | require 'BenchmarkService' |
| 6 | |
| 7 | class Client |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 8 | def initialize(host, port, clients_per_process, calls_per_client, log_exceptions) |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 9 | @host = host |
| 10 | @port = port |
| 11 | @clients_per_process = clients_per_process |
| 12 | @calls_per_client = calls_per_client |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 13 | @log_exceptions = log_exceptions |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 14 | end |
| 15 | |
| 16 | def run |
| 17 | @clients_per_process.times do |
| 18 | socket = Thrift::Socket.new(@host, @port) |
| 19 | transport = Thrift::FramedTransport.new(socket) |
| 20 | protocol = Thrift::BinaryProtocol.new(transport) |
| 21 | client = ThriftBenchmark::BenchmarkService::Client.new(protocol) |
| 22 | begin |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 23 | start = Time.now |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 24 | transport.open |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 25 | Marshal.dump [:start, start], STDOUT |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 26 | rescue => e |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 27 | Marshal.dump [:connection_failure, Time.now], STDOUT |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 28 | print_exception e if @log_exceptions |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 29 | else |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 30 | begin |
| 31 | @calls_per_client.times do |
| 32 | Marshal.dump [:call_start, Time.now], STDOUT |
| 33 | client.fibonacci(15) |
| 34 | Marshal.dump [:call_end, Time.now], STDOUT |
| 35 | end |
| 36 | transport.close |
| 37 | Marshal.dump [:end, Time.now], STDOUT |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 38 | rescue Thrift::TransportException => e |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 39 | Marshal.dump [:connection_error, Time.now], STDOUT |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 40 | print_exception e if @log_exceptions |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 41 | end |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 42 | end |
| 43 | end |
| 44 | end |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 45 | |
| 46 | def print_exception(e) |
| 47 | STDERR.puts "ERROR: #{e.message}" |
| 48 | STDERR.puts "\t#{e.backtrace * "\n\t"}" |
| 49 | end |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 50 | end |
| 51 | |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 52 | log_exceptions = true if ARGV[0] == '-log-exceptions' and ARGV.shift |
| 53 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 54 | host, port, clients_per_process, calls_per_client = ARGV |
| 55 | |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 56 | Client.new(host, port.to_i, clients_per_process.to_i, calls_per_client.to_i, log_exceptions).run |