blob: 693bf60f14c3d96e63f6138c15edc93e1363310e [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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 Clarkd3cee022008-06-18 01:19:09 +000020$:.unshift File.dirname(__FILE__) + '/../lib'
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050021$:.unshift File.dirname(__FILE__) + '/../ext'
Kevin Clarkd3cee022008-06-18 01:19:09 +000022require 'thrift'
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050023require 'openssl'
Kevin Clarkd3cee022008-06-18 01:19:09 +000024$:.unshift File.dirname(__FILE__) + "/gen-rb"
Bryan Duxbury0bbef922009-04-07 22:23:40 +000025require 'benchmark_service'
Kevin Clarkd3cee022008-06-18 01:19:09 +000026
27class Client
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050028 def initialize(host, port, clients_per_process, calls_per_client, log_exceptions, tls)
Kevin Clarkd3cee022008-06-18 01:19:09 +000029 @host = host
30 @port = port
31 @clients_per_process = clients_per_process
32 @calls_per_client = calls_per_client
Kevin Clarkfdc9c972008-06-18 01:19:46 +000033 @log_exceptions = log_exceptions
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050034 @tls = tls
Kevin Clarkd3cee022008-06-18 01:19:09 +000035 end
36
37 def run
38 @clients_per_process.times do
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050039 socket = if @tls
40 ssl_context = OpenSSL::SSL::SSLContext.new.tap do |ctx|
41 ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
42 ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION
43
44 keys_dir = File.expand_path("../../../test/keys", __dir__)
45 ctx.ca_file = File.join(keys_dir, "CA.pem")
46 ctx.cert = OpenSSL::X509::Certificate.new(File.open(File.join(keys_dir, "client.crt")))
47 ctx.cert_store = OpenSSL::X509::Store.new
48 ctx.cert_store.add_file(File.join(keys_dir, 'server.pem'))
49 ctx.key = OpenSSL::PKey::RSA.new(File.open(File.join(keys_dir, "client.key")))
50 end
51
52 Thrift::SSLSocket.new(@host, @port, nil, ssl_context)
53 else
54 Thrift::Socket.new(@host, @port)
55 end
Kevin Clarkd3cee022008-06-18 01:19:09 +000056 transport = Thrift::FramedTransport.new(socket)
57 protocol = Thrift::BinaryProtocol.new(transport)
58 client = ThriftBenchmark::BenchmarkService::Client.new(protocol)
59 begin
Kevin Clarkd2719792008-06-18 01:19:37 +000060 start = Time.now
Kevin Clarkd3cee022008-06-18 01:19:09 +000061 transport.open
Kevin Clarkd2719792008-06-18 01:19:37 +000062 Marshal.dump [:start, start], STDOUT
Kevin Clarkfdc9c972008-06-18 01:19:46 +000063 rescue => e
Kevin Clarkd3cee022008-06-18 01:19:09 +000064 Marshal.dump [:connection_failure, Time.now], STDOUT
Kevin Clarkfdc9c972008-06-18 01:19:46 +000065 print_exception e if @log_exceptions
Kevin Clarkd3cee022008-06-18 01:19:09 +000066 else
Kevin Clarkd2719792008-06-18 01:19:37 +000067 begin
68 @calls_per_client.times do
69 Marshal.dump [:call_start, Time.now], STDOUT
70 client.fibonacci(15)
71 Marshal.dump [:call_end, Time.now], STDOUT
72 end
73 transport.close
74 Marshal.dump [:end, Time.now], STDOUT
Kevin Clarkfdc9c972008-06-18 01:19:46 +000075 rescue Thrift::TransportException => e
Kevin Clarkd2719792008-06-18 01:19:37 +000076 Marshal.dump [:connection_error, Time.now], STDOUT
Kevin Clarkfdc9c972008-06-18 01:19:46 +000077 print_exception e if @log_exceptions
Kevin Clarkd3cee022008-06-18 01:19:09 +000078 end
Kevin Clarkd3cee022008-06-18 01:19:09 +000079 end
80 end
81 end
Kevin Clarkfdc9c972008-06-18 01:19:46 +000082
83 def print_exception(e)
84 STDERR.puts "ERROR: #{e.message}"
85 STDERR.puts "\t#{e.backtrace * "\n\t"}"
86 end
Kevin Clarkd3cee022008-06-18 01:19:09 +000087end
88
Kevin Clarkfdc9c972008-06-18 01:19:46 +000089log_exceptions = true if ARGV[0] == '-log-exceptions' and ARGV.shift
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050090tls = true if ARGV[0] == '-tls' and ARGV.shift
Kevin Clarkfdc9c972008-06-18 01:19:46 +000091
Kevin Clarkd3cee022008-06-18 01:19:09 +000092host, port, clients_per_process, calls_per_client = ARGV
93
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050094Client.new(host, port.to_i, clients_per_process.to_i, calls_per_client.to_i, log_exceptions, tls).run