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
20$:.unshift File.dirname(__FILE__) + '/../lib'
21require 'thrift'
22$:.unshift File.dirname(__FILE__) + "/gen-rb"
23require 'benchmark_service'
24
25class Client
26  def initialize(host, port, clients_per_process, calls_per_client, log_exceptions)
27    @host = host
28    @port = port
29    @clients_per_process = clients_per_process
30    @calls_per_client = calls_per_client
31    @log_exceptions = log_exceptions
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
41        start = Time.now
42        transport.open
43        Marshal.dump [:start, start], STDOUT
44      rescue => e
45        Marshal.dump [:connection_failure, Time.now], STDOUT
46        print_exception e if @log_exceptions
47      else
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
56        rescue Thrift::TransportException => e
57          Marshal.dump [:connection_error, Time.now], STDOUT
58          print_exception e if @log_exceptions
59        end
60      end
61    end
62  end
63
64  def print_exception(e)
65    STDERR.puts "ERROR: #{e.message}"
66    STDERR.puts "\t#{e.backtrace * "\n\t"}"
67  end
68end
69
70log_exceptions = true if ARGV[0] == '-log-exceptions' and ARGV.shift
71
72host, port, clients_per_process, calls_per_client = ARGV
73
74Client.new(host, port.to_i, clients_per_process.to_i, calls_per_client.to_i, log_exceptions).run
75