blob: aa57b06c97c6e3a93a803943f6a68a20188a25a8 [file] [log] [blame]
vjpai75291c92016-03-21 07:59:26 -07001#!/usr/bin/env ruby
2
3# Copyright 2016, Google Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met:
9#
10# * Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# * Redistributions in binary form must reproduce the above
13# copyright notice, this list of conditions and the following disclaimer
14# in the documentation and/or other materials provided with the
15# distribution.
16# * Neither the name of Google Inc. nor the names of its
17# contributors may be used to endorse or promote products derived from
18# this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32# Worker and worker service implementation
33
34this_dir = File.expand_path(File.dirname(__FILE__))
35lib_dir = File.join(File.dirname(this_dir), 'lib')
36$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
37$LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir)
38
39require 'grpc'
40require 'histogram'
41require 'src/proto/grpc/testing/services_services'
42
43class Poisson
44 def interarrival
45 @lambda_recip * (-Math.log(1.0-rand))
46 end
47 def advance
48 t = @next_time
49 @next_time += interarrival
50 t
51 end
52 def initialize(lambda)
53 @lambda_recip = 1.0/lambda
54 @next_time = Time.now + interarrival
55 end
56end
57
vjpaiad1c1cc2016-03-30 09:58:46 -070058class BenchmarkClient
59 def initialize(config)
60 if config.security_params
61 if config.security_params.use_test_ca
62 certs = load_test_certs
63 cred = GRPC::Core::Credentials.new(certs[0])
64 else
65 p 'Unsupported to use non-test CA (TBD)'
66 exit
67 end
68 if config.security_params.server_host_override
69 p 'Unsupported to use severt host override (TBD)'
70 exit
71 end
72 else
73 cred = :this_channel_is_insecure
74 end
75 @histres = config.histogram_params.resolution
76 @histmax = config.histogram_params.max_possible
77 @start_time = Time.now
78 @histogram = Histogram.new(@histres, @histmax)
79 @done = false
80 (0..config.client_channels-1).each do |i|
81 Thread.new {
82 stub = ''
83 req = Grpc::Testing::SimpleRequest.new(response_type: Grpc::Testing::PayloadType::COMPRESSABLE,
84 response_size: config.payload_config.simple_params.resp_size,
85 payload: Grpc::Testing::Payload.new(type: Grpc::Testing::PayloadType::COMPRESSABLE,
86 body: nulls(config.payload_config.simple_params.req_size)))
87 case config.load_params.load.to_s
88 when 'closed_loop'
89 waiter = nil
90 when 'poisson'
91 waiter = Poisson.new(config.load_params.poisson.offered_load / config.client_channels)
92 end
93 stub = Grpc::Testing::BenchmarkService::Stub.new(config.server_targets[i % config.server_targets.length], cred)
94 case config.rpc_type
95 when :UNARY
96 unary_ping_ponger(req,stub,config,waiter)
97 when :STREAMING
98 streaming_ping_ponger(req,stub,config,waiter)
99 end
100 }
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)
118 end
119 def mark(reset)
120 lat = Grpc::Testing::HistogramData.new(
121 bucket: @histogram.contents,
122 min_seen: @histogram.minimum,
123 max_seen: @histogram.maximum,
124 sum: @histogram.sum,
125 sum_of_squares: @histogram.sum_of_squares,
126 count: @histogram.count
127 )
128 elapsed = Time.now-@start_time
129 if reset
130 @start_time = Time.now
131 @histogram = Histogram.new(@histres, @histmax)
132 end
133 Grpc::Testing::ClientStats.new(latencies: lat, time_elapsed: elapsed)
134 end
135 def shutdown
136 @done = true
137 end
138end