blob: 30a8030f513690f773043610b1e00a85f4dee198 [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
vjpaib6df94a2015-11-30 15:52:50 -080069namespace ClientRequestCreation {
70template <class RequestType>
71void CreateRequest(RequestType *req, const PayloadConfig&) {
72 // this template must be specialized
73 // fail with an assertion rather than a compile-time
74 // check since these only happen at the beginning anyway
75 GPR_ASSERT(false);
76}
77
78template <>
79void CreateRequest<SimpleRequest>(SimpleRequest *req, const PayloadConfig& payload_config) {
80 if (payload_config.has_bytebuf_params()) {
81 GPR_ASSERT(false); // not appropriate for this specialization
82 } else if (payload_config.has_simple_params()) {
83 req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
84 req->set_response_size(payload_config.simple_params().resp_size());
85 req->mutable_payload()->set_type(grpc::testing::PayloadType::COMPRESSABLE);
86 int size = payload_config.simple_params().req_size();
87 std::unique_ptr<char[]> body(new char[size]);
88 req->mutable_payload()->set_body(body.get(), size);
89 } else if (payload_config.has_complex_params()) {
90 GPR_ASSERT(false); // not appropriate for this specialization
91 } else {
92 // default should be simple proto without payloads
93 req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
94 req->set_response_size(0);
95 req->mutable_payload()->set_type(grpc::testing::PayloadType::COMPRESSABLE);
96 }
97}
98template <>
99void CreateRequest<ByteBuffer>(ByteBuffer *req, const PayloadConfig& payload_config) {
100 if (payload_config.has_bytebuf_params()) {
101 if (payload_config.req_size() > 0) {
102 std::unique_ptr<char> buf(new char[payload_config.req_size()]);
103 gpr_slice_from_copied_buffer(buf.get(), payload_config.req_size());
104 Slice slice(s, Slice::STEAL_REF);
105 std::unique_ptr<ByteBuffer> bbuf(new ByteBuffer(&slice, 1));
106 req->MoveFrom(bbuf.get());
107 } else {
108 GPR_ASSERT(false); // not appropriate for this specialization
109 }
110 }
111}
112}
113
114template <class StubType, class RequestType>
Craig Tiller26598a32015-03-02 16:16:00 -0800115class Client {
116 public:
vjpaib6df94a2015-11-30 15:52:50 -0800117 Client(const ClientConfig& config,
118 std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> create_stub)
Vijay Pai90e73692015-08-05 19:15:36 -0700119 : channels_(config.client_channels()),
vjpaib6df94a2015-11-30 15:52:50 -0800120 create_stub_(create_stub),
Vijay Pai90e73692015-08-05 19:15:36 -0700121 timer_(new Timer),
122 interarrival_timer_() {
Craig Tiller88568752015-03-04 10:50:43 -0800123 for (int i = 0; i < config.client_channels(); i++) {
Vijay Paieed63fa2015-08-05 23:08:34 +0000124 channels_[i].init(config.server_targets(i % config.server_targets_size()),
Vijay Pai90e73692015-08-05 19:15:36 -0700125 config);
Craig Tiller88568752015-03-04 10:50:43 -0800126 }
vjpaib6df94a2015-11-30 15:52:50 -0800127
128 ClientRequestCreation::CreateRequest<RequestType>(&request_, config.payload_config());
Craig Tiller88568752015-03-04 10:50:43 -0800129 }
Craig Tiller26598a32015-03-02 16:16:00 -0800130 virtual ~Client() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -0800131
vjpai119c1032015-10-29 01:21:04 -0700132 ClientStats Mark(bool reset) {
Craig Tiller88568752015-03-04 10:50:43 -0800133 Histogram latencies;
vjpai119c1032015-10-29 01:21:04 -0700134 Timer::Result timer_result;
Craig Tiller88568752015-03-04 10:50:43 -0800135
vjpai119c1032015-10-29 01:21:04 -0700136 // avoid std::vector for old compilers that expect a copy constructor
137 if (reset) {
138 Histogram* to_merge = new Histogram[threads_.size()];
139 for (size_t i = 0; i < threads_.size(); i++) {
Vijay Paice846702015-11-04 00:30:12 -0800140 threads_[i]->BeginSwap(&to_merge[i]);
vjpai119c1032015-10-29 01:21:04 -0700141 }
142 std::unique_ptr<Timer> timer(new Timer);
143 timer_.swap(timer);
144 for (size_t i = 0; i < threads_.size(); i++) {
Vijay Paice846702015-11-04 00:30:12 -0800145 threads_[i]->EndSwap();
146 latencies.Merge(to_merge[i]);
vjpai119c1032015-10-29 01:21:04 -0700147 }
148 delete[] to_merge;
149 timer_result = timer->Mark();
150 } else {
151 // merge snapshots of each thread histogram
152 for (size_t i = 0; i < threads_.size(); i++) {
Vijay Paice846702015-11-04 00:30:12 -0800153 threads_[i]->MergeStatsInto(&latencies);
vjpai119c1032015-10-29 01:21:04 -0700154 }
155 timer_result = timer_->Mark();
156 }
Craig Tiller88568752015-03-04 10:50:43 -0800157
158 ClientStats stats;
159 latencies.FillProto(stats.mutable_latencies());
160 stats.set_time_elapsed(timer_result.wall);
161 stats.set_time_system(timer_result.system);
162 stats.set_time_user(timer_result.user);
163 return stats;
164 }
165
166 protected:
vjpaib6df94a2015-11-30 15:52:50 -0800167 RequestType request_;
Vijay Pai372fd872015-06-08 13:30:08 -0700168 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -0800169
170 class ClientChannelInfo {
171 public:
Vijay Paieed63fa2015-08-05 23:08:34 +0000172 ClientChannelInfo() {}
vjpaib1db8692015-08-11 22:41:02 -0700173 ClientChannelInfo(const ClientChannelInfo& i) {
Vijay Pai90e73692015-08-05 19:15:36 -0700174 // The copy constructor is to satisfy old compilers
175 // that need it for using std::vector . It is only ever
176 // used for empty entries
Vijay Paieed63fa2015-08-05 23:08:34 +0000177 GPR_ASSERT(!i.channel_ && !i.stub_);
178 }
179 void init(const grpc::string& target, const ClientConfig& config) {
Vijay Pai90e73692015-08-05 19:15:36 -0700180 // We have to use a 2-phase init like this with a default
181 // constructor followed by an initializer function to make
182 // old compilers happy with using this in std::vector
Vijay Paice846702015-11-04 00:30:12 -0800183 channel_ = CreateTestChannel(
184 target, config.security_params().server_host_override(),
185 config.has_security_params(),
186 !config.security_params().use_test_ca());
vjpaib6df94a2015-11-30 15:52:50 -0800187 stub_ = create_stub_(channel_);
Vijay Paieed63fa2015-08-05 23:08:34 +0000188 }
yang-g8c2be9f2015-08-19 16:28:09 -0700189 Channel* get_channel() { return channel_.get(); }
vjpaib6df94a2015-11-30 15:52:50 -0800190 StubType* get_stub() { return stub_.get(); }
Vijay Pai90e73692015-08-05 19:15:36 -0700191
Craig Tiller88568752015-03-04 10:50:43 -0800192 private:
yang-g8c2be9f2015-08-19 16:28:09 -0700193 std::shared_ptr<Channel> channel_;
vjpaib6df94a2015-11-30 15:52:50 -0800194 std::unique_ptr<StubType> stub_;
Craig Tiller88568752015-03-04 10:50:43 -0800195 };
196 std::vector<ClientChannelInfo> channels_;
vjpaib6df94a2015-11-30 15:52:50 -0800197 std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> create_stub_;
Craig Tiller88568752015-03-04 10:50:43 -0800198
199 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800200 for (size_t i = 0; i < num_threads; i++) {
201 threads_.emplace_back(new Thread(this, i));
202 }
Craig Tiller88568752015-03-04 10:50:43 -0800203 }
204
Craig Tillera182bf12015-03-04 13:54:39 -0800205 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800206
Craig Tiller8a5a6662015-04-09 11:31:28 -0700207 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700208
Vijay Pai372fd872015-06-08 13:30:08 -0700209 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
210 // Set up the load distribution based on the number of threads
vjpai754751e2015-10-28 09:16:22 -0700211 const auto& load = config.load_params();
212
213 std::unique_ptr<RandomDist> random_dist;
vjpaifba20c92015-11-04 14:49:17 -0800214 switch (load.load_case()) {
215 case LoadParams::kClosedLoop:
216 // Closed-loop doesn't use random dist at all
217 break;
218 case LoadParams::kPoisson:
Vijay Paice846702015-11-04 00:30:12 -0800219 random_dist.reset(
220 new ExpDist(load.poisson().offered_load() / num_threads));
vjpaifba20c92015-11-04 14:49:17 -0800221 break;
222 case LoadParams::kUniform:
Vijay Paice846702015-11-04 00:30:12 -0800223 random_dist.reset(
224 new UniformDist(load.uniform().interarrival_lo() * num_threads,
225 load.uniform().interarrival_hi() * num_threads));
vjpaifba20c92015-11-04 14:49:17 -0800226 break;
227 case LoadParams::kDeterm:
Vijay Paice846702015-11-04 00:30:12 -0800228 random_dist.reset(
229 new DetDist(num_threads / load.determ().offered_load()));
vjpaifba20c92015-11-04 14:49:17 -0800230 break;
231 case LoadParams::kPareto:
Vijay Paice846702015-11-04 00:30:12 -0800232 random_dist.reset(
233 new ParetoDist(load.pareto().interarrival_base() * num_threads,
234 load.pareto().alpha()));
vjpaifba20c92015-11-04 14:49:17 -0800235 break;
236 default:
vjpai119c1032015-10-29 01:21:04 -0700237 GPR_ASSERT(false);
vjpai754751e2015-10-28 09:16:22 -0700238 }
239
240 // Set closed_loop_ based on whether or not random_dist is set
241 if (!random_dist) {
Vijay Pai372fd872015-06-08 13:30:08 -0700242 closed_loop_ = true;
243 } else {
244 closed_loop_ = false;
vjpai754751e2015-10-28 09:16:22 -0700245 // set up interarrival timer according to random dist
Vijay Pai372fd872015-06-08 13:30:08 -0700246 interarrival_timer_.init(*random_dist, num_threads);
247 for (size_t i = 0; i < num_threads; i++) {
248 next_time_.push_back(
249 grpc_time_source::now() +
250 std::chrono::duration_cast<grpc_time_source::duration>(
251 interarrival_timer_(i)));
252 }
253 }
254 }
255
256 bool NextIssueTime(int thread_idx, grpc_time* time_delay) {
257 if (closed_loop_) {
258 return false;
259 } else {
260 *time_delay = next_time_[thread_idx];
261 next_time_[thread_idx] +=
262 std::chrono::duration_cast<grpc_time_source::duration>(
263 interarrival_timer_(thread_idx));
264 return true;
265 }
266 }
267
Craig Tiller88568752015-03-04 10:50:43 -0800268 private:
269 class Thread {
270 public:
271 Thread(Client* client, size_t idx)
Vijay Paiab1dba72015-07-31 09:09:09 -0700272 : done_(false),
vjpai119c1032015-10-29 01:21:04 -0700273 new_stats_(nullptr),
Vijay Paiab1dba72015-07-31 09:09:09 -0700274 client_(client),
275 idx_(idx),
276 impl_(&Thread::ThreadFunc, this) {}
Craig Tiller88568752015-03-04 10:50:43 -0800277
278 ~Thread() {
279 {
280 std::lock_guard<std::mutex> g(mu_);
281 done_ = true;
282 }
283 impl_.join();
284 }
285
286 void BeginSwap(Histogram* n) {
287 std::lock_guard<std::mutex> g(mu_);
vjpai119c1032015-10-29 01:21:04 -0700288 new_stats_ = n;
Craig Tiller88568752015-03-04 10:50:43 -0800289 }
290
291 void EndSwap() {
292 std::unique_lock<std::mutex> g(mu_);
vjpai119c1032015-10-29 01:21:04 -0700293 while (new_stats_ != nullptr) {
Vijay Pai784005b2015-07-31 10:53:42 -0700294 cv_.wait(g);
295 };
Craig Tiller88568752015-03-04 10:50:43 -0800296 }
297
vjpai119c1032015-10-29 01:21:04 -0700298 void MergeStatsInto(Histogram* hist) {
299 std::unique_lock<std::mutex> g(mu_);
300 hist->Merge(histogram_);
301 }
302
Craig Tiller88568752015-03-04 10:50:43 -0800303 private:
304 Thread(const Thread&);
305 Thread& operator=(const Thread&);
306
vjpaia9e08302015-07-31 07:55:06 -0700307 void ThreadFunc() {
308 for (;;) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700309 // run the loop body
vjpaib1db8692015-08-11 22:41:02 -0700310 const bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_);
Vijay Paiab1dba72015-07-31 09:09:09 -0700311 // lock, see if we're done
312 std::lock_guard<std::mutex> g(mu_);
313 if (!thread_still_ok) {
314 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
315 done_ = true;
316 }
317 if (done_) {
318 return;
319 }
vjpai119c1032015-10-29 01:21:04 -0700320 // check if we're resetting stats, swap out the histogram if so
321 if (new_stats_) {
322 new_stats_->Swap(&histogram_);
323 new_stats_ = nullptr;
Vijay Paiab1dba72015-07-31 09:09:09 -0700324 cv_.notify_one();
325 }
vjpaia9e08302015-07-31 07:55:06 -0700326 }
327 }
328
vjpai119c1032015-10-29 01:21:04 -0700329 BenchmarkService::Stub* stub_;
Craig Tiller88568752015-03-04 10:50:43 -0800330 ClientConfig config_;
331 std::mutex mu_;
332 std::condition_variable cv_;
333 bool done_;
vjpai119c1032015-10-29 01:21:04 -0700334 Histogram* new_stats_;
Craig Tiller88568752015-03-04 10:50:43 -0800335 Histogram histogram_;
Vijay Paiab1dba72015-07-31 09:09:09 -0700336 Client* client_;
vjpaia9e08302015-07-31 07:55:06 -0700337 size_t idx_;
Craig Tiller88568752015-03-04 10:50:43 -0800338 std::thread impl_;
339 };
vjpaib6df94a2015-11-30 15:52:50 -0800340
Craig Tiller88568752015-03-04 10:50:43 -0800341 std::vector<std::unique_ptr<Thread>> threads_;
342 std::unique_ptr<Timer> timer_;
Vijay Pai372fd872015-06-08 13:30:08 -0700343
344 InterarrivalTimer interarrival_timer_;
345 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800346};
347
Craig Tiller5c8737d2015-05-21 11:42:17 -0700348std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
349std::unique_ptr<Client> CreateSynchronousStreamingClient(
350 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700351std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
352std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800353
354} // namespace testing
355} // namespace grpc
356
357#endif