proxy-worker.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env ruby
  2. # Copyright 2017, Google Inc.
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. # Proxy of worker service implementation for running a PHP client
  31. this_dir = File.expand_path(File.dirname(__FILE__))
  32. lib_dir = File.join(File.dirname(this_dir), 'lib')
  33. $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
  34. $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir)
  35. require 'grpc'
  36. require 'optparse'
  37. require 'histogram'
  38. require 'etc'
  39. require 'facter'
  40. require 'qps-common'
  41. require 'src/proto/grpc/testing/services_services_pb'
  42. require 'src/proto/grpc/testing/proxy-service_services_pb'
  43. class ProxyBenchmarkClientServiceImpl < Grpc::Testing::ProxyClientService::Service
  44. def initialize(port)
  45. @mytarget = "localhost:" + port.to_s
  46. end
  47. def setup(config)
  48. @config = config
  49. @histres = config.histogram_params.resolution
  50. @histmax = config.histogram_params.max_possible
  51. @histogram = Histogram.new(@histres, @histmax)
  52. @start_time = Time.now
  53. # TODO(vjpai): Support multiple client channels by spawning off a PHP client per channel
  54. command = "php " + File.expand_path(File.dirname(__FILE__)) + "/../../php/tests/qps/client.php " + @mytarget
  55. puts "Starting command: " + command
  56. @php_pid = spawn(command)
  57. end
  58. def stop
  59. Process.kill("TERM", @php_pid)
  60. Process.wait(@php_pid)
  61. end
  62. def get_config(_args, _call)
  63. puts "Answering get_config"
  64. @config
  65. end
  66. def report_time(call)
  67. puts "Starting a time reporting stream"
  68. call.each_remote_read do |lat|
  69. @histogram.add((lat.latency)*1e9)
  70. end
  71. Grpc::Testing::Void.new
  72. end
  73. def mark(reset)
  74. lat = Grpc::Testing::HistogramData.new(
  75. bucket: @histogram.contents,
  76. min_seen: @histogram.minimum,
  77. max_seen: @histogram.maximum,
  78. sum: @histogram.sum,
  79. sum_of_squares: @histogram.sum_of_squares,
  80. count: @histogram.count
  81. )
  82. elapsed = Time.now-@start_time
  83. if reset
  84. @start_time = Time.now
  85. @histogram = Histogram.new(@histres, @histmax)
  86. end
  87. Grpc::Testing::ClientStats.new(latencies: lat, time_elapsed: elapsed)
  88. end
  89. end
  90. class ProxyWorkerServiceImpl < Grpc::Testing::WorkerService::Service
  91. def cpu_cores
  92. Facter.value('processors')['count']
  93. end
  94. # Leave run_server unimplemented since this proxies for a client only.
  95. # If the driver tries to use this as a server, it will get an unimplemented
  96. # status return value.
  97. def run_client(reqs)
  98. q = EnumeratorQueue.new(self)
  99. Thread.new {
  100. reqs.each do |req|
  101. case req.argtype.to_s
  102. when 'setup'
  103. @bmc.setup(req.setup)
  104. q.push(Grpc::Testing::ClientStatus.new(stats: @bmc.mark(false)))
  105. when 'mark'
  106. q.push(Grpc::Testing::ClientStatus.new(stats:
  107. @bmc.mark(req.mark.reset)))
  108. end
  109. end
  110. @bmc.stop
  111. q.push(self)
  112. }
  113. q.each_item
  114. end
  115. def core_count(_args, _call)
  116. Grpc::Testing::CoreResponse.new(cores: cpu_cores)
  117. end
  118. def quit_worker(_args, _call)
  119. Thread.new {
  120. sleep 3
  121. @server.stop
  122. }
  123. Grpc::Testing::Void.new
  124. end
  125. def initialize(s, bmc)
  126. @server = s
  127. @bmc = bmc
  128. end
  129. end
  130. def proxymain
  131. options = {
  132. 'driver_port' => 0
  133. }
  134. OptionParser.new do |opts|
  135. opts.banner = 'Usage: [--driver_port <port>]'
  136. opts.on('--driver_port PORT', '<port>') do |v|
  137. options['driver_port'] = v
  138. end
  139. end.parse!
  140. # Configure any errors with client or server child threads to surface
  141. Thread.abort_on_exception = true
  142. s = GRPC::RpcServer.new
  143. port = s.add_http2_port("0.0.0.0:" + options['driver_port'].to_s,
  144. :this_port_is_insecure)
  145. bmc = ProxyBenchmarkClientServiceImpl.new(port)
  146. s.handle(bmc)
  147. s.handle(ProxyWorkerServiceImpl.new(s, bmc))
  148. s.run
  149. end
  150. proxymain