vjpai | 75291c9 | 2016-03-21 07:59:26 -0700 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | # Copyright 2016 gRPC authors. |
vjpai | 75291c9 | 2016-03-21 07:59:26 -0700 | [diff] [blame] | 4 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
vjpai | 75291c9 | 2016-03-21 07:59:26 -0700 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
vjpai | 75291c9 | 2016-03-21 07:59:26 -0700 | [diff] [blame] | 10 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
vjpai | 75291c9 | 2016-03-21 07:59:26 -0700 | [diff] [blame] | 16 | |
| 17 | # Worker and worker service implementation |
| 18 | |
| 19 | this_dir = File.expand_path(File.dirname(__FILE__)) |
| 20 | lib_dir = File.join(File.dirname(this_dir), 'lib') |
| 21 | $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) |
| 22 | $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) |
| 23 | |
| 24 | require 'grpc' |
| 25 | require 'histogram' |
Ken Payson | 5a2c918 | 2016-07-26 17:15:08 -0700 | [diff] [blame] | 26 | require 'src/proto/grpc/testing/services_services_pb' |
vjpai | 75291c9 | 2016-03-21 07:59:26 -0700 | [diff] [blame] | 27 | |
| 28 | class Poisson |
| 29 | def interarrival |
| 30 | @lambda_recip * (-Math.log(1.0-rand)) |
| 31 | end |
| 32 | def advance |
| 33 | t = @next_time |
| 34 | @next_time += interarrival |
| 35 | t |
| 36 | end |
| 37 | def initialize(lambda) |
| 38 | @lambda_recip = 1.0/lambda |
| 39 | @next_time = Time.now + interarrival |
| 40 | end |
| 41 | end |
| 42 | |
vjpai | ad1c1cc | 2016-03-30 09:58:46 -0700 | [diff] [blame] | 43 | class BenchmarkClient |
| 44 | def initialize(config) |
vjpai | d9389a8 | 2016-03-30 13:10:20 -0700 | [diff] [blame] | 45 | opts = {} |
vjpai | ad1c1cc | 2016-03-30 09:58:46 -0700 | [diff] [blame] | 46 | if config.security_params |
| 47 | if config.security_params.use_test_ca |
| 48 | certs = load_test_certs |
vjpai | cf36f85 | 2016-03-30 12:35:34 -0700 | [diff] [blame] | 49 | cred = GRPC::Core::ChannelCredentials.new(certs[0]) |
vjpai | ad1c1cc | 2016-03-30 09:58:46 -0700 | [diff] [blame] | 50 | else |
vjpai | d9389a8 | 2016-03-30 13:10:20 -0700 | [diff] [blame] | 51 | cred = GRPC::Core::ChannelCredentials.new() |
vjpai | ad1c1cc | 2016-03-30 09:58:46 -0700 | [diff] [blame] | 52 | end |
| 53 | if config.security_params.server_host_override |
murgatroid99 | 2939e2f | 2016-05-23 13:41:32 -0700 | [diff] [blame] | 54 | channel_args = {} |
| 55 | channel_args[GRPC::Core::Channel::SSL_TARGET] = |
vjpai | d9389a8 | 2016-03-30 13:10:20 -0700 | [diff] [blame] | 56 | config.security_params.server_host_override |
murgatroid99 | 2939e2f | 2016-05-23 13:41:32 -0700 | [diff] [blame] | 57 | opts[:channel_args] = channel_args |
vjpai | ad1c1cc | 2016-03-30 09:58:46 -0700 | [diff] [blame] | 58 | end |
| 59 | else |
| 60 | cred = :this_channel_is_insecure |
| 61 | end |
| 62 | @histres = config.histogram_params.resolution |
| 63 | @histmax = config.histogram_params.max_possible |
| 64 | @start_time = Time.now |
| 65 | @histogram = Histogram.new(@histres, @histmax) |
| 66 | @done = false |
Vijay Pai | 5562807 | 2016-03-31 14:42:35 -0700 | [diff] [blame] | 67 | |
| 68 | gtsr = Grpc::Testing::SimpleRequest |
| 69 | gtpt = Grpc::Testing::PayloadType |
| 70 | gtp = Grpc::Testing::Payload |
| 71 | simple_params = config.payload_config.simple_params |
| 72 | req = gtsr.new(response_type: gtpt::COMPRESSABLE, |
| 73 | response_size: simple_params.resp_size, |
| 74 | payload: gtp.new(type: gtpt::COMPRESSABLE, |
| 75 | body: nulls(simple_params.req_size))) |
| 76 | |
Alexander Polcyn | 847f9ec | 2016-08-15 18:44:14 -0700 | [diff] [blame] | 77 | @child_threads = [] |
| 78 | |
Vijay Pai | 5562807 | 2016-03-31 14:42:35 -0700 | [diff] [blame] | 79 | (0..config.client_channels-1).each do |chan| |
| 80 | gtbss = Grpc::Testing::BenchmarkService::Stub |
| 81 | st = config.server_targets |
| 82 | stub = gtbss.new(st[chan % st.length], cred, **opts) |
| 83 | (0..config.outstanding_rpcs_per_channel-1).each do |r| |
Alexander Polcyn | 847f9ec | 2016-08-15 18:44:14 -0700 | [diff] [blame] | 84 | @child_threads << Thread.new { |
Vijay Pai | 5562807 | 2016-03-31 14:42:35 -0700 | [diff] [blame] | 85 | case config.load_params.load.to_s |
| 86 | when 'closed_loop' |
| 87 | waiter = nil |
| 88 | when 'poisson' |
| 89 | waiter = Poisson.new(config.load_params.poisson.offered_load / |
| 90 | (config.client_channels * |
| 91 | config.outstanding_rpcs_per_channel)) |
| 92 | end |
| 93 | case config.rpc_type |
| 94 | when :UNARY |
| 95 | unary_ping_ponger(req,stub,config,waiter) |
| 96 | when :STREAMING |
| 97 | streaming_ping_ponger(req,stub,config,waiter) |
| 98 | end |
| 99 | } |
| 100 | end |
vjpai | ad1c1cc | 2016-03-30 09:58:46 -0700 | [diff] [blame] | 101 | end |
| 102 | end |
| 103 | def wait_to_issue(waiter) |
| 104 | if waiter |
| 105 | delay = waiter.advance-Time.now |
| 106 | sleep delay if delay > 0 |
| 107 | end |
| 108 | end |
| 109 | def unary_ping_ponger(req, stub, config,waiter) |
| 110 | while !@done |
| 111 | wait_to_issue(waiter) |
| 112 | start = Time.now |
| 113 | resp = stub.unary_call(req) |
| 114 | @histogram.add((Time.now-start)*1e9) |
| 115 | end |
| 116 | end |
| 117 | def streaming_ping_ponger(req, stub, config, waiter) |
vjpai | 5286eee | 2016-03-30 12:07:19 -0700 | [diff] [blame] | 118 | q = EnumeratorQueue.new(self) |
| 119 | resp = stub.streaming_call(q.each_item) |
| 120 | start = Time.now |
| 121 | q.push(req) |
Alexander Polcyn | 3d48cf4 | 2016-11-23 09:50:22 -0800 | [diff] [blame] | 122 | pushed_sentinal = false |
vjpai | 5286eee | 2016-03-30 12:07:19 -0700 | [diff] [blame] | 123 | resp.each do |r| |
| 124 | @histogram.add((Time.now-start)*1e9) |
| 125 | if !@done |
| 126 | wait_to_issue(waiter) |
| 127 | start = Time.now |
| 128 | q.push(req) |
| 129 | else |
Alexander Polcyn | 3d48cf4 | 2016-11-23 09:50:22 -0800 | [diff] [blame] | 130 | q.push(self) unless pushed_sentinal |
| 131 | # Continue polling on the responses to consume and release resources |
| 132 | pushed_sentinal = true |
vjpai | 5286eee | 2016-03-30 12:07:19 -0700 | [diff] [blame] | 133 | end |
| 134 | end |
vjpai | ad1c1cc | 2016-03-30 09:58:46 -0700 | [diff] [blame] | 135 | end |
| 136 | def mark(reset) |
| 137 | lat = Grpc::Testing::HistogramData.new( |
| 138 | bucket: @histogram.contents, |
| 139 | min_seen: @histogram.minimum, |
| 140 | max_seen: @histogram.maximum, |
| 141 | sum: @histogram.sum, |
| 142 | sum_of_squares: @histogram.sum_of_squares, |
| 143 | count: @histogram.count |
| 144 | ) |
| 145 | elapsed = Time.now-@start_time |
| 146 | if reset |
| 147 | @start_time = Time.now |
| 148 | @histogram = Histogram.new(@histres, @histmax) |
| 149 | end |
| 150 | Grpc::Testing::ClientStats.new(latencies: lat, time_elapsed: elapsed) |
| 151 | end |
| 152 | def shutdown |
| 153 | @done = true |
Alexander Polcyn | 847f9ec | 2016-08-15 18:44:14 -0700 | [diff] [blame] | 154 | @child_threads.each do |thread| |
| 155 | thread.join |
| 156 | end |
vjpai | ad1c1cc | 2016-03-30 09:58:46 -0700 | [diff] [blame] | 157 | end |
| 158 | end |