blob: 23993131ccf5fb3d8d33c9c1d25e809b1a9a6532 [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
Craig Tiller88568752015-03-04 10:50:43 -080037#include "test/cpp/qps/histogram.h"
Vijay Pai372fd872015-06-08 13:30:08 -070038#include "test/cpp/qps/interarrival.h"
Craig Tiller88568752015-03-04 10:50:43 -080039#include "test/cpp/qps/timer.h"
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020040#include "test/cpp/qps/qpstest.grpc.pb.h"
Craig Tiller26598a32015-03-02 16:16:00 -080041
Craig Tillere38ec212015-03-04 11:19:15 -080042#include <condition_variable>
43#include <mutex>
vjpaia9e08302015-07-31 07:55:06 -070044#include <grpc++/config.h>
45#include <grpc++/config.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 }
79 request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
80 request_.set_response_size(config.payload_size());
81 }
Craig Tiller26598a32015-03-02 16:16:00 -080082 virtual ~Client() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -080083
Craig Tiller88568752015-03-04 10:50:43 -080084 ClientStats Mark() {
85 Histogram latencies;
Vijay Pai90e73692015-08-05 19:15:36 -070086 Histogram to_merge[threads_.size()]; // avoid std::vector for old compilers
Craig Tiller88568752015-03-04 10:50:43 -080087 for (size_t i = 0; i < threads_.size(); i++) {
88 threads_[i]->BeginSwap(&to_merge[i]);
89 }
90 std::unique_ptr<Timer> timer(new Timer);
91 timer_.swap(timer);
92 for (size_t i = 0; i < threads_.size(); i++) {
93 threads_[i]->EndSwap();
94 latencies.Merge(&to_merge[i]);
95 }
96
97 auto timer_result = timer->Mark();
98
99 ClientStats stats;
100 latencies.FillProto(stats.mutable_latencies());
101 stats.set_time_elapsed(timer_result.wall);
102 stats.set_time_system(timer_result.system);
103 stats.set_time_user(timer_result.user);
104 return stats;
105 }
106
107 protected:
108 SimpleRequest request_;
Vijay Pai372fd872015-06-08 13:30:08 -0700109 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -0800110
111 class ClientChannelInfo {
112 public:
Vijay Paieed63fa2015-08-05 23:08:34 +0000113 ClientChannelInfo() {}
Vijay Pai90e73692015-08-05 19:15:36 -0700114 ClientChannelInfo(const ClientChannelInfo& i) : channel_(), stub_() {
115 // The copy constructor is to satisfy old compilers
116 // that need it for using std::vector . It is only ever
117 // used for empty entries
Vijay Paieed63fa2015-08-05 23:08:34 +0000118 GPR_ASSERT(!i.channel_ && !i.stub_);
119 }
120 void init(const grpc::string& target, const ClientConfig& config) {
Vijay Pai90e73692015-08-05 19:15:36 -0700121 // We have to use a 2-phase init like this with a default
122 // constructor followed by an initializer function to make
123 // old compilers happy with using this in std::vector
Vijay Paieed63fa2015-08-05 23:08:34 +0000124 channel_ = CreateTestChannel(target, config.enable_ssl());
125 stub_ = TestService::NewStub(channel_);
126 }
Craig Tiller88568752015-03-04 10:50:43 -0800127 ChannelInterface* get_channel() { return channel_.get(); }
128 TestService::Stub* get_stub() { return stub_.get(); }
Vijay Pai90e73692015-08-05 19:15:36 -0700129
Craig Tiller88568752015-03-04 10:50:43 -0800130 private:
131 std::shared_ptr<ChannelInterface> channel_;
132 std::unique_ptr<TestService::Stub> stub_;
133 };
134 std::vector<ClientChannelInfo> channels_;
135
136 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800137 for (size_t i = 0; i < num_threads; i++) {
138 threads_.emplace_back(new Thread(this, i));
139 }
Craig Tiller88568752015-03-04 10:50:43 -0800140 }
141
Craig Tillera182bf12015-03-04 13:54:39 -0800142 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800143
Craig Tiller8a5a6662015-04-09 11:31:28 -0700144 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700145
Vijay Pai372fd872015-06-08 13:30:08 -0700146 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
147 // Set up the load distribution based on the number of threads
148 if (config.load_type() == CLOSED_LOOP) {
149 closed_loop_ = true;
150 } else {
151 closed_loop_ = false;
152
153 std::unique_ptr<RandomDist> random_dist;
154 const auto& load = config.load_params();
155 switch (config.load_type()) {
156 case POISSON:
157 random_dist.reset(
158 new ExpDist(load.poisson().offered_load() / num_threads));
159 break;
160 case UNIFORM:
161 random_dist.reset(
162 new UniformDist(load.uniform().interarrival_lo() * num_threads,
163 load.uniform().interarrival_hi() * num_threads));
164 break;
165 case DETERMINISTIC:
166 random_dist.reset(
167 new DetDist(num_threads / load.determ().offered_load()));
168 break;
169 case PARETO:
170 random_dist.reset(
171 new ParetoDist(load.pareto().interarrival_base() * num_threads,
172 load.pareto().alpha()));
173 break;
174 default:
175 GPR_ASSERT(false);
176 break;
177 }
178
179 interarrival_timer_.init(*random_dist, num_threads);
180 for (size_t i = 0; i < num_threads; i++) {
181 next_time_.push_back(
182 grpc_time_source::now() +
183 std::chrono::duration_cast<grpc_time_source::duration>(
184 interarrival_timer_(i)));
185 }
186 }
187 }
188
189 bool NextIssueTime(int thread_idx, grpc_time* time_delay) {
190 if (closed_loop_) {
191 return false;
192 } else {
193 *time_delay = next_time_[thread_idx];
194 next_time_[thread_idx] +=
195 std::chrono::duration_cast<grpc_time_source::duration>(
196 interarrival_timer_(thread_idx));
197 return true;
198 }
199 }
200
Craig Tiller88568752015-03-04 10:50:43 -0800201 private:
202 class Thread {
203 public:
204 Thread(Client* client, size_t idx)
Vijay Paiab1dba72015-07-31 09:09:09 -0700205 : done_(false),
206 new_(nullptr),
207 client_(client),
208 idx_(idx),
209 impl_(&Thread::ThreadFunc, this) {}
Craig Tiller88568752015-03-04 10:50:43 -0800210
211 ~Thread() {
212 {
213 std::lock_guard<std::mutex> g(mu_);
214 done_ = true;
215 }
216 impl_.join();
217 }
218
219 void BeginSwap(Histogram* n) {
220 std::lock_guard<std::mutex> g(mu_);
221 new_ = n;
222 }
223
224 void EndSwap() {
225 std::unique_lock<std::mutex> g(mu_);
Vijay Pai784005b2015-07-31 10:53:42 -0700226 while (new_ != nullptr) {
227 cv_.wait(g);
228 };
Craig Tiller88568752015-03-04 10:50:43 -0800229 }
230
231 private:
232 Thread(const Thread&);
233 Thread& operator=(const Thread&);
234
vjpaia9e08302015-07-31 07:55:06 -0700235 void ThreadFunc() {
236 for (;;) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700237 // run the loop body
238 bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_);
239 // lock, see if we're done
240 std::lock_guard<std::mutex> g(mu_);
241 if (!thread_still_ok) {
242 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
243 done_ = true;
244 }
245 if (done_) {
246 return;
247 }
248 // check if we're marking, swap out the histogram if so
249 if (new_) {
250 new_->Swap(&histogram_);
251 new_ = nullptr;
252 cv_.notify_one();
253 }
vjpaia9e08302015-07-31 07:55:06 -0700254 }
255 }
256
Craig Tiller88568752015-03-04 10:50:43 -0800257 TestService::Stub* stub_;
258 ClientConfig config_;
259 std::mutex mu_;
260 std::condition_variable cv_;
261 bool done_;
262 Histogram* new_;
263 Histogram histogram_;
Vijay Paiab1dba72015-07-31 09:09:09 -0700264 Client* client_;
vjpaia9e08302015-07-31 07:55:06 -0700265 size_t idx_;
Craig Tiller88568752015-03-04 10:50:43 -0800266 std::thread impl_;
267 };
268
269 std::vector<std::unique_ptr<Thread>> threads_;
270 std::unique_ptr<Timer> timer_;
Vijay Pai372fd872015-06-08 13:30:08 -0700271
272 InterarrivalTimer interarrival_timer_;
273 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800274};
275
Craig Tiller5c8737d2015-05-21 11:42:17 -0700276std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
277std::unique_ptr<Client> CreateSynchronousStreamingClient(
278 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700279std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
280std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800281
282} // namespace testing
283} // namespace grpc
284
285#endif