blob: b24a90adacdba6b93c9d68c8703d71e8a0bafe44 [file] [log] [blame]
Craig Tiller26598a32015-03-02 16:16:00 -08001/*
2 *
3 * Copyright 2015, 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 */
33
34#ifndef TEST_QPS_CLIENT_H
35#define TEST_QPS_CLIENT_H
36
yang-g9e2f90c2015-08-21 15:35:03 -070037#include <condition_variable>
38#include <mutex>
39
Craig Tiller88568752015-03-04 10:50:43 -080040#include "test/cpp/qps/histogram.h"
Vijay Pai372fd872015-06-08 13:30:08 -070041#include "test/cpp/qps/interarrival.h"
Craig Tiller88568752015-03-04 10:50:43 -080042#include "test/cpp/qps/timer.h"
yang-g9e2f90c2015-08-21 15:35:03 -070043#include "test/cpp/util/create_test_channel.h"
vjpai780a7f22015-11-04 00:19:09 -080044#include "test/proto/benchmarks/payloads.grpc.pb.h"
vjpaid08a7382015-11-02 16:45:08 -080045#include "test/proto/benchmarks/services.grpc.pb.h"
Craig Tillere38ec212015-03-04 11:19:15 -080046
Craig Tiller26598a32015-03-02 16:16:00 -080047namespace grpc {
Vijay Pai372fd872015-06-08 13:30:08 -070048
49#if defined(__APPLE__)
50// Specialize Timepoint for high res clock as we need that
51template <>
52class TimePoint<std::chrono::high_resolution_clock::time_point> {
53 public:
54 TimePoint(const std::chrono::high_resolution_clock::time_point& time) {
55 TimepointHR2Timespec(time, &time_);
56 }
57 gpr_timespec raw_time() const { return time_; }
58
59 private:
60 gpr_timespec time_;
61};
62#endif
63
Craig Tiller26598a32015-03-02 16:16:00 -080064namespace testing {
65
Vijay Pai372fd872015-06-08 13:30:08 -070066typedef std::chrono::high_resolution_clock grpc_time_source;
67typedef std::chrono::time_point<grpc_time_source> grpc_time;
68
Craig Tiller26598a32015-03-02 16:16:00 -080069class Client {
70 public:
Vijay Pai372fd872015-06-08 13:30:08 -070071 explicit Client(const ClientConfig& config)
Vijay Pai90e73692015-08-05 19:15:36 -070072 : channels_(config.client_channels()),
73 timer_(new Timer),
74 interarrival_timer_() {
Craig Tiller88568752015-03-04 10:50:43 -080075 for (int i = 0; i < config.client_channels(); i++) {
Vijay Paieed63fa2015-08-05 23:08:34 +000076 channels_[i].init(config.server_targets(i % config.server_targets_size()),
Vijay Pai90e73692015-08-05 19:15:36 -070077 config);
Craig Tiller88568752015-03-04 10:50:43 -080078 }
vjpai780a7f22015-11-04 00:19:09 -080079 if (config.payload_config().has_bytebuf_params()) {
Vijay Paice846702015-11-04 00:30:12 -080080 GPR_ASSERT(false); // not yet implemented
vjpai780a7f22015-11-04 00:19:09 -080081 } else if (config.payload_config().has_simple_params()) {
82 request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
Vijay Paice846702015-11-04 00:30:12 -080083 request_.set_response_size(
84 config.payload_config().simple_params().resp_size());
85 request_.mutable_payload()->set_type(
86 grpc::testing::PayloadType::COMPRESSABLE);
vjpai780a7f22015-11-04 00:19:09 -080087 int size = config.payload_config().simple_params().req_size();
88 std::unique_ptr<char[]> body(new char[size]);
89 request_.mutable_payload()->set_body(body.get(), size);
90 } else if (config.payload_config().has_complex_params()) {
Vijay Paice846702015-11-04 00:30:12 -080091 GPR_ASSERT(false); // not yet implemented
vjpai780a7f22015-11-04 00:19:09 -080092 } else {
vjpaifba20c92015-11-04 14:49:17 -080093 // default should be simple proto without payloads
94 request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
95 request_.set_response_size(0);
96 request_.mutable_payload()->set_type(
97 grpc::testing::PayloadType::COMPRESSABLE);
vjpai780a7f22015-11-04 00:19:09 -080098 }
Craig Tiller88568752015-03-04 10:50:43 -080099 }
Craig Tiller26598a32015-03-02 16:16:00 -0800100 virtual ~Client() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -0800101
vjpai119c1032015-10-29 01:21:04 -0700102 ClientStats Mark(bool reset) {
Craig Tiller88568752015-03-04 10:50:43 -0800103 Histogram latencies;
vjpai119c1032015-10-29 01:21:04 -0700104 Timer::Result timer_result;
Craig Tiller88568752015-03-04 10:50:43 -0800105
vjpai119c1032015-10-29 01:21:04 -0700106 // avoid std::vector for old compilers that expect a copy constructor
107 if (reset) {
108 Histogram* to_merge = new Histogram[threads_.size()];
109 for (size_t i = 0; i < threads_.size(); i++) {
Vijay Paice846702015-11-04 00:30:12 -0800110 threads_[i]->BeginSwap(&to_merge[i]);
vjpai119c1032015-10-29 01:21:04 -0700111 }
112 std::unique_ptr<Timer> timer(new Timer);
113 timer_.swap(timer);
114 for (size_t i = 0; i < threads_.size(); i++) {
Vijay Paice846702015-11-04 00:30:12 -0800115 threads_[i]->EndSwap();
116 latencies.Merge(to_merge[i]);
vjpai119c1032015-10-29 01:21:04 -0700117 }
118 delete[] to_merge;
119 timer_result = timer->Mark();
120 } else {
121 // merge snapshots of each thread histogram
122 for (size_t i = 0; i < threads_.size(); i++) {
Vijay Paice846702015-11-04 00:30:12 -0800123 threads_[i]->MergeStatsInto(&latencies);
vjpai119c1032015-10-29 01:21:04 -0700124 }
125 timer_result = timer_->Mark();
126 }
Craig Tiller88568752015-03-04 10:50:43 -0800127
128 ClientStats stats;
129 latencies.FillProto(stats.mutable_latencies());
130 stats.set_time_elapsed(timer_result.wall);
131 stats.set_time_system(timer_result.system);
132 stats.set_time_user(timer_result.user);
133 return stats;
134 }
135
136 protected:
137 SimpleRequest request_;
Vijay Pai372fd872015-06-08 13:30:08 -0700138 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -0800139
140 class ClientChannelInfo {
141 public:
Vijay Paieed63fa2015-08-05 23:08:34 +0000142 ClientChannelInfo() {}
vjpaib1db8692015-08-11 22:41:02 -0700143 ClientChannelInfo(const ClientChannelInfo& i) {
Vijay Pai90e73692015-08-05 19:15:36 -0700144 // The copy constructor is to satisfy old compilers
145 // that need it for using std::vector . It is only ever
146 // used for empty entries
Vijay Paieed63fa2015-08-05 23:08:34 +0000147 GPR_ASSERT(!i.channel_ && !i.stub_);
148 }
149 void init(const grpc::string& target, const ClientConfig& config) {
Vijay Pai90e73692015-08-05 19:15:36 -0700150 // We have to use a 2-phase init like this with a default
151 // constructor followed by an initializer function to make
152 // old compilers happy with using this in std::vector
Vijay Paice846702015-11-04 00:30:12 -0800153 channel_ = CreateTestChannel(
154 target, config.security_params().server_host_override(),
155 config.has_security_params(),
156 !config.security_params().use_test_ca());
vjpai119c1032015-10-29 01:21:04 -0700157 stub_ = BenchmarkService::NewStub(channel_);
Vijay Paieed63fa2015-08-05 23:08:34 +0000158 }
yang-g8c2be9f2015-08-19 16:28:09 -0700159 Channel* get_channel() { return channel_.get(); }
vjpai119c1032015-10-29 01:21:04 -0700160 BenchmarkService::Stub* get_stub() { return stub_.get(); }
Vijay Pai90e73692015-08-05 19:15:36 -0700161
Craig Tiller88568752015-03-04 10:50:43 -0800162 private:
yang-g8c2be9f2015-08-19 16:28:09 -0700163 std::shared_ptr<Channel> channel_;
vjpai119c1032015-10-29 01:21:04 -0700164 std::unique_ptr<BenchmarkService::Stub> stub_;
Craig Tiller88568752015-03-04 10:50:43 -0800165 };
166 std::vector<ClientChannelInfo> channels_;
167
168 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800169 for (size_t i = 0; i < num_threads; i++) {
170 threads_.emplace_back(new Thread(this, i));
171 }
Craig Tiller88568752015-03-04 10:50:43 -0800172 }
173
Craig Tillera182bf12015-03-04 13:54:39 -0800174 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800175
Craig Tiller8a5a6662015-04-09 11:31:28 -0700176 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700177
Vijay Pai372fd872015-06-08 13:30:08 -0700178 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
179 // Set up the load distribution based on the number of threads
vjpai754751e2015-10-28 09:16:22 -0700180 const auto& load = config.load_params();
181
182 std::unique_ptr<RandomDist> random_dist;
vjpaifba20c92015-11-04 14:49:17 -0800183 switch (load.load_case()) {
Craig Tiller3c53bb22015-11-10 14:24:36 +0000184 case LoadParams::kClosedLoop:
185 // Closed-loop doesn't use random dist at all
186 break;
187 case LoadParams::kPoisson:
188 random_dist.reset(
189 new ExpDist(load.poisson().offered_load() / num_threads));
190 break;
191 case LoadParams::kUniform:
192 random_dist.reset(
193 new UniformDist(load.uniform().interarrival_lo() * num_threads,
194 load.uniform().interarrival_hi() * num_threads));
195 break;
196 case LoadParams::kDeterm:
197 random_dist.reset(
198 new DetDist(num_threads / load.determ().offered_load()));
199 break;
200 case LoadParams::kPareto:
201 random_dist.reset(
202 new ParetoDist(load.pareto().interarrival_base() * num_threads,
203 load.pareto().alpha()));
204 break;
205 default:
206 GPR_ASSERT(false);
vjpai754751e2015-10-28 09:16:22 -0700207 }
208
209 // Set closed_loop_ based on whether or not random_dist is set
210 if (!random_dist) {
Vijay Pai372fd872015-06-08 13:30:08 -0700211 closed_loop_ = true;
212 } else {
213 closed_loop_ = false;
vjpai754751e2015-10-28 09:16:22 -0700214 // set up interarrival timer according to random dist
Vijay Pai372fd872015-06-08 13:30:08 -0700215 interarrival_timer_.init(*random_dist, num_threads);
216 for (size_t i = 0; i < num_threads; i++) {
217 next_time_.push_back(
218 grpc_time_source::now() +
219 std::chrono::duration_cast<grpc_time_source::duration>(
220 interarrival_timer_(i)));
221 }
222 }
223 }
224
225 bool NextIssueTime(int thread_idx, grpc_time* time_delay) {
226 if (closed_loop_) {
227 return false;
228 } else {
229 *time_delay = next_time_[thread_idx];
230 next_time_[thread_idx] +=
231 std::chrono::duration_cast<grpc_time_source::duration>(
232 interarrival_timer_(thread_idx));
233 return true;
234 }
235 }
236
Craig Tiller88568752015-03-04 10:50:43 -0800237 private:
238 class Thread {
239 public:
240 Thread(Client* client, size_t idx)
Vijay Paiab1dba72015-07-31 09:09:09 -0700241 : done_(false),
vjpai119c1032015-10-29 01:21:04 -0700242 new_stats_(nullptr),
Vijay Paiab1dba72015-07-31 09:09:09 -0700243 client_(client),
244 idx_(idx),
245 impl_(&Thread::ThreadFunc, this) {}
Craig Tiller88568752015-03-04 10:50:43 -0800246
247 ~Thread() {
248 {
249 std::lock_guard<std::mutex> g(mu_);
250 done_ = true;
251 }
252 impl_.join();
253 }
254
255 void BeginSwap(Histogram* n) {
256 std::lock_guard<std::mutex> g(mu_);
vjpai119c1032015-10-29 01:21:04 -0700257 new_stats_ = n;
Craig Tiller88568752015-03-04 10:50:43 -0800258 }
259
260 void EndSwap() {
261 std::unique_lock<std::mutex> g(mu_);
vjpai119c1032015-10-29 01:21:04 -0700262 while (new_stats_ != nullptr) {
Vijay Pai784005b2015-07-31 10:53:42 -0700263 cv_.wait(g);
264 };
Craig Tiller88568752015-03-04 10:50:43 -0800265 }
266
vjpai119c1032015-10-29 01:21:04 -0700267 void MergeStatsInto(Histogram* hist) {
268 std::unique_lock<std::mutex> g(mu_);
269 hist->Merge(histogram_);
270 }
271
Craig Tiller88568752015-03-04 10:50:43 -0800272 private:
273 Thread(const Thread&);
274 Thread& operator=(const Thread&);
275
vjpaia9e08302015-07-31 07:55:06 -0700276 void ThreadFunc() {
277 for (;;) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700278 // run the loop body
vjpaib1db8692015-08-11 22:41:02 -0700279 const bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_);
Vijay Paiab1dba72015-07-31 09:09:09 -0700280 // lock, see if we're done
281 std::lock_guard<std::mutex> g(mu_);
282 if (!thread_still_ok) {
283 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
284 done_ = true;
285 }
286 if (done_) {
287 return;
288 }
vjpai119c1032015-10-29 01:21:04 -0700289 // check if we're resetting stats, swap out the histogram if so
290 if (new_stats_) {
291 new_stats_->Swap(&histogram_);
292 new_stats_ = nullptr;
Vijay Paiab1dba72015-07-31 09:09:09 -0700293 cv_.notify_one();
294 }
vjpaia9e08302015-07-31 07:55:06 -0700295 }
296 }
297
vjpai119c1032015-10-29 01:21:04 -0700298 BenchmarkService::Stub* stub_;
Craig Tiller88568752015-03-04 10:50:43 -0800299 ClientConfig config_;
300 std::mutex mu_;
301 std::condition_variable cv_;
302 bool done_;
vjpai119c1032015-10-29 01:21:04 -0700303 Histogram* new_stats_;
Craig Tiller88568752015-03-04 10:50:43 -0800304 Histogram histogram_;
Vijay Paiab1dba72015-07-31 09:09:09 -0700305 Client* client_;
vjpaia9e08302015-07-31 07:55:06 -0700306 size_t idx_;
Craig Tiller88568752015-03-04 10:50:43 -0800307 std::thread impl_;
308 };
309
310 std::vector<std::unique_ptr<Thread>> threads_;
311 std::unique_ptr<Timer> timer_;
Vijay Pai372fd872015-06-08 13:30:08 -0700312
313 InterarrivalTimer interarrival_timer_;
314 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800315};
316
Craig Tiller5c8737d2015-05-21 11:42:17 -0700317std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
318std::unique_ptr<Client> CreateSynchronousStreamingClient(
319 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700320std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
321std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800322
323} // namespace testing
324} // namespace grpc
325
326#endif