blob: 087ea75bf408cbbf23416e94640abb7e9d1bba28 [file] [log] [blame]
vjpaidea740f2015-02-26 16:35:35 -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#include <cassert>
Vijay Pai372fd872015-06-08 13:30:08 -070035#include <forward_list>
vjpaidea740f2015-02-26 16:35:35 -080036#include <functional>
Vijay Pai372fd872015-06-08 13:30:08 -070037#include <list>
vjpaidea740f2015-02-26 16:35:35 -080038#include <memory>
Vijay Pai372fd872015-06-08 13:30:08 -070039#include <mutex>
Vijay Pai18c04772016-01-04 09:52:10 -080040#include <sstream>
vjpaidea740f2015-02-26 16:35:35 -080041#include <string>
42#include <thread>
43#include <vector>
vjpaidea740f2015-02-26 16:35:35 -080044
vjpaidea740f2015-02-26 16:35:35 -080045#include <gflags/gflags.h>
vjpaidea740f2015-02-26 16:35:35 -080046#include <grpc++/client_context.h>
Vijay Paie4886682015-12-30 11:56:19 -080047#include <grpc++/generic/generic_stub.h>
Vijay Pai18c04772016-01-04 09:52:10 -080048#include <grpc/grpc.h>
49#include <grpc/support/histogram.h>
50#include <grpc/support/log.h>
yang-g9e2f90c2015-08-21 15:35:03 -070051
Craig Tiller88568752015-03-04 10:50:43 -080052#include "test/cpp/qps/client.h"
Vijay Pai18c04772016-01-04 09:52:10 -080053#include "test/cpp/qps/timer.h"
yang-g9e2f90c2015-08-21 15:35:03 -070054#include "test/cpp/util/create_test_channel.h"
vjpaid08a7382015-11-02 16:45:08 -080055#include "test/proto/benchmarks/services.grpc.pb.h"
vjpaidea740f2015-02-26 16:35:35 -080056
Craig Tiller88568752015-03-04 10:50:43 -080057namespace grpc {
58namespace testing {
vjpaidea740f2015-02-26 16:35:35 -080059
Vijay Pai372fd872015-06-08 13:30:08 -070060typedef std::list<grpc_time> deadline_list;
61
Vijay Pai64ac47f2015-02-26 17:59:51 -080062class ClientRpcContext {
63 public:
Yang Gaob6d57e72015-06-09 22:11:12 -070064 explicit ClientRpcContext(int ch) : channel_id_(ch) {}
Vijay Pai64ac47f2015-02-26 17:59:51 -080065 virtual ~ClientRpcContext() {}
vjpai46f65232015-03-23 10:10:27 -070066 // next state, return false if done. Collect stats when appropriate
Vijay Pai1da729a2015-03-23 12:52:56 -070067 virtual bool RunNextState(bool, Histogram* hist) = 0;
Vijay Pai372fd872015-06-08 13:30:08 -070068 virtual ClientRpcContext* StartNewClone() = 0;
Yang Gao6baa9b62015-03-17 10:49:39 -070069 static void* tag(ClientRpcContext* c) { return reinterpret_cast<void*>(c); }
70 static ClientRpcContext* detag(void* t) {
71 return reinterpret_cast<ClientRpcContext*>(t);
Vijay Pai64ac47f2015-02-26 17:59:51 -080072 }
Vijay Pai372fd872015-06-08 13:30:08 -070073
74 deadline_list::iterator deadline_posn() const { return deadline_posn_; }
75 void set_deadline_posn(const deadline_list::iterator& it) {
76 deadline_posn_ = it;
77 }
78 virtual void Start(CompletionQueue* cq) = 0;
79 int channel_id() const { return channel_id_; }
80
81 protected:
82 int channel_id_;
83
84 private:
85 deadline_list::iterator deadline_posn_;
Vijay Pai64ac47f2015-02-26 17:59:51 -080086};
Craig Tiller88568752015-03-04 10:50:43 -080087
Vijay Pai64ac47f2015-02-26 17:59:51 -080088template <class RequestType, class ResponseType>
89class ClientRpcContextUnaryImpl : public ClientRpcContext {
90 public:
Vijay Paicf3fb092015-06-05 03:41:30 -070091 ClientRpcContextUnaryImpl(
vjpai119c1032015-10-29 01:21:04 -070092 int channel_id, BenchmarkService::Stub* stub, const RequestType& req,
vjpai4e1e1bc2015-02-27 23:47:12 -080093 std::function<
94 std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>(
vjpai119c1032015-10-29 01:21:04 -070095 BenchmarkService::Stub*, grpc::ClientContext*, const RequestType&,
Vijay Pai18c04772016-01-04 09:52:10 -080096 CompletionQueue*)>
97 start_req,
Yang Gao6baa9b62015-03-17 10:49:39 -070098 std::function<void(grpc::Status, ResponseType*)> on_done)
Vijay Pai372fd872015-06-08 13:30:08 -070099 : ClientRpcContext(channel_id),
100 context_(),
Craig Tiller88568752015-03-04 10:50:43 -0800101 stub_(stub),
Vijay Pai64ac47f2015-02-26 17:59:51 -0800102 req_(req),
103 response_(),
Craig Tiller3676b382015-05-06 13:01:05 -0700104 next_state_(&ClientRpcContextUnaryImpl::RespDone),
Craig Tillera182bf12015-03-04 13:54:39 -0800105 callback_(on_done),
Vijay Pai372fd872015-06-08 13:30:08 -0700106 start_req_(start_req) {}
107 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
108 start_ = Timer::Now();
109 response_reader_ = start_req_(stub_, &context_, req_, cq);
Craig Tiller3676b382015-05-06 13:01:05 -0700110 response_reader_->Finish(&response_, &status_, ClientRpcContext::tag(this));
111 }
vjpai3c110662015-02-27 07:17:16 -0800112 ~ClientRpcContextUnaryImpl() GRPC_OVERRIDE {}
Vijay Pai1da729a2015-03-23 12:52:56 -0700113 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -0700114 bool ret = (this->*next_state_)(ok);
115 if (!ret) {
116 hist->Add((Timer::Now() - start_) * 1e9);
117 }
118 return ret;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800119 }
120
Vijay Pai372fd872015-06-08 13:30:08 -0700121 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
122 return new ClientRpcContextUnaryImpl(channel_id_, stub_, req_, start_req_,
123 callback_);
Craig Tilleref638392015-03-04 12:23:12 -0800124 }
125
Vijay Pai64ac47f2015-02-26 17:59:51 -0800126 private:
vjpai46f65232015-03-23 10:10:27 -0700127 bool RespDone(bool) {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800128 next_state_ = &ClientRpcContextUnaryImpl::DoCallBack;
129 return false;
130 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700131 bool DoCallBack(bool) {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800132 callback_(status_, &response_);
Vijay Pai372fd872015-06-08 13:30:08 -0700133 return true; // we're done, this'll be ignored
Vijay Pai64ac47f2015-02-26 17:59:51 -0800134 }
135 grpc::ClientContext context_;
vjpai119c1032015-10-29 01:21:04 -0700136 BenchmarkService::Stub* stub_;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800137 RequestType req_;
138 ResponseType response_;
vjpai46f65232015-03-23 10:10:27 -0700139 bool (ClientRpcContextUnaryImpl::*next_state_)(bool);
Yang Gao6baa9b62015-03-17 10:49:39 -0700140 std::function<void(grpc::Status, ResponseType*)> callback_;
Craig Tillera182bf12015-03-04 13:54:39 -0800141 std::function<std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>(
vjpai119c1032015-10-29 01:21:04 -0700142 BenchmarkService::Stub*, grpc::ClientContext*, const RequestType&,
Vijay Pai18c04772016-01-04 09:52:10 -0800143 CompletionQueue*)>
144 start_req_;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800145 grpc::Status status_;
146 double start_;
147 std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>
148 response_reader_;
149};
vjpaidea740f2015-02-26 16:35:35 -0800150
Vijay Pai372fd872015-06-08 13:30:08 -0700151typedef std::forward_list<ClientRpcContext*> context_list;
152
vjpaib6df94a2015-11-30 15:52:50 -0800153template <class StubType, class RequestType>
Vijay Paie4886682015-12-30 11:56:19 -0800154class AsyncClient : public ClientImpl<StubType, RequestType> {
155 // Specify which protected members we are using since there is no
156 // member name resolution until the template types are fully resolved
Craig Tiller88568752015-03-04 10:50:43 -0800157 public:
Vijay Paie4886682015-12-30 11:56:19 -0800158 using Client::SetupLoadTest;
159 using Client::NextIssueTime;
160 using Client::closed_loop_;
Vijay Pai18c04772016-01-04 09:52:10 -0800161 using ClientImpl<StubType, RequestType>::channels_;
162 using ClientImpl<StubType, RequestType>::request_;
vjpaib6df94a2015-11-30 15:52:50 -0800163 AsyncClient(
Vijay Pai372fd872015-06-08 13:30:08 -0700164 const ClientConfig& config,
Vijay Pai18c04772016-01-04 09:52:10 -0800165 std::function<ClientRpcContext*(int, StubType*, const RequestType&)>
166 setup_ctx,
167 std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)>
168 create_stub)
169 : ClientImpl<StubType, RequestType>(config, create_stub),
Vijay Pai90e73692015-08-05 19:15:36 -0700170 channel_lock_(new std::mutex[config.client_channels()]),
Vijay Pai372fd872015-06-08 13:30:08 -0700171 contexts_(config.client_channels()),
172 max_outstanding_per_channel_(config.outstanding_rpcs_per_channel()),
173 channel_count_(config.client_channels()),
174 pref_channel_inc_(config.async_client_threads()) {
175 SetupLoadTest(config, config.async_client_threads());
176
Craig Tiller88568752015-03-04 10:50:43 -0800177 for (int i = 0; i < config.async_client_threads(); i++) {
178 cli_cqs_.emplace_back(new CompletionQueue);
Vijay Pai372fd872015-06-08 13:30:08 -0700179 if (!closed_loop_) {
180 rpc_deadlines_.emplace_back();
181 next_channel_.push_back(i % channel_count_);
Vijay Paie2980cd2015-07-01 09:38:20 -0700182 issue_allowed_.emplace_back(true);
Vijay Pai372fd872015-06-08 13:30:08 -0700183
184 grpc_time next_issue;
185 NextIssueTime(i, &next_issue);
186 next_issue_.push_back(next_issue);
187 }
Craig Tiller88568752015-03-04 10:50:43 -0800188 }
Vijay Pai372fd872015-06-08 13:30:08 -0700189
vjpai6a608022015-05-18 09:16:53 -0700190 int t = 0;
191 for (int i = 0; i < config.outstanding_rpcs_per_channel(); i++) {
Vijay Pai372fd872015-06-08 13:30:08 -0700192 for (int ch = 0; ch < channel_count_; ch++) {
Vijay Pai7b172b22015-06-05 02:03:18 -0700193 auto* cq = cli_cqs_[t].get();
194 t = (t + 1) % cli_cqs_.size();
Vijay Pai372fd872015-06-08 13:30:08 -0700195 auto ctx = setup_ctx(ch, channels_[ch].get_stub(), request_);
196 if (closed_loop_) {
197 ctx->Start(cq);
198 } else {
199 contexts_[ch].push_front(ctx);
200 }
Craig Tiller88568752015-03-04 10:50:43 -0800201 }
202 }
Craig Tiller88568752015-03-04 10:50:43 -0800203 }
Vijay Paie10ebf152015-04-30 13:12:31 -0700204 virtual ~AsyncClient() {
Vijay Pai82dd80a2015-03-24 10:36:08 -0700205 for (auto cq = cli_cqs_.begin(); cq != cli_cqs_.end(); cq++) {
206 (*cq)->Shutdown();
Yang Gao6baa9b62015-03-17 10:49:39 -0700207 void* got_tag;
Craig Tilleref638392015-03-04 12:23:12 -0800208 bool ok;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700209 while ((*cq)->Next(&got_tag, &ok)) {
Craig Tilleref638392015-03-04 12:23:12 -0800210 delete ClientRpcContext::detag(got_tag);
211 }
212 }
Vijay Pai56061992015-07-01 13:39:48 -0700213 // Now clear out all the pre-allocated idle contexts
214 for (int ch = 0; ch < channel_count_; ch++) {
Vijay Pai13735d52015-07-01 14:00:08 -0700215 while (!contexts_[ch].empty()) {
Vijay Pai56061992015-07-01 13:39:48 -0700216 // Get an idle context from the front of the list
217 auto* ctx = *(contexts_[ch].begin());
218 contexts_[ch].pop_front();
219 delete ctx;
220 }
221 }
Vijay Paieed63fa2015-08-05 23:08:34 +0000222 delete[] channel_lock_;
Craig Tilleref638392015-03-04 12:23:12 -0800223 }
224
Craig Tiller5c8737d2015-05-21 11:42:17 -0700225 bool ThreadFunc(Histogram* histogram,
226 size_t thread_idx) GRPC_OVERRIDE GRPC_FINAL {
Yang Gao6baa9b62015-03-17 10:49:39 -0700227 void* got_tag;
Craig Tiller88568752015-03-04 10:50:43 -0800228 bool ok;
Vijay Pai372fd872015-06-08 13:30:08 -0700229 grpc_time deadline, short_deadline;
230 if (closed_loop_) {
231 deadline = grpc_time_source::now() + std::chrono::seconds(1);
232 short_deadline = deadline;
233 } else {
234 if (rpc_deadlines_[thread_idx].empty()) {
235 deadline = grpc_time_source::now() + std::chrono::seconds(1);
236 } else {
237 deadline = *(rpc_deadlines_[thread_idx].begin());
238 }
239 short_deadline =
240 issue_allowed_[thread_idx] ? next_issue_[thread_idx] : deadline;
241 }
242
243 bool got_event;
244
245 switch (cli_cqs_[thread_idx]->AsyncNext(&got_tag, &ok, short_deadline)) {
Vijay Paicf3fb092015-06-05 03:41:30 -0700246 case CompletionQueue::SHUTDOWN:
247 return false;
vjpai37f72572015-05-12 10:29:07 -0700248 case CompletionQueue::TIMEOUT:
Vijay Pai372fd872015-06-08 13:30:08 -0700249 got_event = false;
250 break;
vjpai37f72572015-05-12 10:29:07 -0700251 case CompletionQueue::GOT_EVENT:
Vijay Pai372fd872015-06-08 13:30:08 -0700252 got_event = true;
253 break;
254 default:
255 GPR_ASSERT(false);
Vijay Pai9dc5c152015-06-03 11:34:53 -0700256 break;
Craig Tiller1c61af72015-04-09 12:08:44 -0700257 }
Vijay Pai372fd872015-06-08 13:30:08 -0700258 if (got_event) {
259 ClientRpcContext* ctx = ClientRpcContext::detag(got_tag);
260 if (ctx->RunNextState(ok, histogram) == false) {
261 // call the callback and then clone the ctx
262 ctx->RunNextState(ok, histogram);
263 ClientRpcContext* clone_ctx = ctx->StartNewClone();
264 if (closed_loop_) {
265 clone_ctx->Start(cli_cqs_[thread_idx].get());
266 } else {
267 // Remove the entry from the rpc deadlines list
268 rpc_deadlines_[thread_idx].erase(ctx->deadline_posn());
269 // Put the clone_ctx in the list of idle contexts for this channel
270 // Under lock
271 int ch = clone_ctx->channel_id();
272 std::lock_guard<std::mutex> g(channel_lock_[ch]);
273 contexts_[ch].push_front(clone_ctx);
274 }
275 // delete the old version
276 delete ctx;
277 }
278 if (!closed_loop_)
279 issue_allowed_[thread_idx] =
280 true; // may be ok now even if it hadn't been
281 }
282 if (!closed_loop_ && issue_allowed_[thread_idx] &&
283 grpc_time_source::now() >= next_issue_[thread_idx]) {
284 // Attempt to issue
285 bool issued = false;
286 for (int num_attempts = 0, channel_attempt = next_channel_[thread_idx];
287 num_attempts < channel_count_ && !issued; num_attempts++) {
288 bool can_issue = false;
289 ClientRpcContext* ctx = nullptr;
290 {
291 std::lock_guard<std::mutex> g(channel_lock_[channel_attempt]);
292 if (!contexts_[channel_attempt].empty()) {
293 // Get an idle context from the front of the list
294 ctx = *(contexts_[channel_attempt].begin());
295 contexts_[channel_attempt].pop_front();
296 can_issue = true;
297 }
298 }
299 if (can_issue) {
300 // do the work to issue
301 rpc_deadlines_[thread_idx].emplace_back(grpc_time_source::now() +
302 std::chrono::seconds(1));
303 auto it = rpc_deadlines_[thread_idx].end();
304 --it;
305 ctx->set_deadline_posn(it);
306 ctx->Start(cli_cqs_[thread_idx].get());
307 issued = true;
308 // If we did issue, then next time, try our thread's next
309 // preferred channel
310 next_channel_[thread_idx] += pref_channel_inc_;
311 if (next_channel_[thread_idx] >= channel_count_)
312 next_channel_[thread_idx] = (thread_idx % channel_count_);
313 } else {
314 // Do a modular increment of channel attempt if we couldn't issue
315 channel_attempt = (channel_attempt + 1) % channel_count_;
316 }
317 }
318 if (issued) {
319 // We issued one; see when we can issue the next
320 grpc_time next_issue;
321 NextIssueTime(thread_idx, &next_issue);
322 next_issue_[thread_idx] = next_issue;
323 } else {
324 issue_allowed_[thread_idx] = false;
325 }
326 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700327 return true;
Craig Tiller88568752015-03-04 10:50:43 -0800328 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700329
Vijay Paie10ebf152015-04-30 13:12:31 -0700330 private:
Vijay Paiab1dba72015-07-31 09:09:09 -0700331 class boolean { // exists only to avoid data-race on vector<bool>
Vijay Paie2980cd2015-07-01 09:38:20 -0700332 public:
Vijay Paiab1dba72015-07-31 09:09:09 -0700333 boolean() : val_(false) {}
334 boolean(bool b) : val_(b) {}
335 operator bool() const { return val_; }
336 boolean& operator=(bool b) {
337 val_ = b;
338 return *this;
339 }
340
Vijay Paie2980cd2015-07-01 09:38:20 -0700341 private:
342 bool val_;
343 };
Craig Tiller88568752015-03-04 10:50:43 -0800344 std::vector<std::unique_ptr<CompletionQueue>> cli_cqs_;
Vijay Pai372fd872015-06-08 13:30:08 -0700345
346 std::vector<deadline_list> rpc_deadlines_; // per thread deadlines
Vijay Paiab1dba72015-07-31 09:09:09 -0700347 std::vector<int> next_channel_; // per thread round-robin channel ctr
348 std::vector<boolean> issue_allowed_; // may this thread attempt to issue
349 std::vector<grpc_time> next_issue_; // when should it issue?
Vijay Pai372fd872015-06-08 13:30:08 -0700350
Vijay Pai90e73692015-08-05 19:15:36 -0700351 std::mutex*
352 channel_lock_; // a vector, but avoid std::vector for old compilers
Vijay Pai372fd872015-06-08 13:30:08 -0700353 std::vector<context_list> contexts_; // per-channel list of idle contexts
354 int max_outstanding_per_channel_;
355 int channel_count_;
356 int pref_channel_inc_;
Craig Tiller88568752015-03-04 10:50:43 -0800357};
358
Vijay Pai18c04772016-01-04 09:52:10 -0800359static std::unique_ptr<BenchmarkService::Stub> BenchmarkStubCreator(
360 std::shared_ptr<Channel> ch) {
361 return BenchmarkService::NewStub(ch);
362}
363
364class AsyncUnaryClient GRPC_FINAL
365 : public AsyncClient<BenchmarkService::Stub, SimpleRequest> {
Vijay Paie10ebf152015-04-30 13:12:31 -0700366 public:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700367 explicit AsyncUnaryClient(const ClientConfig& config)
Vijay Pai18c04772016-01-04 09:52:10 -0800368 : AsyncClient(config, SetupCtx, BenchmarkStubCreator) {
Vijay Paie10ebf152015-04-30 13:12:31 -0700369 StartThreads(config.async_client_threads());
370 }
371 ~AsyncUnaryClient() GRPC_OVERRIDE { EndThreads(); }
Vijay Paicf3fb092015-06-05 03:41:30 -0700372
373 private:
vjpai09d0b0c2015-07-31 08:39:54 -0700374 static void CheckDone(grpc::Status s, SimpleResponse* response) {}
375 static std::unique_ptr<grpc::ClientAsyncResponseReader<SimpleResponse>>
vjpai119c1032015-10-29 01:21:04 -0700376 StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx,
Vijay Paiab1dba72015-07-31 09:09:09 -0700377 const SimpleRequest& request, CompletionQueue* cq) {
vjpai09d0b0c2015-07-31 08:39:54 -0700378 return stub->AsyncUnaryCall(ctx, request, cq);
379 };
Vijay Paice846702015-11-04 00:30:12 -0800380 static ClientRpcContext* SetupCtx(int channel_id,
381 BenchmarkService::Stub* stub,
Vijay Pai372fd872015-06-08 13:30:08 -0700382 const SimpleRequest& req) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700383 return new ClientRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
384 channel_id, stub, req, AsyncUnaryClient::StartReq,
385 AsyncUnaryClient::CheckDone);
Vijay Paie10ebf152015-04-30 13:12:31 -0700386 }
387};
388
vjpai46f65232015-03-23 10:10:27 -0700389template <class RequestType, class ResponseType>
390class ClientRpcContextStreamingImpl : public ClientRpcContext {
391 public:
Vijay Paicf3fb092015-06-05 03:41:30 -0700392 ClientRpcContextStreamingImpl(
vjpai119c1032015-10-29 01:21:04 -0700393 int channel_id, BenchmarkService::Stub* stub, const RequestType& req,
Vijay Paice846702015-11-04 00:30:12 -0800394 std::function<std::unique_ptr<
395 grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>(
396 BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*,
Vijay Pai18c04772016-01-04 09:52:10 -0800397 void*)>
398 start_req,
Craig Tiller5c8737d2015-05-21 11:42:17 -0700399 std::function<void(grpc::Status, ResponseType*)> on_done)
Vijay Pai372fd872015-06-08 13:30:08 -0700400 : ClientRpcContext(channel_id),
401 context_(),
vjpai46f65232015-03-23 10:10:27 -0700402 stub_(stub),
403 req_(req),
404 response_(),
405 next_state_(&ClientRpcContextStreamingImpl::ReqSent),
406 callback_(on_done),
407 start_req_(start_req),
Vijay Pai372fd872015-06-08 13:30:08 -0700408 start_(Timer::Now()) {}
vjpai46f65232015-03-23 10:10:27 -0700409 ~ClientRpcContextStreamingImpl() GRPC_OVERRIDE {}
Craig Tiller5c8737d2015-05-21 11:42:17 -0700410 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -0700411 return (this->*next_state_)(ok, hist);
412 }
Vijay Pai372fd872015-06-08 13:30:08 -0700413 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
414 return new ClientRpcContextStreamingImpl(channel_id_, stub_, req_,
415 start_req_, callback_);
416 }
417 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
418 stream_ = start_req_(stub_, &context_, cq, ClientRpcContext::tag(this));
Vijay Pai7b172b22015-06-05 02:03:18 -0700419 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700420
vjpai46f65232015-03-23 10:10:27 -0700421 private:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700422 bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); }
vjpai46f65232015-03-23 10:10:27 -0700423 bool StartWrite(bool ok) {
424 if (!ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700425 return (false);
vjpai46f65232015-03-23 10:10:27 -0700426 }
427 start_ = Timer::Now();
428 next_state_ = &ClientRpcContextStreamingImpl::WriteDone;
429 stream_->Write(req_, ClientRpcContext::tag(this));
430 return true;
431 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700432 bool WriteDone(bool ok, Histogram*) {
vjpai46f65232015-03-23 10:10:27 -0700433 if (!ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700434 return (false);
vjpai46f65232015-03-23 10:10:27 -0700435 }
436 next_state_ = &ClientRpcContextStreamingImpl::ReadDone;
Vijay Paie10ebf152015-04-30 13:12:31 -0700437 stream_->Read(&response_, ClientRpcContext::tag(this));
vjpai46f65232015-03-23 10:10:27 -0700438 return true;
439 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700440 bool ReadDone(bool ok, Histogram* hist) {
vjpai46f65232015-03-23 10:10:27 -0700441 hist->Add((Timer::Now() - start_) * 1e9);
442 return StartWrite(ok);
443 }
444 grpc::ClientContext context_;
vjpai119c1032015-10-29 01:21:04 -0700445 BenchmarkService::Stub* stub_;
vjpai46f65232015-03-23 10:10:27 -0700446 RequestType req_;
447 ResponseType response_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700448 bool (ClientRpcContextStreamingImpl::*next_state_)(bool, Histogram*);
449 std::function<void(grpc::Status, ResponseType*)> callback_;
Vijay Pai18c04772016-01-04 09:52:10 -0800450 std::function<std::unique_ptr<
451 grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>(
452 BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*, void*)>
453 start_req_;
vjpai46f65232015-03-23 10:10:27 -0700454 grpc::Status status_;
455 double start_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700456 std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>
457 stream_;
vjpai46f65232015-03-23 10:10:27 -0700458};
459
Vijay Pai18c04772016-01-04 09:52:10 -0800460class AsyncStreamingClient GRPC_FINAL
461 : public AsyncClient<BenchmarkService::Stub, SimpleRequest> {
vjpai46f65232015-03-23 10:10:27 -0700462 public:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700463 explicit AsyncStreamingClient(const ClientConfig& config)
Vijay Pai18c04772016-01-04 09:52:10 -0800464 : AsyncClient(config, SetupCtx, BenchmarkStubCreator) {
vjpai754751e2015-10-28 09:16:22 -0700465 // async streaming currently only supports closed loop
466 GPR_ASSERT(closed_loop_);
Vijay Pai372fd872015-06-08 13:30:08 -0700467
vjpai46f65232015-03-23 10:10:27 -0700468 StartThreads(config.async_client_threads());
469 }
470
Vijay Paie10ebf152015-04-30 13:12:31 -0700471 ~AsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); }
Vijay Paicf3fb092015-06-05 03:41:30 -0700472
473 private:
vjpai09d0b0c2015-07-31 08:39:54 -0700474 static void CheckDone(grpc::Status s, SimpleResponse* response) {}
Vijay Paiab1dba72015-07-31 09:09:09 -0700475 static std::unique_ptr<
476 grpc::ClientAsyncReaderWriter<SimpleRequest, SimpleResponse>>
vjpai119c1032015-10-29 01:21:04 -0700477 StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx,
Vijay Paiab1dba72015-07-31 09:09:09 -0700478 CompletionQueue* cq, void* tag) {
vjpai09d0b0c2015-07-31 08:39:54 -0700479 auto stream = stub->AsyncStreamingCall(ctx, cq, tag);
480 return stream;
481 };
Vijay Paice846702015-11-04 00:30:12 -0800482 static ClientRpcContext* SetupCtx(int channel_id,
483 BenchmarkService::Stub* stub,
Vijay Pai372fd872015-06-08 13:30:08 -0700484 const SimpleRequest& req) {
Vijay Pai372fd872015-06-08 13:30:08 -0700485 return new ClientRpcContextStreamingImpl<SimpleRequest, SimpleResponse>(
Vijay Paiab1dba72015-07-31 09:09:09 -0700486 channel_id, stub, req, AsyncStreamingClient::StartReq,
487 AsyncStreamingClient::CheckDone);
vjpai46f65232015-03-23 10:10:27 -0700488 }
vjpai46f65232015-03-23 10:10:27 -0700489};
490
vjpaib6df94a2015-11-30 15:52:50 -0800491class ClientGenericRpcContextStreamingImpl : public ClientRpcContext {
492 public:
493 ClientGenericRpcContextStreamingImpl(
494 int channel_id, grpc::GenericStub* stub, const ByteBuffer& req,
Vijay Pai18c04772016-01-04 09:52:10 -0800495 std::function<std::unique_ptr<grpc::GenericClientAsyncReaderWriter>(
496 grpc::GenericStub*, grpc::ClientContext*,
497 const grpc::string& method_name, CompletionQueue*, void*)>
498 start_req,
vjpaib6df94a2015-11-30 15:52:50 -0800499 std::function<void(grpc::Status, ByteBuffer*)> on_done)
500 : ClientRpcContext(channel_id),
501 context_(),
502 stub_(stub),
503 req_(req),
504 response_(),
505 next_state_(&ClientGenericRpcContextStreamingImpl::ReqSent),
506 callback_(on_done),
507 start_req_(start_req),
508 start_(Timer::Now()) {}
509 ~ClientGenericRpcContextStreamingImpl() GRPC_OVERRIDE {}
510 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
511 return (this->*next_state_)(ok, hist);
512 }
513 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
514 return new ClientGenericRpcContextStreamingImpl(channel_id_, stub_, req_,
Vijay Pai18c04772016-01-04 09:52:10 -0800515 start_req_, callback_);
vjpaib6df94a2015-11-30 15:52:50 -0800516 }
517 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
Vijay Pai18c04772016-01-04 09:52:10 -0800518 const grpc::string kMethodName(
519 "/grpc.testing.BenchmarkService/StreamingCall");
520 stream_ = start_req_(stub_, &context_, kMethodName, cq,
521 ClientRpcContext::tag(this));
vjpaib6df94a2015-11-30 15:52:50 -0800522 }
523
524 private:
525 bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); }
526 bool StartWrite(bool ok) {
527 if (!ok) {
528 return (false);
529 }
530 start_ = Timer::Now();
531 next_state_ = &ClientGenericRpcContextStreamingImpl::WriteDone;
532 stream_->Write(req_, ClientRpcContext::tag(this));
533 return true;
534 }
535 bool WriteDone(bool ok, Histogram*) {
536 if (!ok) {
537 return (false);
538 }
539 next_state_ = &ClientGenericRpcContextStreamingImpl::ReadDone;
540 stream_->Read(&response_, ClientRpcContext::tag(this));
541 return true;
542 }
543 bool ReadDone(bool ok, Histogram* hist) {
544 hist->Add((Timer::Now() - start_) * 1e9);
545 return StartWrite(ok);
546 }
547 grpc::ClientContext context_;
548 grpc::GenericStub* stub_;
549 ByteBuffer req_;
550 ByteBuffer response_;
551 bool (ClientGenericRpcContextStreamingImpl::*next_state_)(bool, Histogram*);
552 std::function<void(grpc::Status, ByteBuffer*)> callback_;
Vijay Pai18c04772016-01-04 09:52:10 -0800553 std::function<std::unique_ptr<grpc::GenericClientAsyncReaderWriter>(
554 grpc::GenericStub*, grpc::ClientContext*, const grpc::string&,
555 CompletionQueue*, void*)>
556 start_req_;
vjpaib6df94a2015-11-30 15:52:50 -0800557 grpc::Status status_;
558 double start_;
559 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> stream_;
560};
561
Vijay Pai18c04772016-01-04 09:52:10 -0800562static std::unique_ptr<grpc::GenericStub> GenericStubCreator(
563 std::shared_ptr<Channel> ch) {
564 return std::unique_ptr<grpc::GenericStub>(new grpc::GenericStub(ch));
565}
566
567class GenericAsyncStreamingClient GRPC_FINAL
568 : public AsyncClient<grpc::GenericStub, ByteBuffer> {
vjpaib6df94a2015-11-30 15:52:50 -0800569 public:
570 explicit GenericAsyncStreamingClient(const ClientConfig& config)
Vijay Pai18c04772016-01-04 09:52:10 -0800571 : AsyncClient(config, SetupCtx, GenericStubCreator) {
vjpaib6df94a2015-11-30 15:52:50 -0800572 // async streaming currently only supports closed loop
573 GPR_ASSERT(closed_loop_);
574
575 StartThreads(config.async_client_threads());
576 }
577
578 ~GenericAsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); }
579
580 private:
581 static void CheckDone(grpc::Status s, ByteBuffer* response) {}
Vijay Pai18c04772016-01-04 09:52:10 -0800582 static std::unique_ptr<grpc::GenericClientAsyncReaderWriter> StartReq(
583 grpc::GenericStub* stub, grpc::ClientContext* ctx,
584 const grpc::string& method_name, CompletionQueue* cq, void* tag) {
vjpaib6df94a2015-11-30 15:52:50 -0800585 auto stream = stub->Call(ctx, method_name, cq, tag);
586 return stream;
587 };
Vijay Pai18c04772016-01-04 09:52:10 -0800588 static ClientRpcContext* SetupCtx(int channel_id, grpc::GenericStub* stub,
Vijay Paie4886682015-12-30 11:56:19 -0800589 const ByteBuffer& req) {
590 return new ClientGenericRpcContextStreamingImpl(
591 channel_id, stub, req, GenericAsyncStreamingClient::StartReq,
592 GenericAsyncStreamingClient::CheckDone);
vjpaib6df94a2015-11-30 15:52:50 -0800593 }
594};
595
Vijay Pai1da729a2015-03-23 12:52:56 -0700596std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args) {
vjpai46f65232015-03-23 10:10:27 -0700597 return std::unique_ptr<Client>(new AsyncUnaryClient(args));
598}
Vijay Pai1da729a2015-03-23 12:52:56 -0700599std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args) {
vjpai46f65232015-03-23 10:10:27 -0700600 return std::unique_ptr<Client>(new AsyncStreamingClient(args));
Craig Tiller88568752015-03-04 10:50:43 -0800601}
Vijay Pai18c04772016-01-04 09:52:10 -0800602std::unique_ptr<Client> CreateGenericAsyncStreamingClient(
603 const ClientConfig& args) {
vjpaib6df94a2015-11-30 15:52:50 -0800604 return std::unique_ptr<Client>(new GenericAsyncStreamingClient(args));
605}
Craig Tiller88568752015-03-04 10:50:43 -0800606
Craig Tillera182bf12015-03-04 13:54:39 -0800607} // namespace testing
608} // namespace grpc