blob: a6bd1e43431996a2f7e93bd112135b501260862a [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 Paiad3e00c2015-08-07 17:21:08 +000086 // avoid std::vector for old compilers
87 Histogram *to_merge = new Histogram[threads_.size()];
Craig Tiller88568752015-03-04 10:50:43 -080088 for (size_t i = 0; i < threads_.size(); i++) {
89 threads_[i]->BeginSwap(&to_merge[i]);
90 }
91 std::unique_ptr<Timer> timer(new Timer);
92 timer_.swap(timer);
93 for (size_t i = 0; i < threads_.size(); i++) {
94 threads_[i]->EndSwap();
95 latencies.Merge(&to_merge[i]);
96 }
Vijay Paiad3e00c2015-08-07 17:21:08 +000097 delete[] to_merge;
Craig Tiller88568752015-03-04 10:50:43 -080098
99 auto timer_result = timer->Mark();
100
101 ClientStats stats;
102 latencies.FillProto(stats.mutable_latencies());
103 stats.set_time_elapsed(timer_result.wall);
104 stats.set_time_system(timer_result.system);
105 stats.set_time_user(timer_result.user);
106 return stats;
107 }
108
109 protected:
110 SimpleRequest request_;
Vijay Pai372fd872015-06-08 13:30:08 -0700111 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -0800112
113 class ClientChannelInfo {
114 public:
Vijay Paieed63fa2015-08-05 23:08:34 +0000115 ClientChannelInfo() {}
Vijay Pai90e73692015-08-05 19:15:36 -0700116 ClientChannelInfo(const ClientChannelInfo& i) : channel_(), stub_() {
117 // The copy constructor is to satisfy old compilers
118 // that need it for using std::vector . It is only ever
119 // used for empty entries
Vijay Paieed63fa2015-08-05 23:08:34 +0000120 GPR_ASSERT(!i.channel_ && !i.stub_);
121 }
122 void init(const grpc::string& target, const ClientConfig& config) {
Vijay Pai90e73692015-08-05 19:15:36 -0700123 // We have to use a 2-phase init like this with a default
124 // constructor followed by an initializer function to make
125 // old compilers happy with using this in std::vector
Vijay Paieed63fa2015-08-05 23:08:34 +0000126 channel_ = CreateTestChannel(target, config.enable_ssl());
127 stub_ = TestService::NewStub(channel_);
128 }
Craig Tiller88568752015-03-04 10:50:43 -0800129 ChannelInterface* get_channel() { return channel_.get(); }
130 TestService::Stub* get_stub() { return stub_.get(); }
Vijay Pai90e73692015-08-05 19:15:36 -0700131
Craig Tiller88568752015-03-04 10:50:43 -0800132 private:
133 std::shared_ptr<ChannelInterface> channel_;
134 std::unique_ptr<TestService::Stub> stub_;
135 };
136 std::vector<ClientChannelInfo> channels_;
137
138 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800139 for (size_t i = 0; i < num_threads; i++) {
140 threads_.emplace_back(new Thread(this, i));
141 }
Craig Tiller88568752015-03-04 10:50:43 -0800142 }
143
Craig Tillera182bf12015-03-04 13:54:39 -0800144 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800145
Craig Tiller8a5a6662015-04-09 11:31:28 -0700146 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700147
Vijay Pai372fd872015-06-08 13:30:08 -0700148 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
149 // Set up the load distribution based on the number of threads
150 if (config.load_type() == CLOSED_LOOP) {
151 closed_loop_ = true;
152 } else {
153 closed_loop_ = false;
154
155 std::unique_ptr<RandomDist> random_dist;
156 const auto& load = config.load_params();
157 switch (config.load_type()) {
158 case POISSON:
159 random_dist.reset(
160 new ExpDist(load.poisson().offered_load() / num_threads));
161 break;
162 case UNIFORM:
163 random_dist.reset(
164 new UniformDist(load.uniform().interarrival_lo() * num_threads,
165 load.uniform().interarrival_hi() * num_threads));
166 break;
167 case DETERMINISTIC:
168 random_dist.reset(
169 new DetDist(num_threads / load.determ().offered_load()));
170 break;
171 case PARETO:
172 random_dist.reset(
173 new ParetoDist(load.pareto().interarrival_base() * num_threads,
174 load.pareto().alpha()));
175 break;
176 default:
177 GPR_ASSERT(false);
178 break;
179 }
180
181 interarrival_timer_.init(*random_dist, num_threads);
182 for (size_t i = 0; i < num_threads; i++) {
183 next_time_.push_back(
184 grpc_time_source::now() +
185 std::chrono::duration_cast<grpc_time_source::duration>(
186 interarrival_timer_(i)));
187 }
188 }
189 }
190
191 bool NextIssueTime(int thread_idx, grpc_time* time_delay) {
192 if (closed_loop_) {
193 return false;
194 } else {
195 *time_delay = next_time_[thread_idx];
196 next_time_[thread_idx] +=
197 std::chrono::duration_cast<grpc_time_source::duration>(
198 interarrival_timer_(thread_idx));
199 return true;
200 }
201 }
202
Craig Tiller88568752015-03-04 10:50:43 -0800203 private:
204 class Thread {
205 public:
206 Thread(Client* client, size_t idx)
Vijay Paiab1dba72015-07-31 09:09:09 -0700207 : done_(false),
208 new_(nullptr),
209 client_(client),
210 idx_(idx),
211 impl_(&Thread::ThreadFunc, this) {}
Craig Tiller88568752015-03-04 10:50:43 -0800212
213 ~Thread() {
214 {
215 std::lock_guard<std::mutex> g(mu_);
216 done_ = true;
217 }
218 impl_.join();
219 }
220
221 void BeginSwap(Histogram* n) {
222 std::lock_guard<std::mutex> g(mu_);
223 new_ = n;
224 }
225
226 void EndSwap() {
227 std::unique_lock<std::mutex> g(mu_);
Vijay Pai784005b2015-07-31 10:53:42 -0700228 while (new_ != nullptr) {
229 cv_.wait(g);
230 };
Craig Tiller88568752015-03-04 10:50:43 -0800231 }
232
233 private:
234 Thread(const Thread&);
235 Thread& operator=(const Thread&);
236
vjpaia9e08302015-07-31 07:55:06 -0700237 void ThreadFunc() {
238 for (;;) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700239 // run the loop body
240 bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_);
241 // lock, see if we're done
242 std::lock_guard<std::mutex> g(mu_);
243 if (!thread_still_ok) {
244 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
245 done_ = true;
246 }
247 if (done_) {
248 return;
249 }
250 // check if we're marking, swap out the histogram if so
251 if (new_) {
252 new_->Swap(&histogram_);
253 new_ = nullptr;
254 cv_.notify_one();
255 }
vjpaia9e08302015-07-31 07:55:06 -0700256 }
257 }
258
Craig Tiller88568752015-03-04 10:50:43 -0800259 TestService::Stub* stub_;
260 ClientConfig config_;
261 std::mutex mu_;
262 std::condition_variable cv_;
263 bool done_;
264 Histogram* new_;
265 Histogram histogram_;
Vijay Paiab1dba72015-07-31 09:09:09 -0700266 Client* client_;
vjpaia9e08302015-07-31 07:55:06 -0700267 size_t idx_;
Craig Tiller88568752015-03-04 10:50:43 -0800268 std::thread impl_;
269 };
270
271 std::vector<std::unique_ptr<Thread>> threads_;
272 std::unique_ptr<Timer> timer_;
Vijay Pai372fd872015-06-08 13:30:08 -0700273
274 InterarrivalTimer interarrival_timer_;
275 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800276};
277
Craig Tiller5c8737d2015-05-21 11:42:17 -0700278std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
279std::unique_ptr<Client> CreateSynchronousStreamingClient(
280 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700281std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
282std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800283
284} // namespace testing
285} // namespace grpc
286
287#endif