blob: 961e714fa829f3b0c8330407c9406216e378fb0a [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"
vjpaid08a7382015-11-02 16:45:08 -080044#include "test/proto/benchmarks/services.grpc.pb.h"
Craig Tillere38ec212015-03-04 11:19:15 -080045
Craig Tiller26598a32015-03-02 16:16:00 -080046namespace grpc {
Vijay Pai372fd872015-06-08 13:30:08 -070047
48#if defined(__APPLE__)
49// Specialize Timepoint for high res clock as we need that
50template <>
51class TimePoint<std::chrono::high_resolution_clock::time_point> {
52 public:
53 TimePoint(const std::chrono::high_resolution_clock::time_point& time) {
54 TimepointHR2Timespec(time, &time_);
55 }
56 gpr_timespec raw_time() const { return time_; }
57
58 private:
59 gpr_timespec time_;
60};
61#endif
62
Craig Tiller26598a32015-03-02 16:16:00 -080063namespace testing {
64
Vijay Pai372fd872015-06-08 13:30:08 -070065typedef std::chrono::high_resolution_clock grpc_time_source;
66typedef std::chrono::time_point<grpc_time_source> grpc_time;
67
Craig Tiller26598a32015-03-02 16:16:00 -080068class Client {
69 public:
Vijay Pai372fd872015-06-08 13:30:08 -070070 explicit Client(const ClientConfig& config)
Vijay Pai90e73692015-08-05 19:15:36 -070071 : channels_(config.client_channels()),
72 timer_(new Timer),
73 interarrival_timer_() {
Craig Tiller88568752015-03-04 10:50:43 -080074 for (int i = 0; i < config.client_channels(); i++) {
Vijay Paieed63fa2015-08-05 23:08:34 +000075 channels_[i].init(config.server_targets(i % config.server_targets_size()),
Vijay Pai90e73692015-08-05 19:15:36 -070076 config);
Craig Tiller88568752015-03-04 10:50:43 -080077 }
78 request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
79 request_.set_response_size(config.payload_size());
80 }
Craig Tiller26598a32015-03-02 16:16:00 -080081 virtual ~Client() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -080082
vjpai119c1032015-10-29 01:21:04 -070083 ClientStats Mark(bool reset) {
Craig Tiller88568752015-03-04 10:50:43 -080084 Histogram latencies;
vjpai119c1032015-10-29 01:21:04 -070085 Timer::Result timer_result;
Craig Tiller88568752015-03-04 10:50:43 -080086
vjpai119c1032015-10-29 01:21:04 -070087 // avoid std::vector for old compilers that expect a copy constructor
88 if (reset) {
89 Histogram* to_merge = new Histogram[threads_.size()];
90 for (size_t i = 0; i < threads_.size(); i++) {
91 threads_[i]->BeginSwap(&to_merge[i]);
92 }
93 std::unique_ptr<Timer> timer(new Timer);
94 timer_.swap(timer);
95 for (size_t i = 0; i < threads_.size(); i++) {
96 threads_[i]->EndSwap();
97 latencies.Merge(to_merge[i]);
98 }
99 delete[] to_merge;
100 timer_result = timer->Mark();
101 } else {
102 // merge snapshots of each thread histogram
103 for (size_t i = 0; i < threads_.size(); i++) {
104 threads_[i]->MergeStatsInto(&latencies);
105 }
106 timer_result = timer_->Mark();
107 }
Craig Tiller88568752015-03-04 10:50:43 -0800108
109 ClientStats stats;
110 latencies.FillProto(stats.mutable_latencies());
111 stats.set_time_elapsed(timer_result.wall);
112 stats.set_time_system(timer_result.system);
113 stats.set_time_user(timer_result.user);
114 return stats;
115 }
116
117 protected:
118 SimpleRequest request_;
Vijay Pai372fd872015-06-08 13:30:08 -0700119 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -0800120
121 class ClientChannelInfo {
122 public:
Vijay Paieed63fa2015-08-05 23:08:34 +0000123 ClientChannelInfo() {}
vjpaib1db8692015-08-11 22:41:02 -0700124 ClientChannelInfo(const ClientChannelInfo& i) {
Vijay Pai90e73692015-08-05 19:15:36 -0700125 // The copy constructor is to satisfy old compilers
126 // that need it for using std::vector . It is only ever
127 // used for empty entries
Vijay Paieed63fa2015-08-05 23:08:34 +0000128 GPR_ASSERT(!i.channel_ && !i.stub_);
129 }
130 void init(const grpc::string& target, const ClientConfig& config) {
Vijay Pai90e73692015-08-05 19:15:36 -0700131 // We have to use a 2-phase init like this with a default
132 // constructor followed by an initializer function to make
133 // old compilers happy with using this in std::vector
vjpai754751e2015-10-28 09:16:22 -0700134 channel_ = CreateTestChannel(target, config.use_tls());
vjpai119c1032015-10-29 01:21:04 -0700135 stub_ = BenchmarkService::NewStub(channel_);
Vijay Paieed63fa2015-08-05 23:08:34 +0000136 }
yang-g8c2be9f2015-08-19 16:28:09 -0700137 Channel* get_channel() { return channel_.get(); }
vjpai119c1032015-10-29 01:21:04 -0700138 BenchmarkService::Stub* get_stub() { return stub_.get(); }
Vijay Pai90e73692015-08-05 19:15:36 -0700139
Craig Tiller88568752015-03-04 10:50:43 -0800140 private:
yang-g8c2be9f2015-08-19 16:28:09 -0700141 std::shared_ptr<Channel> channel_;
vjpai119c1032015-10-29 01:21:04 -0700142 std::unique_ptr<BenchmarkService::Stub> stub_;
Craig Tiller88568752015-03-04 10:50:43 -0800143 };
144 std::vector<ClientChannelInfo> channels_;
145
146 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800147 for (size_t i = 0; i < num_threads; i++) {
148 threads_.emplace_back(new Thread(this, i));
149 }
Craig Tiller88568752015-03-04 10:50:43 -0800150 }
151
Craig Tillera182bf12015-03-04 13:54:39 -0800152 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800153
Craig Tiller8a5a6662015-04-09 11:31:28 -0700154 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700155
Vijay Pai372fd872015-06-08 13:30:08 -0700156 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
157 // Set up the load distribution based on the number of threads
vjpai754751e2015-10-28 09:16:22 -0700158 const auto& load = config.load_params();
159
160 std::unique_ptr<RandomDist> random_dist;
161 if (load.has_poisson()) {
162 random_dist.reset(new ExpDist(load.poisson().offered_load() /
163 num_threads));
164 } else if (load.has_uniform()) {
165 random_dist.reset(new UniformDist(load.uniform().interarrival_lo() *
166 num_threads,
167 load.uniform().interarrival_hi() *
168 num_threads));
169 } else if (load.has_determ()) {
170 random_dist.reset(new DetDist(num_threads / load.determ().offered_load()));
171 } else if (load.has_pareto()) {
172 random_dist.reset(new ParetoDist(load.pareto().interarrival_base() * num_threads,
173 load.pareto().alpha()));
vjpaid08a7382015-11-02 16:45:08 -0800174 } else if (load.has_closed_loop()) {
vjpai119c1032015-10-29 01:21:04 -0700175 // Closed-loop doesn't use random dist at all
176 } else { // invalid load type
177 GPR_ASSERT(false);
vjpai754751e2015-10-28 09:16:22 -0700178 }
179
180 // Set closed_loop_ based on whether or not random_dist is set
181 if (!random_dist) {
Vijay Pai372fd872015-06-08 13:30:08 -0700182 closed_loop_ = true;
183 } else {
184 closed_loop_ = false;
vjpai754751e2015-10-28 09:16:22 -0700185 // set up interarrival timer according to random dist
Vijay Pai372fd872015-06-08 13:30:08 -0700186 interarrival_timer_.init(*random_dist, num_threads);
187 for (size_t i = 0; i < num_threads; i++) {
188 next_time_.push_back(
189 grpc_time_source::now() +
190 std::chrono::duration_cast<grpc_time_source::duration>(
191 interarrival_timer_(i)));
192 }
193 }
194 }
195
196 bool NextIssueTime(int thread_idx, grpc_time* time_delay) {
197 if (closed_loop_) {
198 return false;
199 } else {
200 *time_delay = next_time_[thread_idx];
201 next_time_[thread_idx] +=
202 std::chrono::duration_cast<grpc_time_source::duration>(
203 interarrival_timer_(thread_idx));
204 return true;
205 }
206 }
207
Craig Tiller88568752015-03-04 10:50:43 -0800208 private:
209 class Thread {
210 public:
211 Thread(Client* client, size_t idx)
Vijay Paiab1dba72015-07-31 09:09:09 -0700212 : done_(false),
vjpai119c1032015-10-29 01:21:04 -0700213 new_stats_(nullptr),
Vijay Paiab1dba72015-07-31 09:09:09 -0700214 client_(client),
215 idx_(idx),
216 impl_(&Thread::ThreadFunc, this) {}
Craig Tiller88568752015-03-04 10:50:43 -0800217
218 ~Thread() {
219 {
220 std::lock_guard<std::mutex> g(mu_);
221 done_ = true;
222 }
223 impl_.join();
224 }
225
226 void BeginSwap(Histogram* n) {
227 std::lock_guard<std::mutex> g(mu_);
vjpai119c1032015-10-29 01:21:04 -0700228 new_stats_ = n;
Craig Tiller88568752015-03-04 10:50:43 -0800229 }
230
231 void EndSwap() {
232 std::unique_lock<std::mutex> g(mu_);
vjpai119c1032015-10-29 01:21:04 -0700233 while (new_stats_ != nullptr) {
Vijay Pai784005b2015-07-31 10:53:42 -0700234 cv_.wait(g);
235 };
Craig Tiller88568752015-03-04 10:50:43 -0800236 }
237
vjpai119c1032015-10-29 01:21:04 -0700238 void MergeStatsInto(Histogram* hist) {
239 std::unique_lock<std::mutex> g(mu_);
240 hist->Merge(histogram_);
241 }
242
Craig Tiller88568752015-03-04 10:50:43 -0800243 private:
244 Thread(const Thread&);
245 Thread& operator=(const Thread&);
246
vjpaia9e08302015-07-31 07:55:06 -0700247 void ThreadFunc() {
248 for (;;) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700249 // run the loop body
vjpaib1db8692015-08-11 22:41:02 -0700250 const bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_);
Vijay Paiab1dba72015-07-31 09:09:09 -0700251 // lock, see if we're done
252 std::lock_guard<std::mutex> g(mu_);
253 if (!thread_still_ok) {
254 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
255 done_ = true;
256 }
257 if (done_) {
258 return;
259 }
vjpai119c1032015-10-29 01:21:04 -0700260 // check if we're resetting stats, swap out the histogram if so
261 if (new_stats_) {
262 new_stats_->Swap(&histogram_);
263 new_stats_ = nullptr;
Vijay Paiab1dba72015-07-31 09:09:09 -0700264 cv_.notify_one();
265 }
vjpaia9e08302015-07-31 07:55:06 -0700266 }
267 }
268
vjpai119c1032015-10-29 01:21:04 -0700269 BenchmarkService::Stub* stub_;
Craig Tiller88568752015-03-04 10:50:43 -0800270 ClientConfig config_;
271 std::mutex mu_;
272 std::condition_variable cv_;
273 bool done_;
vjpai119c1032015-10-29 01:21:04 -0700274 Histogram* new_stats_;
Craig Tiller88568752015-03-04 10:50:43 -0800275 Histogram histogram_;
Vijay Paiab1dba72015-07-31 09:09:09 -0700276 Client* client_;
vjpaia9e08302015-07-31 07:55:06 -0700277 size_t idx_;
Craig Tiller88568752015-03-04 10:50:43 -0800278 std::thread impl_;
279 };
280
281 std::vector<std::unique_ptr<Thread>> threads_;
282 std::unique_ptr<Timer> timer_;
Vijay Pai372fd872015-06-08 13:30:08 -0700283
284 InterarrivalTimer interarrival_timer_;
285 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800286};
287
Craig Tiller5c8737d2015-05-21 11:42:17 -0700288std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
289std::unique_ptr<Client> CreateSynchronousStreamingClient(
290 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700291std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
292std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800293
294} // namespace testing
295} // namespace grpc
296
297#endif