blob: 9a2894687dd1cecdbd258963cd763aa2402fd518 [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
Vijay Paie4886682015-12-30 11:56:19 -080040#include <grpc++/support/byte_buffer.h>
41#include <grpc++/support/slice.h>
42
Craig Tiller88568752015-03-04 10:50:43 -080043#include "test/cpp/qps/histogram.h"
Vijay Pai372fd872015-06-08 13:30:08 -070044#include "test/cpp/qps/interarrival.h"
Craig Tiller88568752015-03-04 10:50:43 -080045#include "test/cpp/qps/timer.h"
yang-g9e2f90c2015-08-21 15:35:03 -070046#include "test/cpp/util/create_test_channel.h"
vjpai780a7f22015-11-04 00:19:09 -080047#include "test/proto/benchmarks/payloads.grpc.pb.h"
vjpaid08a7382015-11-02 16:45:08 -080048#include "test/proto/benchmarks/services.grpc.pb.h"
Craig Tillere38ec212015-03-04 11:19:15 -080049
Craig Tiller26598a32015-03-02 16:16:00 -080050namespace grpc {
Vijay Pai372fd872015-06-08 13:30:08 -070051
52#if defined(__APPLE__)
53// Specialize Timepoint for high res clock as we need that
54template <>
55class TimePoint<std::chrono::high_resolution_clock::time_point> {
56 public:
57 TimePoint(const std::chrono::high_resolution_clock::time_point& time) {
58 TimepointHR2Timespec(time, &time_);
59 }
60 gpr_timespec raw_time() const { return time_; }
61
62 private:
63 gpr_timespec time_;
64};
65#endif
66
Craig Tiller26598a32015-03-02 16:16:00 -080067namespace testing {
68
Vijay Pai372fd872015-06-08 13:30:08 -070069typedef std::chrono::high_resolution_clock grpc_time_source;
70typedef std::chrono::time_point<grpc_time_source> grpc_time;
71
vjpaib6df94a2015-11-30 15:52:50 -080072namespace ClientRequestCreation {
73template <class RequestType>
74void CreateRequest(RequestType *req, const PayloadConfig&) {
75 // this template must be specialized
76 // fail with an assertion rather than a compile-time
77 // check since these only happen at the beginning anyway
78 GPR_ASSERT(false);
79}
Vijay Paie4886682015-12-30 11:56:19 -080080
vjpaib6df94a2015-11-30 15:52:50 -080081template <>
82void CreateRequest<SimpleRequest>(SimpleRequest *req, const PayloadConfig& payload_config) {
83 if (payload_config.has_bytebuf_params()) {
84 GPR_ASSERT(false); // not appropriate for this specialization
85 } else if (payload_config.has_simple_params()) {
86 req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
87 req->set_response_size(payload_config.simple_params().resp_size());
88 req->mutable_payload()->set_type(grpc::testing::PayloadType::COMPRESSABLE);
89 int size = payload_config.simple_params().req_size();
90 std::unique_ptr<char[]> body(new char[size]);
91 req->mutable_payload()->set_body(body.get(), size);
92 } else if (payload_config.has_complex_params()) {
93 GPR_ASSERT(false); // not appropriate for this specialization
94 } else {
95 // default should be simple proto without payloads
96 req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
97 req->set_response_size(0);
98 req->mutable_payload()->set_type(grpc::testing::PayloadType::COMPRESSABLE);
99 }
100}
101template <>
Vijay Paie4886682015-12-30 11:56:19 -0800102void CreateRequest<ByteBuffer>(ByteBuffer *req,
103 const PayloadConfig& payload_config) {
vjpaib6df94a2015-11-30 15:52:50 -0800104 if (payload_config.has_bytebuf_params()) {
Vijay Paie4886682015-12-30 11:56:19 -0800105 if (payload_config.bytebuf_params().req_size() > 0) {
106 std::unique_ptr<char>
107 buf(new char[payload_config.bytebuf_params().req_size()]);
108 gpr_slice s =
109 gpr_slice_from_copied_buffer(buf.get(),
110 payload_config.bytebuf_params().req_size());
vjpaib6df94a2015-11-30 15:52:50 -0800111 Slice slice(s, Slice::STEAL_REF);
112 std::unique_ptr<ByteBuffer> bbuf(new ByteBuffer(&slice, 1));
113 req->MoveFrom(bbuf.get());
114 } else {
115 GPR_ASSERT(false); // not appropriate for this specialization
116 }
117 }
118}
119}
Vijay Paie4886682015-12-30 11:56:19 -0800120
Craig Tiller26598a32015-03-02 16:16:00 -0800121class Client {
122 public:
Vijay Paie4886682015-12-30 11:56:19 -0800123 Client() : timer_(new Timer), interarrival_timer_() {}
124 virtual ~Client();
Craig Tiller6af9ed02015-03-02 22:42:10 -0800125
vjpai119c1032015-10-29 01:21:04 -0700126 ClientStats Mark(bool reset) {
Craig Tiller88568752015-03-04 10:50:43 -0800127 Histogram latencies;
vjpai119c1032015-10-29 01:21:04 -0700128 Timer::Result timer_result;
Craig Tiller88568752015-03-04 10:50:43 -0800129
vjpai119c1032015-10-29 01:21:04 -0700130 // avoid std::vector for old compilers that expect a copy constructor
131 if (reset) {
132 Histogram* to_merge = new Histogram[threads_.size()];
133 for (size_t i = 0; i < threads_.size(); i++) {
Vijay Paice846702015-11-04 00:30:12 -0800134 threads_[i]->BeginSwap(&to_merge[i]);
vjpai119c1032015-10-29 01:21:04 -0700135 }
136 std::unique_ptr<Timer> timer(new Timer);
137 timer_.swap(timer);
138 for (size_t i = 0; i < threads_.size(); i++) {
Vijay Paice846702015-11-04 00:30:12 -0800139 threads_[i]->EndSwap();
140 latencies.Merge(to_merge[i]);
vjpai119c1032015-10-29 01:21:04 -0700141 }
142 delete[] to_merge;
143 timer_result = timer->Mark();
144 } else {
145 // merge snapshots of each thread histogram
146 for (size_t i = 0; i < threads_.size(); i++) {
Vijay Paice846702015-11-04 00:30:12 -0800147 threads_[i]->MergeStatsInto(&latencies);
vjpai119c1032015-10-29 01:21:04 -0700148 }
149 timer_result = timer_->Mark();
150 }
Craig Tiller88568752015-03-04 10:50:43 -0800151
152 ClientStats stats;
153 latencies.FillProto(stats.mutable_latencies());
154 stats.set_time_elapsed(timer_result.wall);
155 stats.set_time_system(timer_result.system);
156 stats.set_time_user(timer_result.user);
157 return stats;
158 }
Craig Tiller88568752015-03-04 10:50:43 -0800159 protected:
Vijay Pai372fd872015-06-08 13:30:08 -0700160 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -0800161
Craig Tiller88568752015-03-04 10:50:43 -0800162 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800163 for (size_t i = 0; i < num_threads; i++) {
164 threads_.emplace_back(new Thread(this, i));
165 }
Craig Tiller88568752015-03-04 10:50:43 -0800166 }
167
Craig Tillera182bf12015-03-04 13:54:39 -0800168 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800169
Craig Tiller8a5a6662015-04-09 11:31:28 -0700170 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700171
Vijay Pai372fd872015-06-08 13:30:08 -0700172 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
173 // Set up the load distribution based on the number of threads
vjpai754751e2015-10-28 09:16:22 -0700174 const auto& load = config.load_params();
175
176 std::unique_ptr<RandomDist> random_dist;
vjpaifba20c92015-11-04 14:49:17 -0800177 switch (load.load_case()) {
Craig Tiller3c53bb22015-11-10 14:24:36 +0000178 case LoadParams::kClosedLoop:
179 // Closed-loop doesn't use random dist at all
180 break;
181 case LoadParams::kPoisson:
182 random_dist.reset(
183 new ExpDist(load.poisson().offered_load() / num_threads));
184 break;
185 case LoadParams::kUniform:
186 random_dist.reset(
187 new UniformDist(load.uniform().interarrival_lo() * num_threads,
188 load.uniform().interarrival_hi() * num_threads));
189 break;
190 case LoadParams::kDeterm:
191 random_dist.reset(
192 new DetDist(num_threads / load.determ().offered_load()));
193 break;
194 case LoadParams::kPareto:
195 random_dist.reset(
196 new ParetoDist(load.pareto().interarrival_base() * num_threads,
197 load.pareto().alpha()));
198 break;
199 default:
200 GPR_ASSERT(false);
vjpai754751e2015-10-28 09:16:22 -0700201 }
202
203 // Set closed_loop_ based on whether or not random_dist is set
204 if (!random_dist) {
Vijay Pai372fd872015-06-08 13:30:08 -0700205 closed_loop_ = true;
206 } else {
207 closed_loop_ = false;
vjpai754751e2015-10-28 09:16:22 -0700208 // set up interarrival timer according to random dist
Vijay Pai372fd872015-06-08 13:30:08 -0700209 interarrival_timer_.init(*random_dist, num_threads);
210 for (size_t i = 0; i < num_threads; i++) {
211 next_time_.push_back(
212 grpc_time_source::now() +
213 std::chrono::duration_cast<grpc_time_source::duration>(
214 interarrival_timer_(i)));
215 }
216 }
217 }
218
219 bool NextIssueTime(int thread_idx, grpc_time* time_delay) {
220 if (closed_loop_) {
221 return false;
222 } else {
223 *time_delay = next_time_[thread_idx];
224 next_time_[thread_idx] +=
225 std::chrono::duration_cast<grpc_time_source::duration>(
226 interarrival_timer_(thread_idx));
227 return true;
228 }
229 }
Craig Tiller88568752015-03-04 10:50:43 -0800230 private:
231 class Thread {
232 public:
233 Thread(Client* client, size_t idx)
Vijay Paiab1dba72015-07-31 09:09:09 -0700234 : done_(false),
vjpai119c1032015-10-29 01:21:04 -0700235 new_stats_(nullptr),
Vijay Paiab1dba72015-07-31 09:09:09 -0700236 client_(client),
237 idx_(idx),
238 impl_(&Thread::ThreadFunc, this) {}
Craig Tiller88568752015-03-04 10:50:43 -0800239
240 ~Thread() {
241 {
242 std::lock_guard<std::mutex> g(mu_);
243 done_ = true;
244 }
245 impl_.join();
246 }
247
248 void BeginSwap(Histogram* n) {
249 std::lock_guard<std::mutex> g(mu_);
vjpai119c1032015-10-29 01:21:04 -0700250 new_stats_ = n;
Craig Tiller88568752015-03-04 10:50:43 -0800251 }
252
253 void EndSwap() {
254 std::unique_lock<std::mutex> g(mu_);
vjpai119c1032015-10-29 01:21:04 -0700255 while (new_stats_ != nullptr) {
Vijay Pai784005b2015-07-31 10:53:42 -0700256 cv_.wait(g);
257 };
Craig Tiller88568752015-03-04 10:50:43 -0800258 }
259
vjpai119c1032015-10-29 01:21:04 -0700260 void MergeStatsInto(Histogram* hist) {
261 std::unique_lock<std::mutex> g(mu_);
262 hist->Merge(histogram_);
263 }
264
Craig Tiller88568752015-03-04 10:50:43 -0800265 private:
266 Thread(const Thread&);
267 Thread& operator=(const Thread&);
268
vjpaia9e08302015-07-31 07:55:06 -0700269 void ThreadFunc() {
270 for (;;) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700271 // run the loop body
vjpaib1db8692015-08-11 22:41:02 -0700272 const bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_);
Vijay Paiab1dba72015-07-31 09:09:09 -0700273 // lock, see if we're done
274 std::lock_guard<std::mutex> g(mu_);
275 if (!thread_still_ok) {
276 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
277 done_ = true;
278 }
279 if (done_) {
280 return;
281 }
vjpai119c1032015-10-29 01:21:04 -0700282 // check if we're resetting stats, swap out the histogram if so
283 if (new_stats_) {
284 new_stats_->Swap(&histogram_);
285 new_stats_ = nullptr;
Vijay Paiab1dba72015-07-31 09:09:09 -0700286 cv_.notify_one();
287 }
vjpaia9e08302015-07-31 07:55:06 -0700288 }
289 }
290
Craig Tiller88568752015-03-04 10:50:43 -0800291 std::mutex mu_;
292 std::condition_variable cv_;
293 bool done_;
vjpai119c1032015-10-29 01:21:04 -0700294 Histogram* new_stats_;
Craig Tiller88568752015-03-04 10:50:43 -0800295 Histogram histogram_;
Vijay Paiab1dba72015-07-31 09:09:09 -0700296 Client* client_;
vjpaia9e08302015-07-31 07:55:06 -0700297 size_t idx_;
Craig Tiller88568752015-03-04 10:50:43 -0800298 std::thread impl_;
299 };
Vijay Paie4886682015-12-30 11:56:19 -0800300
Craig Tiller88568752015-03-04 10:50:43 -0800301 std::vector<std::unique_ptr<Thread>> threads_;
302 std::unique_ptr<Timer> timer_;
Vijay Pai372fd872015-06-08 13:30:08 -0700303
304 InterarrivalTimer interarrival_timer_;
305 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800306};
307
Vijay Paie4886682015-12-30 11:56:19 -0800308template <class StubType, class RequestType>
309class ClientImpl : public Client {
310 public:
311 ClientImpl(const ClientConfig& config,
312 std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> create_stub)
313 : channels_(config.client_channels()),
314 create_stub_(create_stub) {
315 for (int i = 0; i < config.client_channels(); i++) {
316 channels_[i].init(config.server_targets(i % config.server_targets_size()),
317 config);
318 }
319
320 ClientRequestCreation::CreateRequest<RequestType>(&request_, config.payload_config());
321 }
322 virtual ~ClientImpl() {}
323
324 protected:
325 RequestType request_;
326
327 class ClientChannelInfo {
328 public:
329 ClientChannelInfo() {}
330 ClientChannelInfo(const ClientChannelInfo& i) {
331 // The copy constructor is to satisfy old compilers
332 // that need it for using std::vector . It is only ever
333 // used for empty entries
334 GPR_ASSERT(!i.channel_ && !i.stub_);
335 }
336 void init(const grpc::string& target, const ClientConfig& config) {
337 // We have to use a 2-phase init like this with a default
338 // constructor followed by an initializer function to make
339 // old compilers happy with using this in std::vector
340 channel_ = CreateTestChannel(
341 target, config.security_params().server_host_override(),
342 config.has_security_params(),
343 !config.security_params().use_test_ca());
344 stub_ = create_stub_(channel_);
345 }
346 Channel* get_channel() { return channel_.get(); }
347 StubType* get_stub() { return stub_.get(); }
348
349 private:
350 std::shared_ptr<Channel> channel_;
351 std::unique_ptr<StubType> stub_;
352 };
353 std::vector<ClientChannelInfo> channels_;
354 std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> create_stub_;
355};
356
Craig Tiller5c8737d2015-05-21 11:42:17 -0700357std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
358std::unique_ptr<Client> CreateSynchronousStreamingClient(
359 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700360std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
361std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800362
363} // namespace testing
364} // namespace grpc
365
366#endif