blob: cdcae9ad3fbf753922bbe392aab7751d895132cf [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)
72 : timer_(new Timer), interarrival_timer_() {
Craig Tiller88568752015-03-04 10:50:43 -080073 for (int i = 0; i < config.client_channels(); i++) {
Vijay Paiab1dba72015-07-31 09:09:09 -070074 channels_.emplace_back(
75 config.server_targets(i % config.server_targets_size()), 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;
84 std::vector<Histogram> to_merge(threads_.size());
85 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:
Craig Tillera182bf12015-03-04 13:54:39 -0800111 ClientChannelInfo(const grpc::string& target, const ClientConfig& config)
Craig Tiller88568752015-03-04 10:50:43 -0800112 : channel_(CreateTestChannel(target, config.enable_ssl())),
113 stub_(TestService::NewStub(channel_)) {}
114 ChannelInterface* get_channel() { return channel_.get(); }
115 TestService::Stub* get_stub() { return stub_.get(); }
116
117 private:
118 std::shared_ptr<ChannelInterface> channel_;
119 std::unique_ptr<TestService::Stub> stub_;
120 };
121 std::vector<ClientChannelInfo> channels_;
122
123 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800124 for (size_t i = 0; i < num_threads; i++) {
125 threads_.emplace_back(new Thread(this, i));
126 }
Craig Tiller88568752015-03-04 10:50:43 -0800127 }
128
Craig Tillera182bf12015-03-04 13:54:39 -0800129 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800130
Craig Tiller8a5a6662015-04-09 11:31:28 -0700131 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700132
Vijay Pai372fd872015-06-08 13:30:08 -0700133 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
134 // Set up the load distribution based on the number of threads
135 if (config.load_type() == CLOSED_LOOP) {
136 closed_loop_ = true;
137 } else {
138 closed_loop_ = false;
139
140 std::unique_ptr<RandomDist> random_dist;
141 const auto& load = config.load_params();
142 switch (config.load_type()) {
143 case POISSON:
144 random_dist.reset(
145 new ExpDist(load.poisson().offered_load() / num_threads));
146 break;
147 case UNIFORM:
148 random_dist.reset(
149 new UniformDist(load.uniform().interarrival_lo() * num_threads,
150 load.uniform().interarrival_hi() * num_threads));
151 break;
152 case DETERMINISTIC:
153 random_dist.reset(
154 new DetDist(num_threads / load.determ().offered_load()));
155 break;
156 case PARETO:
157 random_dist.reset(
158 new ParetoDist(load.pareto().interarrival_base() * num_threads,
159 load.pareto().alpha()));
160 break;
161 default:
162 GPR_ASSERT(false);
163 break;
164 }
165
166 interarrival_timer_.init(*random_dist, num_threads);
167 for (size_t i = 0; i < num_threads; i++) {
168 next_time_.push_back(
169 grpc_time_source::now() +
170 std::chrono::duration_cast<grpc_time_source::duration>(
171 interarrival_timer_(i)));
172 }
173 }
174 }
175
176 bool NextIssueTime(int thread_idx, grpc_time* time_delay) {
177 if (closed_loop_) {
178 return false;
179 } else {
180 *time_delay = next_time_[thread_idx];
181 next_time_[thread_idx] +=
182 std::chrono::duration_cast<grpc_time_source::duration>(
183 interarrival_timer_(thread_idx));
184 return true;
185 }
186 }
187
Craig Tiller88568752015-03-04 10:50:43 -0800188 private:
189 class Thread {
190 public:
191 Thread(Client* client, size_t idx)
Vijay Paiab1dba72015-07-31 09:09:09 -0700192 : done_(false),
193 new_(nullptr),
194 client_(client),
195 idx_(idx),
196 impl_(&Thread::ThreadFunc, this) {}
Craig Tiller88568752015-03-04 10:50:43 -0800197
198 ~Thread() {
199 {
200 std::lock_guard<std::mutex> g(mu_);
201 done_ = true;
202 }
203 impl_.join();
204 }
205
206 void BeginSwap(Histogram* n) {
207 std::lock_guard<std::mutex> g(mu_);
208 new_ = n;
209 }
210
211 void EndSwap() {
212 std::unique_lock<std::mutex> g(mu_);
213 cv_.wait(g, [this]() { return new_ == nullptr; });
214 }
215
216 private:
217 Thread(const Thread&);
218 Thread& operator=(const Thread&);
219
vjpaia9e08302015-07-31 07:55:06 -0700220 void ThreadFunc() {
221 for (;;) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700222 // run the loop body
223 bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_);
224 // lock, see if we're done
225 std::lock_guard<std::mutex> g(mu_);
226 if (!thread_still_ok) {
227 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
228 done_ = true;
229 }
230 if (done_) {
231 return;
232 }
233 // check if we're marking, swap out the histogram if so
234 if (new_) {
235 new_->Swap(&histogram_);
236 new_ = nullptr;
237 cv_.notify_one();
238 }
vjpaia9e08302015-07-31 07:55:06 -0700239 }
240 }
241
Craig Tiller88568752015-03-04 10:50:43 -0800242 TestService::Stub* stub_;
243 ClientConfig config_;
244 std::mutex mu_;
245 std::condition_variable cv_;
246 bool done_;
247 Histogram* new_;
248 Histogram histogram_;
Vijay Paiab1dba72015-07-31 09:09:09 -0700249 Client* client_;
vjpaia9e08302015-07-31 07:55:06 -0700250 size_t idx_;
Craig Tiller88568752015-03-04 10:50:43 -0800251 std::thread impl_;
252 };
253
254 std::vector<std::unique_ptr<Thread>> threads_;
255 std::unique_ptr<Timer> timer_;
Vijay Pai372fd872015-06-08 13:30:08 -0700256
257 InterarrivalTimer interarrival_timer_;
258 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800259};
260
Craig Tiller5c8737d2015-05-21 11:42:17 -0700261std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
262std::unique_ptr<Client> CreateSynchronousStreamingClient(
263 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700264std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
265std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800266
267} // namespace testing
268} // namespace grpc
269
270#endif