server.rb 2.4 KB

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