| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # | 
|  | 2 | # Licensed to the Apache Software Foundation (ASF) under one | 
|  | 3 | # or more contributor license agreements. See the NOTICE file | 
|  | 4 | # distributed with this work for additional information | 
|  | 5 | # regarding copyright ownership. The ASF licenses this file | 
|  | 6 | # to you under the Apache License, Version 2.0 (the | 
|  | 7 | # "License"); you may not use this file except in compliance | 
|  | 8 | # with the License. You may obtain a copy of the License at | 
|  | 9 | # | 
|  | 10 | #   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 11 | # | 
|  | 12 | # Unless required by applicable law or agreed to in writing, | 
|  | 13 | # software distributed under the License is distributed on an | 
|  | 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 15 | # KIND, either express or implied. See the License for the | 
|  | 16 | # specific language governing permissions and limitations | 
|  | 17 | # under the License. | 
|  | 18 | # | 
|  | 19 |  | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 20 | $:.unshift File.dirname(__FILE__) + '/../lib' | 
|  | 21 | require 'thrift' | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 22 | $:.unshift File.dirname(__FILE__) + "/gen-rb" | 
| Bryan Duxbury | 0bbef92 | 2009-04-07 22:23:40 +0000 | [diff] [blame] | 23 | require 'benchmark_service' | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 24 |  | 
|  | 25 | class Client | 
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 26 | def initialize(host, port, clients_per_process, calls_per_client, log_exceptions) | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 27 | @host = host | 
|  | 28 | @port = port | 
|  | 29 | @clients_per_process = clients_per_process | 
|  | 30 | @calls_per_client = calls_per_client | 
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 31 | @log_exceptions = log_exceptions | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 32 | end | 
|  | 33 |  | 
|  | 34 | def run | 
|  | 35 | @clients_per_process.times do | 
|  | 36 | socket = Thrift::Socket.new(@host, @port) | 
|  | 37 | transport = Thrift::FramedTransport.new(socket) | 
|  | 38 | protocol = Thrift::BinaryProtocol.new(transport) | 
|  | 39 | client = ThriftBenchmark::BenchmarkService::Client.new(protocol) | 
|  | 40 | begin | 
| Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 41 | start = Time.now | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 42 | transport.open | 
| Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 43 | Marshal.dump [:start, start], STDOUT | 
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 44 | rescue => e | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 45 | Marshal.dump [:connection_failure, Time.now], STDOUT | 
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 46 | print_exception e if @log_exceptions | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 47 | else | 
| Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 48 | begin | 
|  | 49 | @calls_per_client.times do | 
|  | 50 | Marshal.dump [:call_start, Time.now], STDOUT | 
|  | 51 | client.fibonacci(15) | 
|  | 52 | Marshal.dump [:call_end, Time.now], STDOUT | 
|  | 53 | end | 
|  | 54 | transport.close | 
|  | 55 | Marshal.dump [:end, Time.now], STDOUT | 
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 56 | rescue Thrift::TransportException => e | 
| Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 57 | Marshal.dump [:connection_error, Time.now], STDOUT | 
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 58 | print_exception e if @log_exceptions | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 59 | end | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 60 | end | 
|  | 61 | end | 
|  | 62 | end | 
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 63 |  | 
|  | 64 | def print_exception(e) | 
|  | 65 | STDERR.puts "ERROR: #{e.message}" | 
|  | 66 | STDERR.puts "\t#{e.backtrace * "\n\t"}" | 
|  | 67 | end | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 68 | end | 
|  | 69 |  | 
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 70 | log_exceptions = true if ARGV[0] == '-log-exceptions' and ARGV.shift | 
|  | 71 |  | 
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 72 | host, port, clients_per_process, calls_per_client = ARGV | 
|  | 73 |  | 
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 74 | Client.new(host, port.to_i, clients_per_process.to_i, calls_per_client.to_i, log_exceptions).run |