client.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_pb'
  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. @child_threads = []
  85. (0..config.client_channels-1).each do |chan|
  86. gtbss = Grpc::Testing::BenchmarkService::Stub
  87. st = config.server_targets
  88. stub = gtbss.new(st[chan % st.length], cred, **opts)
  89. (0..config.outstanding_rpcs_per_channel-1).each do |r|
  90. @child_threads << Thread.new {
  91. case config.load_params.load.to_s
  92. when 'closed_loop'
  93. waiter = nil
  94. when 'poisson'
  95. waiter = Poisson.new(config.load_params.poisson.offered_load /
  96. (config.client_channels *
  97. config.outstanding_rpcs_per_channel))
  98. end
  99. case config.rpc_type
  100. when :UNARY
  101. unary_ping_ponger(req,stub,config,waiter)
  102. when :STREAMING
  103. streaming_ping_ponger(req,stub,config,waiter)
  104. end
  105. }
  106. end
  107. end
  108. end
  109. def wait_to_issue(waiter)
  110. if waiter
  111. delay = waiter.advance-Time.now
  112. sleep delay if delay > 0
  113. end
  114. end
  115. def unary_ping_ponger(req, stub, config,waiter)
  116. while !@done
  117. wait_to_issue(waiter)
  118. start = Time.now
  119. resp = stub.unary_call(req)
  120. @histogram.add((Time.now-start)*1e9)
  121. end
  122. end
  123. def streaming_ping_ponger(req, stub, config, waiter)
  124. q = EnumeratorQueue.new(self)
  125. resp = stub.streaming_call(q.each_item)
  126. start = Time.now
  127. q.push(req)
  128. pushed_sentinal = false
  129. resp.each do |r|
  130. @histogram.add((Time.now-start)*1e9)
  131. if !@done
  132. wait_to_issue(waiter)
  133. start = Time.now
  134. q.push(req)
  135. else
  136. q.push(self) unless pushed_sentinal
  137. # Continue polling on the responses to consume and release resources
  138. pushed_sentinal = true
  139. end
  140. end
  141. end
  142. def mark(reset)
  143. lat = Grpc::Testing::HistogramData.new(
  144. bucket: @histogram.contents,
  145. min_seen: @histogram.minimum,
  146. max_seen: @histogram.maximum,
  147. sum: @histogram.sum,
  148. sum_of_squares: @histogram.sum_of_squares,
  149. count: @histogram.count
  150. )
  151. elapsed = Time.now-@start_time
  152. if reset
  153. @start_time = Time.now
  154. @histogram = Histogram.new(@histres, @histmax)
  155. end
  156. Grpc::Testing::ClientStats.new(latencies: lat, time_elapsed: elapsed)
  157. end
  158. def shutdown
  159. @done = true
  160. @child_threads.each do |thread|
  161. thread.join
  162. end
  163. end
  164. end