blob: 304e6d8971af6ecb5b87aa3c11c5c8a0e6c936e9 [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 Shteflyuk67bfb292026-01-28 11:23:50 -050028 def initialize(host, port, clients_per_process, calls_per_client, log_exceptions, tls, protocol_type)
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
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -050035 @protocol_type = protocol_type || 'binary'
36 end
37
38 def create_protocol(socket)
39 case @protocol_type
40 when 'binary'
41 transport = Thrift::FramedTransport.new(socket)
42 Thrift::BinaryProtocol.new(transport)
43 when 'compact'
44 transport = Thrift::FramedTransport.new(socket)
45 Thrift::CompactProtocol.new(transport)
46 when 'header'
47 Thrift::HeaderProtocol.new(socket)
48 when 'header-compact'
49 Thrift::HeaderProtocol.new(socket, nil, Thrift::HeaderSubprotocolID::COMPACT)
50 when 'header-zlib'
51 protocol = Thrift::HeaderProtocol.new(socket)
52 protocol.add_transform(Thrift::HeaderTransformID::ZLIB)
53 protocol
54 else
55 transport = Thrift::FramedTransport.new(socket)
56 Thrift::BinaryProtocol.new(transport)
57 end
Kevin Clarkd3cee022008-06-18 01:19:09 +000058 end
59
60 def run
61 @clients_per_process.times do
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050062 socket = if @tls
63 ssl_context = OpenSSL::SSL::SSLContext.new.tap do |ctx|
64 ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
65 ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION
66
67 keys_dir = File.expand_path("../../../test/keys", __dir__)
68 ctx.ca_file = File.join(keys_dir, "CA.pem")
69 ctx.cert = OpenSSL::X509::Certificate.new(File.open(File.join(keys_dir, "client.crt")))
70 ctx.cert_store = OpenSSL::X509::Store.new
71 ctx.cert_store.add_file(File.join(keys_dir, 'server.pem'))
72 ctx.key = OpenSSL::PKey::RSA.new(File.open(File.join(keys_dir, "client.key")))
73 end
74
75 Thrift::SSLSocket.new(@host, @port, nil, ssl_context)
76 else
77 Thrift::Socket.new(@host, @port)
78 end
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -050079 protocol = create_protocol(socket)
80 transport = protocol.trans
Kevin Clarkd3cee022008-06-18 01:19:09 +000081 client = ThriftBenchmark::BenchmarkService::Client.new(protocol)
82 begin
Kevin Clarkd2719792008-06-18 01:19:37 +000083 start = Time.now
Kevin Clarkd3cee022008-06-18 01:19:09 +000084 transport.open
Kevin Clarkd2719792008-06-18 01:19:37 +000085 Marshal.dump [:start, start], STDOUT
Kevin Clarkfdc9c972008-06-18 01:19:46 +000086 rescue => e
Kevin Clarkd3cee022008-06-18 01:19:09 +000087 Marshal.dump [:connection_failure, Time.now], STDOUT
Kevin Clarkfdc9c972008-06-18 01:19:46 +000088 print_exception e if @log_exceptions
Kevin Clarkd3cee022008-06-18 01:19:09 +000089 else
Kevin Clarkd2719792008-06-18 01:19:37 +000090 begin
91 @calls_per_client.times do
92 Marshal.dump [:call_start, Time.now], STDOUT
93 client.fibonacci(15)
94 Marshal.dump [:call_end, Time.now], STDOUT
95 end
96 transport.close
97 Marshal.dump [:end, Time.now], STDOUT
Kevin Clarkfdc9c972008-06-18 01:19:46 +000098 rescue Thrift::TransportException => e
Kevin Clarkd2719792008-06-18 01:19:37 +000099 Marshal.dump [:connection_error, Time.now], STDOUT
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000100 print_exception e if @log_exceptions
Kevin Clarkd3cee022008-06-18 01:19:09 +0000101 end
Kevin Clarkd3cee022008-06-18 01:19:09 +0000102 end
103 end
104 end
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000105
106 def print_exception(e)
107 STDERR.puts "ERROR: #{e.message}"
108 STDERR.puts "\t#{e.backtrace * "\n\t"}"
109 end
Kevin Clarkd3cee022008-06-18 01:19:09 +0000110end
111
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000112log_exceptions = true if ARGV[0] == '-log-exceptions' and ARGV.shift
Dmytro Shteflyuk09475942025-11-19 16:23:42 -0500113tls = true if ARGV[0] == '-tls' and ARGV.shift
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000114
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -0500115host, port, clients_per_process, calls_per_client, protocol_type = ARGV
Kevin Clarkd3cee022008-06-18 01:19:09 +0000116
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -0500117Client.new(host, port.to_i, clients_per_process.to_i, calls_per_client.to_i, log_exceptions, tls, protocol_type).run