| 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 | module Server |
| 28 | include Thrift |
| 29 | |
| 30 | class BenchmarkHandler |
| 31 | # 1-based index into the fibonacci sequence |
| 32 | def fibonacci(n) |
| 33 | seq = [1, 1] |
| 34 | 3.upto(n) do |
| 35 | seq << seq[-1] + seq[-2] |
| 36 | end |
| 37 | seq[n-1] # n is 1-based |
| 38 | end |
| 39 | end |
| 40 | |
| Dmytro Shteflyuk | 67bfb29 | 2026-01-28 11:23:50 -0500 | [diff] [blame^] | 41 | def self.create_factories(protocol_type) |
| 42 | case protocol_type |
| 43 | when 'binary' |
| 44 | [FramedTransportFactory.new, BinaryProtocolFactory.new] |
| 45 | when 'compact' |
| 46 | [FramedTransportFactory.new, CompactProtocolFactory.new] |
| 47 | when 'header' |
| 48 | [HeaderTransportFactory.new, HeaderProtocolFactory.new] |
| 49 | when 'header-compact' |
| 50 | [HeaderTransportFactory.new, HeaderProtocolFactory.new(nil, HeaderSubprotocolID::COMPACT)] |
| 51 | when 'header-zlib' |
| 52 | # Note: Server doesn't add transforms - it mirrors client's transforms |
| 53 | [HeaderTransportFactory.new, HeaderProtocolFactory.new] |
| 54 | else |
| 55 | [FramedTransportFactory.new, BinaryProtocolFactory.new] |
| 56 | end |
| 57 | end |
| 58 | |
| 59 | def self.start_server(host, port, serverClass, tls, protocol_type = nil) |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 60 | handler = BenchmarkHandler.new |
| 61 | processor = ThriftBenchmark::BenchmarkService::Processor.new(handler) |
| Dmytro Shteflyuk | 0947594 | 2025-11-19 16:23:42 -0500 | [diff] [blame] | 62 | transport = 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, "server.crt"))) |
| 70 | ctx.cert_store = OpenSSL::X509::Store.new |
| 71 | ctx.cert_store.add_file(File.join(keys_dir, 'client.pem')) |
| 72 | ctx.key = OpenSSL::PKey::RSA.new(File.open(File.join(keys_dir, "server.key"))) |
| 73 | end |
| 74 | |
| 75 | Thrift::SSLServerSocket.new(host, port, ssl_context) |
| 76 | else |
| 77 | ServerSocket.new(host, port) |
| 78 | end |
| Dmytro Shteflyuk | 67bfb29 | 2026-01-28 11:23:50 -0500 | [diff] [blame^] | 79 | transport_factory, protocol_factory = create_factories(protocol_type || 'binary') |
| 80 | args = [processor, transport, transport_factory, protocol_factory, 20] |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 81 | if serverClass == NonblockingServer |
| 82 | logger = Logger.new(STDERR) |
| 83 | logger.level = Logger::WARN |
| 84 | args << logger |
| 85 | end |
| 86 | server = serverClass.new(*args) |
| 87 | @server_thread = Thread.new do |
| 88 | server.serve |
| 89 | end |
| 90 | @server = server |
| 91 | end |
| 92 | |
| 93 | def self.shutdown |
| 94 | return if @server.nil? |
| 95 | if @server.respond_to? :shutdown |
| 96 | @server.shutdown |
| 97 | else |
| 98 | @server_thread.kill |
| 99 | end |
| 100 | end |
| 101 | end |
| 102 | |
| 103 | def resolve_const(const) |
| 104 | const and const.split('::').inject(Object) { |k,c| k.const_get(c) } |
| 105 | end |
| 106 | |
| Dmytro Shteflyuk | 0947594 | 2025-11-19 16:23:42 -0500 | [diff] [blame] | 107 | tls = true if ARGV[0] == '-tls' and ARGV.shift |
| 108 | |
| Dmytro Shteflyuk | 67bfb29 | 2026-01-28 11:23:50 -0500 | [diff] [blame^] | 109 | host, port, serverklass, protocol_type = ARGV |
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 110 | |
| Dmytro Shteflyuk | 67bfb29 | 2026-01-28 11:23:50 -0500 | [diff] [blame^] | 111 | Server.start_server(host, port.to_i, resolve_const(serverklass), tls, protocol_type) |
| Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 112 | |
| Kevin Clark | 1a95a1d | 2008-06-18 01:19:28 +0000 | [diff] [blame] | 113 | # let our host know that the interpreter has started |
| 114 | # ideally we'd wait until the server was serving, but we don't have a hook for that |
| 115 | Marshal.dump(:started, STDOUT) |
| Kevin Clark | 1e8f520 | 2008-06-18 01:19:33 +0000 | [diff] [blame] | 116 | STDOUT.flush |
| Kevin Clark | 1a95a1d | 2008-06-18 01:19:28 +0000 | [diff] [blame] | 117 | |
| Kevin Clark | b679131 | 2008-06-18 01:19:23 +0000 | [diff] [blame] | 118 | Marshal.load(STDIN) # wait until we're instructed to shut down |
| Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 119 | |
| 120 | Server.shutdown |