| 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' |
| Dmytro Shteflyuk | 0947594 | 2025-11-19 16:23:42 -0500 | [diff] [blame] | 21 | $:.unshift File.dirname(__FILE__) + '/../ext' |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 22 | require 'thrift' |
| Dmytro Shteflyuk | 0947594 | 2025-11-19 16:23:42 -0500 | [diff] [blame] | 23 | require 'openssl' |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 24 | $:.unshift File.dirname(__FILE__) + "/gen-rb" |
| Bryan Duxbury | 0bbef92 | 2009-04-07 22:23:40 +0000 | [diff] [blame] | 25 | require 'benchmark_service' |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 26 | |
| 27 | class Client |
| Dmytro Shteflyuk | 0947594 | 2025-11-19 16:23:42 -0500 | [diff] [blame] | 28 | def initialize(host, port, clients_per_process, calls_per_client, log_exceptions, tls) |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 29 | @host = host |
| 30 | @port = port |
| 31 | @clients_per_process = clients_per_process |
| 32 | @calls_per_client = calls_per_client |
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 33 | @log_exceptions = log_exceptions |
| Dmytro Shteflyuk | 0947594 | 2025-11-19 16:23:42 -0500 | [diff] [blame] | 34 | @tls = tls |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 35 | end |
| 36 | |
| 37 | def run |
| 38 | @clients_per_process.times do |
| Dmytro Shteflyuk | 0947594 | 2025-11-19 16:23:42 -0500 | [diff] [blame] | 39 | 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 Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 56 | transport = Thrift::FramedTransport.new(socket) |
| 57 | protocol = Thrift::BinaryProtocol.new(transport) |
| 58 | client = ThriftBenchmark::BenchmarkService::Client.new(protocol) |
| 59 | begin |
| Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 60 | start = Time.now |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 61 | transport.open |
| Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 62 | Marshal.dump [:start, start], STDOUT |
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 63 | rescue => e |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 64 | Marshal.dump [:connection_failure, Time.now], STDOUT |
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 65 | print_exception e if @log_exceptions |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 66 | else |
| Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 67 | 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 Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 75 | rescue Thrift::TransportException => e |
| Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 76 | Marshal.dump [:connection_error, Time.now], STDOUT |
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 77 | print_exception e if @log_exceptions |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 78 | end |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 79 | end |
| 80 | end |
| 81 | end |
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 82 | |
| 83 | def print_exception(e) |
| 84 | STDERR.puts "ERROR: #{e.message}" |
| 85 | STDERR.puts "\t#{e.backtrace * "\n\t"}" |
| 86 | end |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 87 | end |
| 88 | |
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 89 | log_exceptions = true if ARGV[0] == '-log-exceptions' and ARGV.shift |
| Dmytro Shteflyuk | 0947594 | 2025-11-19 16:23:42 -0500 | [diff] [blame] | 90 | tls = true if ARGV[0] == '-tls' and ARGV.shift |
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 91 | |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 92 | host, port, clients_per_process, calls_per_client = ARGV |
| 93 | |
| Dmytro Shteflyuk | 0947594 | 2025-11-19 16:23:42 -0500 | [diff] [blame] | 94 | Client.new(host, port.to_i, clients_per_process.to_i, calls_per_client.to_i, log_exceptions, tls).run |