worker.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 'optparse'
  22. require 'histogram'
  23. require 'etc'
  24. require 'facter'
  25. require 'client'
  26. require 'qps-common'
  27. require 'server'
  28. require 'src/proto/grpc/testing/services_services_pb'
  29. class WorkerServiceImpl < Grpc::Testing::WorkerService::Service
  30. def cpu_cores
  31. Facter.value('processors')['count']
  32. end
  33. def run_server(reqs)
  34. q = EnumeratorQueue.new(self)
  35. Thread.new {
  36. bms = ''
  37. gtss = Grpc::Testing::ServerStatus
  38. reqs.each do |req|
  39. case req.argtype.to_s
  40. when 'setup'
  41. bms = BenchmarkServer.new(req.setup, @server_port)
  42. q.push(gtss.new(stats: bms.mark(false), port: bms.get_port))
  43. when 'mark'
  44. q.push(gtss.new(stats: bms.mark(req.mark.reset), cores: cpu_cores))
  45. end
  46. end
  47. bms.stop
  48. q.push(self)
  49. }
  50. q.each_item
  51. end
  52. def run_client(reqs)
  53. q = EnumeratorQueue.new(self)
  54. Thread.new {
  55. client = ''
  56. reqs.each do |req|
  57. case req.argtype.to_s
  58. when 'setup'
  59. client = BenchmarkClient.new(req.setup)
  60. q.push(Grpc::Testing::ClientStatus.new(stats: client.mark(false)))
  61. when 'mark'
  62. q.push(Grpc::Testing::ClientStatus.new(stats:
  63. client.mark(req.mark.reset)))
  64. end
  65. end
  66. client.shutdown
  67. q.push(self)
  68. }
  69. q.each_item
  70. end
  71. def core_count(_args, _call)
  72. Grpc::Testing::CoreResponse.new(cores: cpu_cores)
  73. end
  74. def quit_worker(_args, _call)
  75. Thread.new {
  76. sleep 3
  77. @server.stop
  78. }
  79. Grpc::Testing::Void.new
  80. end
  81. def initialize(s, sp)
  82. @server = s
  83. @server_port = sp
  84. end
  85. end
  86. def main
  87. options = {
  88. 'driver_port' => 0,
  89. 'server_port' => 0
  90. }
  91. OptionParser.new do |opts|
  92. opts.banner = 'Usage: [--driver_port <port>] [--server_port <port>]'
  93. opts.on('--driver_port PORT', '<port>') do |v|
  94. options['driver_port'] = v
  95. end
  96. opts.on('--server_port PORT', '<port>') do |v|
  97. options['server_port'] = v
  98. end
  99. end.parse!
  100. # Configure any errors with client or server child threads to surface
  101. Thread.abort_on_exception = true
  102. s = GRPC::RpcServer.new
  103. s.add_http2_port("0.0.0.0:" + options['driver_port'].to_s,
  104. :this_port_is_insecure)
  105. s.handle(WorkerServiceImpl.new(s, options['server_port'].to_i))
  106. s.run
  107. end
  108. main