1#!/usr/bin/env ruby 2 3# Copyright 2016 gRPC authors. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# Worker and worker service implementation 18 19this_dir = File.expand_path(File.dirname(__FILE__)) 20lib_dir = File.join(File.dirname(this_dir), 'lib') 21$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) 22$LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) 23 24require 'grpc' 25require 'qps-common' 26require 'src/proto/grpc/testing/messages_pb' 27require 'src/proto/grpc/testing/benchmark_service_services_pb' 28require 'src/proto/grpc/testing/stats_pb' 29 30class BenchmarkServiceImpl < Grpc::Testing::BenchmarkService::Service 31 def unary_call(req, _call) 32 sr = Grpc::Testing::SimpleResponse 33 pl = Grpc::Testing::Payload 34 sr.new(payload: pl.new(body: nulls(req.response_size))) 35 end 36 def streaming_call(reqs) 37 PingPongEnumerator.new(reqs).each_item 38 end 39end 40 41class BenchmarkServer 42 def initialize(config, port) 43 if config.security_params 44 certs = load_test_certs 45 cred = GRPC::Core::ServerCredentials.new( 46 nil, [{private_key: certs[1], cert_chain: certs[2]}], false) 47 else 48 cred = :this_port_is_insecure 49 end 50 # Make sure server can handle the large number of calls in benchmarks 51 # TODO: @apolcyn, if scenario config increases total outstanding 52 # calls then will need to increase the pool size too 53 @server = GRPC::RpcServer.new(pool_size: 1024, max_waiting_requests: 1024) 54 @port = @server.add_http2_port("0.0.0.0:" + port.to_s, cred) 55 @server.handle(BenchmarkServiceImpl.new) 56 @start_time = Time.now 57 t = Thread.new { 58 @server.run 59 } 60 t.abort_on_exception 61 end 62 def mark(reset) 63 s = Grpc::Testing::ServerStats.new(time_elapsed: 64 (Time.now-@start_time).to_f) 65 @start_time = Time.now if reset 66 s 67 end 68 def get_port 69 @port 70 end 71 def stop 72 @server.stop 73 end 74end 75