blob: 28cd32a197422fc78ea2599f69de1a418eb5ce57 [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>
44
Craig Tiller26598a32015-03-02 16:16:00 -080045namespace grpc {
Vijay Pai372fd872015-06-08 13:30:08 -070046
47#if defined(__APPLE__)
48// Specialize Timepoint for high res clock as we need that
49template <>
50class TimePoint<std::chrono::high_resolution_clock::time_point> {
51 public:
52 TimePoint(const std::chrono::high_resolution_clock::time_point& time) {
53 TimepointHR2Timespec(time, &time_);
54 }
55 gpr_timespec raw_time() const { return time_; }
56
57 private:
58 gpr_timespec time_;
59};
60#endif
61
Craig Tiller26598a32015-03-02 16:16:00 -080062namespace testing {
63
Vijay Pai372fd872015-06-08 13:30:08 -070064typedef std::chrono::high_resolution_clock grpc_time_source;
65typedef std::chrono::time_point<grpc_time_source> grpc_time;
66
Craig Tiller26598a32015-03-02 16:16:00 -080067class Client {
68 public:
Vijay Pai372fd872015-06-08 13:30:08 -070069 explicit Client(const ClientConfig& config)
70 : timer_(new Timer), interarrival_timer_() {
Craig Tiller88568752015-03-04 10:50:43 -080071 for (int i = 0; i < config.client_channels(); i++) {
72 channels_.push_back(ClientChannelInfo(
73 config.server_targets(i % config.server_targets_size()), config));
74 }
75 request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
76 request_.set_response_size(config.payload_size());
77 }
Craig Tiller26598a32015-03-02 16:16:00 -080078 virtual ~Client() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -080079
Craig Tiller88568752015-03-04 10:50:43 -080080 ClientStats Mark() {
81 Histogram latencies;
82 std::vector<Histogram> to_merge(threads_.size());
83 for (size_t i = 0; i < threads_.size(); i++) {
84 threads_[i]->BeginSwap(&to_merge[i]);
85 }
86 std::unique_ptr<Timer> timer(new Timer);
87 timer_.swap(timer);
88 for (size_t i = 0; i < threads_.size(); i++) {
89 threads_[i]->EndSwap();
90 latencies.Merge(&to_merge[i]);
91 }
92
93 auto timer_result = timer->Mark();
94
95 ClientStats stats;
96 latencies.FillProto(stats.mutable_latencies());
97 stats.set_time_elapsed(timer_result.wall);
98 stats.set_time_system(timer_result.system);
99 stats.set_time_user(timer_result.user);
100 return stats;
101 }
102
103 protected:
104 SimpleRequest request_;
Vijay Pai372fd872015-06-08 13:30:08 -0700105 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -0800106
107 class ClientChannelInfo {
108 public:
Craig Tillera182bf12015-03-04 13:54:39 -0800109 ClientChannelInfo(const grpc::string& target, const ClientConfig& config)
Craig Tiller88568752015-03-04 10:50:43 -0800110 : channel_(CreateTestChannel(target, config.enable_ssl())),
111 stub_(TestService::NewStub(channel_)) {}
112 ChannelInterface* get_channel() { return channel_.get(); }
113 TestService::Stub* get_stub() { return stub_.get(); }
114
115 private:
116 std::shared_ptr<ChannelInterface> channel_;
117 std::unique_ptr<TestService::Stub> stub_;
118 };
119 std::vector<ClientChannelInfo> channels_;
120
121 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800122 for (size_t i = 0; i < num_threads; i++) {
123 threads_.emplace_back(new Thread(this, i));
124 }
Craig Tiller88568752015-03-04 10:50:43 -0800125 }
126
Craig Tillera182bf12015-03-04 13:54:39 -0800127 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800128
Craig Tiller8a5a6662015-04-09 11:31:28 -0700129 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700130
Vijay Pai372fd872015-06-08 13:30:08 -0700131 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
132 // Set up the load distribution based on the number of threads
133 if (config.load_type() == CLOSED_LOOP) {
134 closed_loop_ = true;
135 } else {
136 closed_loop_ = false;
137
138 std::unique_ptr<RandomDist> random_dist;
139 const auto& load = config.load_params();
140 switch (config.load_type()) {
141 case POISSON:
142 random_dist.reset(
143 new ExpDist(load.poisson().offered_load() / num_threads));
144 break;
145 case UNIFORM:
146 random_dist.reset(
147 new UniformDist(load.uniform().interarrival_lo() * num_threads,
148 load.uniform().interarrival_hi() * num_threads));
149 break;
150 case DETERMINISTIC:
151 random_dist.reset(
152 new DetDist(num_threads / load.determ().offered_load()));
153 break;
154 case PARETO:
155 random_dist.reset(
156 new ParetoDist(load.pareto().interarrival_base() * num_threads,
157 load.pareto().alpha()));
158 break;
159 default:
160 GPR_ASSERT(false);
161 break;
162 }
163
164 interarrival_timer_.init(*random_dist, num_threads);
165 for (size_t i = 0; i < num_threads; i++) {
166 next_time_.push_back(
167 grpc_time_source::now() +
168 std::chrono::duration_cast<grpc_time_source::duration>(
169 interarrival_timer_(i)));
170 }
171 }
172 }
173
174 bool NextIssueTime(int thread_idx, grpc_time* time_delay) {
175 if (closed_loop_) {
176 return false;
177 } else {
178 *time_delay = next_time_[thread_idx];
179 next_time_[thread_idx] +=
180 std::chrono::duration_cast<grpc_time_source::duration>(
181 interarrival_timer_(thread_idx));
182 return true;
183 }
184 }
185
Craig Tiller88568752015-03-04 10:50:43 -0800186 private:
187 class Thread {
188 public:
189 Thread(Client* client, size_t idx)
190 : done_(false),
191 new_(nullptr),
192 impl_([this, idx, client]() {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700193 for (;;) {
194 // run the loop body
195 bool thread_still_ok = client->ThreadFunc(&histogram_, idx);
196 // lock, see if we're done
197 std::lock_guard<std::mutex> g(mu_);
198 if (!thread_still_ok) {
199 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
200 done_ = true;
Craig Tiller88568752015-03-04 10:50:43 -0800201 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700202 if (done_) {
203 return;
204 }
205 // check if we're marking, swap out the histogram if so
206 if (new_) {
207 new_->Swap(&histogram_);
208 new_ = nullptr;
209 cv_.notify_one();
210 }
211 }
212 }) {}
Craig Tiller88568752015-03-04 10:50:43 -0800213
214 ~Thread() {
215 {
216 std::lock_guard<std::mutex> g(mu_);
217 done_ = true;
218 }
219 impl_.join();
220 }
221
222 void BeginSwap(Histogram* n) {
223 std::lock_guard<std::mutex> g(mu_);
224 new_ = n;
225 }
226
227 void EndSwap() {
228 std::unique_lock<std::mutex> g(mu_);
229 cv_.wait(g, [this]() { return new_ == nullptr; });
230 }
231
232 private:
233 Thread(const Thread&);
234 Thread& operator=(const Thread&);
235
236 TestService::Stub* stub_;
237 ClientConfig config_;
238 std::mutex mu_;
239 std::condition_variable cv_;
240 bool done_;
241 Histogram* new_;
242 Histogram histogram_;
243 std::thread impl_;
244 };
245
246 std::vector<std::unique_ptr<Thread>> threads_;
247 std::unique_ptr<Timer> timer_;
Vijay Pai372fd872015-06-08 13:30:08 -0700248
249 InterarrivalTimer interarrival_timer_;
250 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800251};
252
Craig Tiller5c8737d2015-05-21 11:42:17 -0700253std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
254std::unique_ptr<Client> CreateSynchronousStreamingClient(
255 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700256std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
257std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800258
259} // namespace testing
260} // namespace grpc
261
262#endif