stress_client.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env ruby
  2. # Copyright 2016, 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. require 'optparse'
  31. require 'thread'
  32. require_relative '../pb/test/client'
  33. require_relative './metrics_server'
  34. require_relative '../lib/grpc'
  35. class QpsGauge < Gauge
  36. @query_count
  37. @query_mutex
  38. @start_time
  39. def initialize
  40. @query_count = 0
  41. @query_mutex = Mutex.new
  42. @start_time = Time.now
  43. end
  44. def increment_queries
  45. @query_mutex.synchronize { @query_count += 1}
  46. end
  47. def get_name
  48. 'qps'
  49. end
  50. def get_type
  51. 'long'
  52. end
  53. def get_value
  54. (@query_mutex.synchronize { @query_count / (Time.now - @start_time) }).to_i
  55. end
  56. end
  57. def start_metrics_server(port)
  58. host = "0.0.0.0:#{port}"
  59. server = GRPC::RpcServer.new
  60. server.add_http2_port(host, :this_port_is_insecure)
  61. service = MetricsServiceImpl.new
  62. server.handle(service)
  63. server_thread = Thread.new { server.run_till_terminated }
  64. [server, service, server_thread]
  65. end
  66. StressArgs = Struct.new(:server_addresses, :test_cases, :duration,
  67. :channels_per_server, :concurrent_calls, :metrics_port)
  68. def start(stress_args)
  69. running = true
  70. threads = []
  71. qps_gauge = QpsGauge.new
  72. metrics_server, metrics_service, metrics_thread =
  73. start_metrics_server(stress_args.metrics_port)
  74. metrics_service.register_gauge(qps_gauge)
  75. stress_args.server_addresses.each do |address|
  76. stress_args.channels_per_server.times do
  77. client_args = Args.new
  78. client_args.host, client_args.port = address.split(':')
  79. client_args.secure = false
  80. client_args.test_case = ''
  81. stub = create_stub(client_args)
  82. named_tests = NamedTests.new(stub, client_args)
  83. stress_args.concurrent_calls.times do
  84. threads << Thread.new do
  85. while running
  86. named_tests.method(stress_args.test_cases.sample).call
  87. qps_gauge.increment_queries
  88. end
  89. end
  90. end
  91. end
  92. end
  93. if stress_args.duration >= 0
  94. sleep stress_args.duration
  95. running = false
  96. metrics_server.stop
  97. p "QPS: #{qps_gauge.get_value}"
  98. threads.each { |thd| thd.join; }
  99. end
  100. metrics_thread.join
  101. end
  102. def parse_stress_args
  103. stress_args = StressArgs.new
  104. stress_args.server_addresses = ['localhost:8080']
  105. stress_args.test_cases = []
  106. stress_args.duration = -1
  107. stress_args.channels_per_server = 1
  108. stress_args.concurrent_calls = 1
  109. stress_args.metrics_port = '8081'
  110. OptionParser.new do |opts|
  111. opts.on('--server_addresses [LIST]', Array) do |addrs|
  112. stress_args.server_addresses = addrs
  113. end
  114. opts.on('--test_cases cases', Array) do |cases|
  115. stress_args.test_cases = (cases.map do |item|
  116. split = item.split(':')
  117. [split[0]] * split[1].to_i
  118. end).reduce([], :+)
  119. end
  120. opts.on('--test_duration_secs [INT]', OptionParser::DecimalInteger) do |time|
  121. stress_args.duration = time
  122. end
  123. opts.on('--num_channels_per_server [INT]', OptionParser::DecimalInteger) do |channels|
  124. stress_args.channels_per_server = channels
  125. end
  126. opts.on('--num_stubs_per_channel [INT]', OptionParser::DecimalInteger) do |stubs|
  127. stress_args.concurrent_calls = stubs
  128. end
  129. opts.on('--metrics_port [port]') do |port|
  130. stress_args.metrics_port = port
  131. end
  132. end.parse!
  133. stress_args
  134. end
  135. def main
  136. opts = parse_stress_args
  137. start(opts)
  138. end
  139. if __FILE__ == $0
  140. main
  141. end