blob: c7513ed9b58166a50e513fb6f913144402d7f003 [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 Paieed63fa2015-08-05 23:08:34 +000072 : channels_(config.client_channels()), timer_(new Timer), interarrival_timer_() {
Craig Tiller88568752015-03-04 10:50:43 -080073 for (int i = 0; i < config.client_channels(); i++) {
Vijay Paieed63fa2015-08-05 23:08:34 +000074 channels_[i].init(config.server_targets(i % config.server_targets_size()),
75 config);
Craig Tiller88568752015-03-04 10:50:43 -080076 }
77 request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
78 request_.set_response_size(config.payload_size());
79 }
Craig Tiller26598a32015-03-02 16:16:00 -080080 virtual ~Client() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -080081
Craig Tiller88568752015-03-04 10:50:43 -080082 ClientStats Mark() {
83 Histogram latencies;
Vijay Paieed63fa2015-08-05 23:08:34 +000084 Histogram to_merge[threads_.size()]; // avoid std::vector for old compilers
Craig Tiller88568752015-03-04 10:50:43 -080085 for (size_t i = 0; i < threads_.size(); i++) {
86 threads_[i]->BeginSwap(&to_merge[i]);
87 }
88 std::unique_ptr<Timer> timer(new Timer);
89 timer_.swap(timer);
90 for (size_t i = 0; i < threads_.size(); i++) {
91 threads_[i]->EndSwap();
92 latencies.Merge(&to_merge[i]);
93 }
94
95 auto timer_result = timer->Mark();
96
97 ClientStats stats;
98 latencies.FillProto(stats.mutable_latencies());
99 stats.set_time_elapsed(timer_result.wall);
100 stats.set_time_system(timer_result.system);
101 stats.set_time_user(timer_result.user);
102 return stats;
103 }
104
105 protected:
106 SimpleRequest request_;
Vijay Pai372fd872015-06-08 13:30:08 -0700107 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -0800108
109 class ClientChannelInfo {
110 public:
Vijay Paieed63fa2015-08-05 23:08:34 +0000111 ClientChannelInfo() {}
112 ClientChannelInfo(const ClientChannelInfo& i): channel_(), stub_() {
113 GPR_ASSERT(!i.channel_ && !i.stub_);
114 }
115 void init(const grpc::string& target, const ClientConfig& config) {
116 channel_ = CreateTestChannel(target, config.enable_ssl());
117 stub_ = TestService::NewStub(channel_);
118 }
Craig Tiller88568752015-03-04 10:50:43 -0800119 ChannelInterface* get_channel() { return channel_.get(); }
120 TestService::Stub* get_stub() { return stub_.get(); }
Craig Tiller88568752015-03-04 10:50:43 -0800121 private:
122 std::shared_ptr<ChannelInterface> channel_;
123 std::unique_ptr<TestService::Stub> stub_;
124 };
125 std::vector<ClientChannelInfo> channels_;
126
127 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800128 for (size_t i = 0; i < num_threads; i++) {
129 threads_.emplace_back(new Thread(this, i));
130 }
Craig Tiller88568752015-03-04 10:50:43 -0800131 }
132
Craig Tillera182bf12015-03-04 13:54:39 -0800133 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800134
Craig Tiller8a5a6662015-04-09 11:31:28 -0700135 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700136
Vijay Pai372fd872015-06-08 13:30:08 -0700137 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
138 // Set up the load distribution based on the number of threads
139 if (config.load_type() == CLOSED_LOOP) {
140 closed_loop_ = true;
141 } else {
142 closed_loop_ = false;
143
144 std::unique_ptr<RandomDist> random_dist;
145 const auto& load = config.load_params();
146 switch (config.load_type()) {
147 case POISSON:
148 random_dist.reset(
149 new ExpDist(load.poisson().offered_load() / num_threads));
150 break;
151 case UNIFORM:
152 random_dist.reset(
153 new UniformDist(load.uniform().interarrival_lo() * num_threads,
154 load.uniform().interarrival_hi() * num_threads));
155 break;
156 case DETERMINISTIC:
157 random_dist.reset(
158 new DetDist(num_threads / load.determ().offered_load()));
159 break;
160 case PARETO:
161 random_dist.reset(
162 new ParetoDist(load.pareto().interarrival_base() * num_threads,
163 load.pareto().alpha()));
164 break;
165 default:
166 GPR_ASSERT(false);
167 break;
168 }
169
170 interarrival_timer_.init(*random_dist, num_threads);
171 for (size_t i = 0; i < num_threads; i++) {
172 next_time_.push_back(
173 grpc_time_source::now() +
174 std::chrono::duration_cast<grpc_time_source::duration>(
175 interarrival_timer_(i)));
176 }
177 }
178 }
179
180 bool NextIssueTime(int thread_idx, grpc_time* time_delay) {
181 if (closed_loop_) {
182 return false;
183 } else {
184 *time_delay = next_time_[thread_idx];
185 next_time_[thread_idx] +=
186 std::chrono::duration_cast<grpc_time_source::duration>(
187 interarrival_timer_(thread_idx));
188 return true;
189 }
190 }
191
Craig Tiller88568752015-03-04 10:50:43 -0800192 private:
193 class Thread {
194 public:
195 Thread(Client* client, size_t idx)
Vijay Paiab1dba72015-07-31 09:09:09 -0700196 : done_(false),
197 new_(nullptr),
198 client_(client),
199 idx_(idx),
200 impl_(&Thread::ThreadFunc, this) {}
Craig Tiller88568752015-03-04 10:50:43 -0800201
202 ~Thread() {
203 {
204 std::lock_guard<std::mutex> g(mu_);
205 done_ = true;
206 }
207 impl_.join();
208 }
209
210 void BeginSwap(Histogram* n) {
211 std::lock_guard<std::mutex> g(mu_);
212 new_ = n;
213 }
214
215 void EndSwap() {
216 std::unique_lock<std::mutex> g(mu_);
Vijay Pai784005b2015-07-31 10:53:42 -0700217 while (new_ != nullptr) {
218 cv_.wait(g);
219 };
Craig Tiller88568752015-03-04 10:50:43 -0800220 }
221
222 private:
223 Thread(const Thread&);
224 Thread& operator=(const Thread&);
225
vjpaia9e08302015-07-31 07:55:06 -0700226 void ThreadFunc() {
227 for (;;) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700228 // run the loop body
229 bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_);
230 // lock, see if we're done
231 std::lock_guard<std::mutex> g(mu_);
232 if (!thread_still_ok) {
233 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
234 done_ = true;
235 }
236 if (done_) {
237 return;
238 }
239 // check if we're marking, swap out the histogram if so
240 if (new_) {
241 new_->Swap(&histogram_);
242 new_ = nullptr;
243 cv_.notify_one();
244 }
vjpaia9e08302015-07-31 07:55:06 -0700245 }
246 }
247
Craig Tiller88568752015-03-04 10:50:43 -0800248 TestService::Stub* stub_;
249 ClientConfig config_;
250 std::mutex mu_;
251 std::condition_variable cv_;
252 bool done_;
253 Histogram* new_;
254 Histogram histogram_;
Vijay Paiab1dba72015-07-31 09:09:09 -0700255 Client* client_;
vjpaia9e08302015-07-31 07:55:06 -0700256 size_t idx_;
Craig Tiller88568752015-03-04 10:50:43 -0800257 std::thread impl_;
258 };
259
260 std::vector<std::unique_ptr<Thread>> threads_;
261 std::unique_ptr<Timer> timer_;
Vijay Pai372fd872015-06-08 13:30:08 -0700262
263 InterarrivalTimer interarrival_timer_;
264 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800265};
266
Craig Tiller5c8737d2015-05-21 11:42:17 -0700267std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
268std::unique_ptr<Client> CreateSynchronousStreamingClient(
269 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700270std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
271std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800272
273} // namespace testing
274} // namespace grpc
275
276#endif