blob: f028fc7c0f74643fc10e16183a632a84043732ad [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"
vjpaic6aa60e2015-04-29 10:20:41 -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 {
vjpai3beb20c2015-06-02 13:58:44 -070046
47// Specialize Timepoint for high res clock as we need that
48template <>
49class TimePoint<std::chrono::high_resolution_clock::time_point> {
50 public:
51 TimePoint(const std::chrono::high_resolution_clock::time_point& time) {
52 Timepoint2Timespec(time, &time_);
53 }
54 gpr_timespec raw_time() const { return time_; }
55 private:
56 gpr_timespec time_;
57};
58
Craig Tiller26598a32015-03-02 16:16:00 -080059namespace testing {
60
vjpai3beb20c2015-06-02 13:58:44 -070061typedef std::chrono::high_resolution_clock grpc_time_source;
vjpai924d4592015-06-02 10:26:52 -070062typedef std::chrono::time_point<grpc_time_source> grpc_time;
63
Craig Tiller26598a32015-03-02 16:16:00 -080064class Client {
65 public:
vjpaic6aa60e2015-04-29 10:20:41 -070066 explicit Client(const ClientConfig& config) : timer_(new Timer),
67 interarrival_timer_() {
Craig Tiller88568752015-03-04 10:50:43 -080068 for (int i = 0; i < config.client_channels(); i++) {
69 channels_.push_back(ClientChannelInfo(
70 config.server_targets(i % config.server_targets_size()), config));
71 }
72 request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
73 request_.set_response_size(config.payload_size());
74 }
Craig Tiller26598a32015-03-02 16:16:00 -080075 virtual ~Client() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -080076
Craig Tiller88568752015-03-04 10:50:43 -080077 ClientStats Mark() {
78 Histogram latencies;
79 std::vector<Histogram> to_merge(threads_.size());
80 for (size_t i = 0; i < threads_.size(); i++) {
81 threads_[i]->BeginSwap(&to_merge[i]);
82 }
83 std::unique_ptr<Timer> timer(new Timer);
84 timer_.swap(timer);
85 for (size_t i = 0; i < threads_.size(); i++) {
86 threads_[i]->EndSwap();
87 latencies.Merge(&to_merge[i]);
88 }
89
90 auto timer_result = timer->Mark();
91
92 ClientStats stats;
93 latencies.FillProto(stats.mutable_latencies());
94 stats.set_time_elapsed(timer_result.wall);
95 stats.set_time_system(timer_result.system);
96 stats.set_time_user(timer_result.user);
97 return stats;
98 }
99
100 protected:
101 SimpleRequest request_;
vjpai37f72572015-05-12 10:29:07 -0700102 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -0800103
104 class ClientChannelInfo {
105 public:
Craig Tillera182bf12015-03-04 13:54:39 -0800106 ClientChannelInfo(const grpc::string& target, const ClientConfig& config)
Craig Tiller88568752015-03-04 10:50:43 -0800107 : channel_(CreateTestChannel(target, config.enable_ssl())),
108 stub_(TestService::NewStub(channel_)) {}
109 ChannelInterface* get_channel() { return channel_.get(); }
110 TestService::Stub* get_stub() { return stub_.get(); }
111
112 private:
113 std::shared_ptr<ChannelInterface> channel_;
114 std::unique_ptr<TestService::Stub> stub_;
115 };
116 std::vector<ClientChannelInfo> channels_;
117
118 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800119 for (size_t i = 0; i < num_threads; i++) {
120 threads_.emplace_back(new Thread(this, i));
121 }
Craig Tiller88568752015-03-04 10:50:43 -0800122 }
123
Craig Tillera182bf12015-03-04 13:54:39 -0800124 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800125
Craig Tiller8a5a6662015-04-09 11:31:28 -0700126 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
Vijay Pai85594852015-06-03 11:01:25 -0700127
vjpaic6aa60e2015-04-29 10:20:41 -0700128 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
129 // Set up the load distribution based on the number of threads
130 if (config.load_type() == CLOSED_LOOP) {
131 closed_loop_ = true;
132 }
133 else {
134 closed_loop_ = false;
Craig Tiller88568752015-03-04 10:50:43 -0800135
vjpaic6aa60e2015-04-29 10:20:41 -0700136 std::unique_ptr<RandomDist> random_dist;
137 auto& load = config.load_params();
138 switch (config.load_type()) {
139 case POISSON:
140 random_dist.reset
141 (new ExpDist(load.poisson().offered_load()/num_threads));
142 break;
143 case UNIFORM:
144 random_dist.reset
145 (new UniformDist(load.uniform().interarrival_lo()*num_threads,
146 load.uniform().interarrival_hi()*num_threads));
147 break;
148 case DETERMINISTIC:
149 random_dist.reset
150 (new DetDist(num_threads/load.determ().offered_load()));
151 break;
152 case PARETO:
153 random_dist.reset
154 (new ParetoDist(load.pareto().interarrival_base()*num_threads,
155 load.pareto().alpha()));
156 break;
157 default:
158 GPR_ASSERT(false);
159 break;
160 }
161
162 interarrival_timer_.init(*random_dist, num_threads);
163 for (size_t i = 0; i<num_threads; i++) {
vjpai924d4592015-06-02 10:26:52 -0700164 next_time_.push_back(grpc_time_source::now() +
165 std::chrono::duration_cast<grpc_time_source::duration>(interarrival_timer_(i)));
vjpaic6aa60e2015-04-29 10:20:41 -0700166 }
167 }
168 }
vjpai924d4592015-06-02 10:26:52 -0700169 bool NextIssueTime(int thread_idx, grpc_time *time_delay) {
vjpaic6aa60e2015-04-29 10:20:41 -0700170 if (closed_loop_) {
171 return false;
172 }
173 else {
174 *time_delay = next_time_[thread_idx];
vjpai924d4592015-06-02 10:26:52 -0700175 next_time_[thread_idx] += std::chrono::duration_cast<grpc_time_source::duration>(interarrival_timer_(thread_idx));
vjpaic6aa60e2015-04-29 10:20:41 -0700176 return true;
177 }
178 }
179
Craig Tiller88568752015-03-04 10:50:43 -0800180 private:
181 class Thread {
182 public:
183 Thread(Client* client, size_t idx)
184 : done_(false),
185 new_(nullptr),
186 impl_([this, idx, client]() {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700187 for (;;) {
188 // run the loop body
189 bool thread_still_ok = client->ThreadFunc(&histogram_, idx);
190 // lock, see if we're done
191 std::lock_guard<std::mutex> g(mu_);
192 if (!thread_still_ok) {
193 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
194 done_ = true;
Craig Tiller88568752015-03-04 10:50:43 -0800195 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700196 if (done_) {
197 return;
198 }
199 // check if we're marking, swap out the histogram if so
200 if (new_) {
201 new_->Swap(&histogram_);
202 new_ = nullptr;
203 cv_.notify_one();
204 }
205 }
206 }) {}
Craig Tiller88568752015-03-04 10:50:43 -0800207
208 ~Thread() {
209 {
210 std::lock_guard<std::mutex> g(mu_);
211 done_ = true;
212 }
213 impl_.join();
214 }
215
216 void BeginSwap(Histogram* n) {
217 std::lock_guard<std::mutex> g(mu_);
218 new_ = n;
219 }
220
221 void EndSwap() {
222 std::unique_lock<std::mutex> g(mu_);
223 cv_.wait(g, [this]() { return new_ == nullptr; });
224 }
225
226 private:
227 Thread(const Thread&);
228 Thread& operator=(const Thread&);
229
230 TestService::Stub* stub_;
231 ClientConfig config_;
232 std::mutex mu_;
233 std::condition_variable cv_;
234 bool done_;
235 Histogram* new_;
236 Histogram histogram_;
237 std::thread impl_;
238 };
239
240 std::vector<std::unique_ptr<Thread>> threads_;
241 std::unique_ptr<Timer> timer_;
vjpaic6aa60e2015-04-29 10:20:41 -0700242
vjpaic6aa60e2015-04-29 10:20:41 -0700243 InterarrivalTimer interarrival_timer_;
vjpai924d4592015-06-02 10:26:52 -0700244 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800245};
246
Craig Tiller5c8737d2015-05-21 11:42:17 -0700247std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
248std::unique_ptr<Client> CreateSynchronousStreamingClient(
249 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700250std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
251std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800252
253} // namespace testing
254} // namespace grpc
255
256#endif