blob: dd37b88fb4c44aeefea286f1002ba0cc09529aa8 [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 {
46namespace testing {
47
vjpai924d4592015-06-02 10:26:52 -070048typedef std::chrono::system_clock grpc_time_source;
49typedef std::chrono::time_point<grpc_time_source> grpc_time;
50
Craig Tiller26598a32015-03-02 16:16:00 -080051class Client {
52 public:
vjpaic6aa60e2015-04-29 10:20:41 -070053 explicit Client(const ClientConfig& config) : timer_(new Timer),
54 interarrival_timer_() {
Craig Tiller88568752015-03-04 10:50:43 -080055 for (int i = 0; i < config.client_channels(); i++) {
56 channels_.push_back(ClientChannelInfo(
57 config.server_targets(i % config.server_targets_size()), config));
58 }
59 request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
60 request_.set_response_size(config.payload_size());
61 }
Craig Tiller26598a32015-03-02 16:16:00 -080062 virtual ~Client() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -080063
Craig Tiller88568752015-03-04 10:50:43 -080064 ClientStats Mark() {
65 Histogram latencies;
66 std::vector<Histogram> to_merge(threads_.size());
67 for (size_t i = 0; i < threads_.size(); i++) {
68 threads_[i]->BeginSwap(&to_merge[i]);
69 }
70 std::unique_ptr<Timer> timer(new Timer);
71 timer_.swap(timer);
72 for (size_t i = 0; i < threads_.size(); i++) {
73 threads_[i]->EndSwap();
74 latencies.Merge(&to_merge[i]);
75 }
76
77 auto timer_result = timer->Mark();
78
79 ClientStats stats;
80 latencies.FillProto(stats.mutable_latencies());
81 stats.set_time_elapsed(timer_result.wall);
82 stats.set_time_system(timer_result.system);
83 stats.set_time_user(timer_result.user);
84 return stats;
85 }
86
87 protected:
88 SimpleRequest request_;
vjpai37f72572015-05-12 10:29:07 -070089 bool closed_loop_;
Craig Tiller88568752015-03-04 10:50:43 -080090
91 class ClientChannelInfo {
92 public:
Craig Tillera182bf12015-03-04 13:54:39 -080093 ClientChannelInfo(const grpc::string& target, const ClientConfig& config)
Craig Tiller88568752015-03-04 10:50:43 -080094 : channel_(CreateTestChannel(target, config.enable_ssl())),
95 stub_(TestService::NewStub(channel_)) {}
96 ChannelInterface* get_channel() { return channel_.get(); }
97 TestService::Stub* get_stub() { return stub_.get(); }
98
99 private:
100 std::shared_ptr<ChannelInterface> channel_;
101 std::unique_ptr<TestService::Stub> stub_;
102 };
103 std::vector<ClientChannelInfo> channels_;
104
105 void StartThreads(size_t num_threads) {
Craig Tillera182bf12015-03-04 13:54:39 -0800106 for (size_t i = 0; i < num_threads; i++) {
107 threads_.emplace_back(new Thread(this, i));
108 }
Craig Tiller88568752015-03-04 10:50:43 -0800109 }
110
Craig Tillera182bf12015-03-04 13:54:39 -0800111 void EndThreads() { threads_.clear(); }
Craig Tiller88568752015-03-04 10:50:43 -0800112
Craig Tiller8a5a6662015-04-09 11:31:28 -0700113 virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0;
vjpaic6aa60e2015-04-29 10:20:41 -0700114
115 void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
116 // Set up the load distribution based on the number of threads
117 if (config.load_type() == CLOSED_LOOP) {
118 closed_loop_ = true;
119 }
120 else {
121 closed_loop_ = false;
Craig Tiller88568752015-03-04 10:50:43 -0800122
vjpaic6aa60e2015-04-29 10:20:41 -0700123 std::unique_ptr<RandomDist> random_dist;
124 auto& load = config.load_params();
125 switch (config.load_type()) {
126 case POISSON:
127 random_dist.reset
128 (new ExpDist(load.poisson().offered_load()/num_threads));
129 break;
130 case UNIFORM:
131 random_dist.reset
132 (new UniformDist(load.uniform().interarrival_lo()*num_threads,
133 load.uniform().interarrival_hi()*num_threads));
134 break;
135 case DETERMINISTIC:
136 random_dist.reset
137 (new DetDist(num_threads/load.determ().offered_load()));
138 break;
139 case PARETO:
140 random_dist.reset
141 (new ParetoDist(load.pareto().interarrival_base()*num_threads,
142 load.pareto().alpha()));
143 break;
144 default:
145 GPR_ASSERT(false);
146 break;
147 }
148
149 interarrival_timer_.init(*random_dist, num_threads);
150 for (size_t i = 0; i<num_threads; i++) {
vjpai924d4592015-06-02 10:26:52 -0700151 next_time_.push_back(grpc_time_source::now() +
152 std::chrono::duration_cast<grpc_time_source::duration>(interarrival_timer_(i)));
vjpaic6aa60e2015-04-29 10:20:41 -0700153 }
154 }
155 }
vjpai924d4592015-06-02 10:26:52 -0700156 bool NextIssueTime(int thread_idx, grpc_time *time_delay) {
vjpaic6aa60e2015-04-29 10:20:41 -0700157 if (closed_loop_) {
158 return false;
159 }
160 else {
161 *time_delay = next_time_[thread_idx];
vjpai924d4592015-06-02 10:26:52 -0700162 next_time_[thread_idx] += std::chrono::duration_cast<grpc_time_source::duration>(interarrival_timer_(thread_idx));
vjpaic6aa60e2015-04-29 10:20:41 -0700163 return true;
164 }
165 }
166
Craig Tiller88568752015-03-04 10:50:43 -0800167 private:
168 class Thread {
169 public:
170 Thread(Client* client, size_t idx)
171 : done_(false),
172 new_(nullptr),
173 impl_([this, idx, client]() {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700174 for (;;) {
175 // run the loop body
176 bool thread_still_ok = client->ThreadFunc(&histogram_, idx);
177 // lock, see if we're done
178 std::lock_guard<std::mutex> g(mu_);
179 if (!thread_still_ok) {
180 gpr_log(GPR_ERROR, "Finishing client thread due to RPC error");
181 done_ = true;
Craig Tiller88568752015-03-04 10:50:43 -0800182 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700183 if (done_) {
184 return;
185 }
186 // check if we're marking, swap out the histogram if so
187 if (new_) {
188 new_->Swap(&histogram_);
189 new_ = nullptr;
190 cv_.notify_one();
191 }
192 }
193 }) {}
Craig Tiller88568752015-03-04 10:50:43 -0800194
195 ~Thread() {
196 {
197 std::lock_guard<std::mutex> g(mu_);
198 done_ = true;
199 }
200 impl_.join();
201 }
202
203 void BeginSwap(Histogram* n) {
204 std::lock_guard<std::mutex> g(mu_);
205 new_ = n;
206 }
207
208 void EndSwap() {
209 std::unique_lock<std::mutex> g(mu_);
210 cv_.wait(g, [this]() { return new_ == nullptr; });
211 }
212
213 private:
214 Thread(const Thread&);
215 Thread& operator=(const Thread&);
216
217 TestService::Stub* stub_;
218 ClientConfig config_;
219 std::mutex mu_;
220 std::condition_variable cv_;
221 bool done_;
222 Histogram* new_;
223 Histogram histogram_;
224 std::thread impl_;
225 };
226
227 std::vector<std::unique_ptr<Thread>> threads_;
228 std::unique_ptr<Timer> timer_;
vjpaic6aa60e2015-04-29 10:20:41 -0700229
vjpaic6aa60e2015-04-29 10:20:41 -0700230 InterarrivalTimer interarrival_timer_;
vjpai924d4592015-06-02 10:26:52 -0700231 std::vector<grpc_time> next_time_;
Craig Tiller26598a32015-03-02 16:16:00 -0800232};
233
Craig Tiller5c8737d2015-05-21 11:42:17 -0700234std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args);
235std::unique_ptr<Client> CreateSynchronousStreamingClient(
236 const ClientConfig& args);
vjpai46f65232015-03-23 10:10:27 -0700237std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args);
238std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args);
Craig Tiller26598a32015-03-02 16:16:00 -0800239
240} // namespace testing
241} // namespace grpc
242
243#endif