client.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. # Worker and worker service implementation
  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 'histogram'
  37. require 'src/proto/grpc/testing/services_services'
  38. class Poisson
  39. def interarrival
  40. @lambda_recip * (-Math.log(1.0-rand))
  41. end
  42. def advance
  43. t = @next_time
  44. @next_time += interarrival
  45. t
  46. end
  47. def initialize(lambda)
  48. @lambda_recip = 1.0/lambda
  49. @next_time = Time.now + interarrival
  50. end
  51. end
  52. class BenchmarkClient
  53. def initialize(config)
  54. opts = {}
  55. if config.security_params
  56. if config.security_params.use_test_ca
  57. certs = load_test_certs
  58. cred = GRPC::Core::ChannelCredentials.new(certs[0])
  59. else
  60. cred = GRPC::Core::ChannelCredentials.new()
  61. end
  62. if config.security_params.server_host_override
  63. channel_args = {}
  64. channel_args[GRPC::Core::Channel::SSL_TARGET] =
  65. config.security_params.server_host_override
  66. opts[:channel_args] = channel_args
  67. end
  68. else
  69. cred = :this_channel_is_insecure
  70. end
  71. @histres = config.histogram_params.resolution
  72. @histmax = config.histogram_params.max_possible
  73. @start_time = Time.now
  74. @histogram = Histogram.new(@histres, @histmax)
  75. @done = false
  76. gtsr = Grpc::Testing::SimpleRequest
  77. gtpt = Grpc::Testing::PayloadType
  78. gtp = Grpc::Testing::Payload
  79. simple_params = config.payload_config.simple_params
  80. req = gtsr.new(response_type: gtpt::COMPRESSABLE,
  81. response_size: simple_params.resp_size,
  82. payload: gtp.new(type: gtpt::COMPRESSABLE,
  83. body: nulls(simple_params.req_size)))
  84. (0..config.client_channels-1).each do |chan|
  85. gtbss = Grpc::Testing::BenchmarkService::Stub
  86. st = config.server_targets
  87. stub = gtbss.new(st[chan % st.length], cred, **opts)
  88. (0..config.outstanding_rpcs_per_channel-1).each do |r|
  89. Thread.new {
  90. case config.load_params.load.to_s
  91. when 'closed_loop'
  92. waiter = nil
  93. when 'poisson'
  94. waiter = Poisson.new(config.load_params.poisson.offered_load /
  95. (config.client_channels *
  96. config.outstanding_rpcs_per_channel))
  97. end
  98. case config.rpc_type
  99. when :UNARY
  100. unary_ping_ponger(req,stub,config,waiter)
  101. when :STREAMING
  102. streaming_ping_ponger(req,stub,config,waiter)
  103. end
  104. }
  105. end
  106. end
  107. end
  108. def wait_to_issue(waiter)
  109. if waiter
  110. delay = waiter.advance-Time.now
  111. sleep delay if delay > 0
  112. end
  113. end
  114. def unary_ping_ponger(req, stub, config,waiter)
  115. while !@done
  116. wait_to_issue(waiter)
  117. start = Time.now
  118. resp = stub.unary_call(req)
  119. @histogram.add((Time.now-start)*1e9)
  120. end
  121. end
  122. def streaming_ping_ponger(req, stub, config, waiter)
  123. q = EnumeratorQueue.new(self)
  124. resp = stub.streaming_call(q.each_item)
  125. start = Time.now
  126. q.push(req)
  127. resp.each do |r|
  128. @histogram.add((Time.now-start)*1e9)
  129. if !@done
  130. wait_to_issue(waiter)
  131. start = Time.now
  132. q.push(req)
  133. else
  134. q.push(self)
  135. break
  136. end
  137. end
  138. end
  139. def mark(reset)
  140. lat = Grpc::Testing::HistogramData.new(
  141. bucket: @histogram.contents,
  142. min_seen: @histogram.minimum,
  143. max_seen: @histogram.maximum,
  144. sum: @histogram.sum,
  145. sum_of_squares: @histogram.sum_of_squares,
  146. count: @histogram.count
  147. )
  148. elapsed = Time.now-@start_time
  149. if reset
  150. @start_time = Time.now
  151. @histogram = Histogram.new(@histres, @histmax)
  152. end
  153. Grpc::Testing::ClientStats.new(latencies: lat, time_elapsed: elapsed)
  154. end
  155. def shutdown
  156. @done = true
  157. end
  158. end