blob: 3e2317c6d460f067c88cbd53a1990fbaea9083cf [file] [log] [blame]
vjpaidea740f2015-02-26 16:35:35 -08001/*
2 *
murgatroid99eeb02ba2016-01-13 20:43:33 -08003 * Copyright 2015-2016, Google Inc.
vjpaidea740f2015-02-26 16:35:35 -08004 * 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"
Craig Tiller1b4e3302015-12-17 16:35:00 -080055#include "src/proto/grpc/testing/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&,
Craig Tiller754bd262016-01-14 06:10:15 -080096 CompletionQueue*)> start_req,
Yang Gao6baa9b62015-03-17 10:49:39 -070097 std::function<void(grpc::Status, ResponseType*)> on_done)
Vijay Pai372fd872015-06-08 13:30:08 -070098 : ClientRpcContext(channel_id),
99 context_(),
Craig Tiller88568752015-03-04 10:50:43 -0800100 stub_(stub),
Vijay Pai64ac47f2015-02-26 17:59:51 -0800101 req_(req),
102 response_(),
Craig Tiller3676b382015-05-06 13:01:05 -0700103 next_state_(&ClientRpcContextUnaryImpl::RespDone),
Craig Tillera182bf12015-03-04 13:54:39 -0800104 callback_(on_done),
Vijay Pai372fd872015-06-08 13:30:08 -0700105 start_req_(start_req) {}
106 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
107 start_ = Timer::Now();
108 response_reader_ = start_req_(stub_, &context_, req_, cq);
Craig Tiller3676b382015-05-06 13:01:05 -0700109 response_reader_->Finish(&response_, &status_, ClientRpcContext::tag(this));
110 }
vjpai3c110662015-02-27 07:17:16 -0800111 ~ClientRpcContextUnaryImpl() GRPC_OVERRIDE {}
Vijay Pai1da729a2015-03-23 12:52:56 -0700112 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -0700113 bool ret = (this->*next_state_)(ok);
114 if (!ret) {
115 hist->Add((Timer::Now() - start_) * 1e9);
116 }
117 return ret;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800118 }
119
Vijay Pai372fd872015-06-08 13:30:08 -0700120 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
121 return new ClientRpcContextUnaryImpl(channel_id_, stub_, req_, start_req_,
122 callback_);
Craig Tilleref638392015-03-04 12:23:12 -0800123 }
124
Vijay Pai64ac47f2015-02-26 17:59:51 -0800125 private:
vjpai46f65232015-03-23 10:10:27 -0700126 bool RespDone(bool) {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800127 next_state_ = &ClientRpcContextUnaryImpl::DoCallBack;
128 return false;
129 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700130 bool DoCallBack(bool) {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800131 callback_(status_, &response_);
Vijay Pai372fd872015-06-08 13:30:08 -0700132 return true; // we're done, this'll be ignored
Vijay Pai64ac47f2015-02-26 17:59:51 -0800133 }
134 grpc::ClientContext context_;
vjpai119c1032015-10-29 01:21:04 -0700135 BenchmarkService::Stub* stub_;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800136 RequestType req_;
137 ResponseType response_;
vjpai46f65232015-03-23 10:10:27 -0700138 bool (ClientRpcContextUnaryImpl::*next_state_)(bool);
Yang Gao6baa9b62015-03-17 10:49:39 -0700139 std::function<void(grpc::Status, ResponseType*)> callback_;
Craig Tillera182bf12015-03-04 13:54:39 -0800140 std::function<std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>(
vjpai119c1032015-10-29 01:21:04 -0700141 BenchmarkService::Stub*, grpc::ClientContext*, const RequestType&,
Craig Tiller754bd262016-01-14 06:10:15 -0800142 CompletionQueue*)> start_req_;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800143 grpc::Status status_;
144 double start_;
145 std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>
146 response_reader_;
147};
vjpaidea740f2015-02-26 16:35:35 -0800148
Vijay Pai372fd872015-06-08 13:30:08 -0700149typedef std::forward_list<ClientRpcContext*> context_list;
150
vjpaib6df94a2015-11-30 15:52:50 -0800151template <class StubType, class RequestType>
Vijay Paie4886682015-12-30 11:56:19 -0800152class AsyncClient : public ClientImpl<StubType, RequestType> {
153 // Specify which protected members we are using since there is no
154 // member name resolution until the template types are fully resolved
Craig Tiller88568752015-03-04 10:50:43 -0800155 public:
Vijay Paie4886682015-12-30 11:56:19 -0800156 using Client::SetupLoadTest;
157 using Client::NextIssueTime;
158 using Client::closed_loop_;
Vijay Pai18c04772016-01-04 09:52:10 -0800159 using ClientImpl<StubType, RequestType>::channels_;
160 using ClientImpl<StubType, RequestType>::request_;
Craig Tiller754bd262016-01-14 06:10:15 -0800161 AsyncClient(const ClientConfig& config,
162 std::function<ClientRpcContext*(int, StubType*,
163 const RequestType&)> setup_ctx,
164 std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)>
165 create_stub)
Vijay Pai18c04772016-01-04 09:52:10 -0800166 : ClientImpl<StubType, RequestType>(config, create_stub),
Vijay Pai90e73692015-08-05 19:15:36 -0700167 channel_lock_(new std::mutex[config.client_channels()]),
Vijay Pai372fd872015-06-08 13:30:08 -0700168 contexts_(config.client_channels()),
169 max_outstanding_per_channel_(config.outstanding_rpcs_per_channel()),
170 channel_count_(config.client_channels()),
171 pref_channel_inc_(config.async_client_threads()) {
172 SetupLoadTest(config, config.async_client_threads());
173
Craig Tiller88568752015-03-04 10:50:43 -0800174 for (int i = 0; i < config.async_client_threads(); i++) {
175 cli_cqs_.emplace_back(new CompletionQueue);
Vijay Pai372fd872015-06-08 13:30:08 -0700176 if (!closed_loop_) {
177 rpc_deadlines_.emplace_back();
178 next_channel_.push_back(i % channel_count_);
Vijay Paie2980cd2015-07-01 09:38:20 -0700179 issue_allowed_.emplace_back(true);
Vijay Pai372fd872015-06-08 13:30:08 -0700180
181 grpc_time next_issue;
182 NextIssueTime(i, &next_issue);
183 next_issue_.push_back(next_issue);
184 }
Craig Tiller88568752015-03-04 10:50:43 -0800185 }
Vijay Pai372fd872015-06-08 13:30:08 -0700186
vjpai6a608022015-05-18 09:16:53 -0700187 int t = 0;
188 for (int i = 0; i < config.outstanding_rpcs_per_channel(); i++) {
Vijay Pai372fd872015-06-08 13:30:08 -0700189 for (int ch = 0; ch < channel_count_; ch++) {
Vijay Pai7b172b22015-06-05 02:03:18 -0700190 auto* cq = cli_cqs_[t].get();
191 t = (t + 1) % cli_cqs_.size();
Vijay Pai372fd872015-06-08 13:30:08 -0700192 auto ctx = setup_ctx(ch, channels_[ch].get_stub(), request_);
193 if (closed_loop_) {
194 ctx->Start(cq);
195 } else {
196 contexts_[ch].push_front(ctx);
197 }
Craig Tiller88568752015-03-04 10:50:43 -0800198 }
199 }
Craig Tiller88568752015-03-04 10:50:43 -0800200 }
Vijay Paie10ebf152015-04-30 13:12:31 -0700201 virtual ~AsyncClient() {
Vijay Pai82dd80a2015-03-24 10:36:08 -0700202 for (auto cq = cli_cqs_.begin(); cq != cli_cqs_.end(); cq++) {
203 (*cq)->Shutdown();
Yang Gao6baa9b62015-03-17 10:49:39 -0700204 void* got_tag;
Craig Tilleref638392015-03-04 12:23:12 -0800205 bool ok;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700206 while ((*cq)->Next(&got_tag, &ok)) {
Craig Tilleref638392015-03-04 12:23:12 -0800207 delete ClientRpcContext::detag(got_tag);
208 }
209 }
Vijay Pai56061992015-07-01 13:39:48 -0700210 // Now clear out all the pre-allocated idle contexts
211 for (int ch = 0; ch < channel_count_; ch++) {
Vijay Pai13735d52015-07-01 14:00:08 -0700212 while (!contexts_[ch].empty()) {
Vijay Pai56061992015-07-01 13:39:48 -0700213 // Get an idle context from the front of the list
214 auto* ctx = *(contexts_[ch].begin());
215 contexts_[ch].pop_front();
216 delete ctx;
217 }
218 }
Vijay Paieed63fa2015-08-05 23:08:34 +0000219 delete[] channel_lock_;
Craig Tilleref638392015-03-04 12:23:12 -0800220 }
221
Craig Tiller5c8737d2015-05-21 11:42:17 -0700222 bool ThreadFunc(Histogram* histogram,
223 size_t thread_idx) GRPC_OVERRIDE GRPC_FINAL {
Yang Gao6baa9b62015-03-17 10:49:39 -0700224 void* got_tag;
Craig Tiller88568752015-03-04 10:50:43 -0800225 bool ok;
Vijay Pai372fd872015-06-08 13:30:08 -0700226 grpc_time deadline, short_deadline;
227 if (closed_loop_) {
228 deadline = grpc_time_source::now() + std::chrono::seconds(1);
229 short_deadline = deadline;
230 } else {
231 if (rpc_deadlines_[thread_idx].empty()) {
232 deadline = grpc_time_source::now() + std::chrono::seconds(1);
233 } else {
234 deadline = *(rpc_deadlines_[thread_idx].begin());
235 }
236 short_deadline =
237 issue_allowed_[thread_idx] ? next_issue_[thread_idx] : deadline;
238 }
239
240 bool got_event;
241
242 switch (cli_cqs_[thread_idx]->AsyncNext(&got_tag, &ok, short_deadline)) {
Vijay Paicf3fb092015-06-05 03:41:30 -0700243 case CompletionQueue::SHUTDOWN:
244 return false;
vjpai37f72572015-05-12 10:29:07 -0700245 case CompletionQueue::TIMEOUT:
Vijay Pai372fd872015-06-08 13:30:08 -0700246 got_event = false;
247 break;
vjpai37f72572015-05-12 10:29:07 -0700248 case CompletionQueue::GOT_EVENT:
Vijay Pai372fd872015-06-08 13:30:08 -0700249 got_event = true;
250 break;
251 default:
252 GPR_ASSERT(false);
Vijay Pai9dc5c152015-06-03 11:34:53 -0700253 break;
Craig Tiller1c61af72015-04-09 12:08:44 -0700254 }
Vijay Pai372fd872015-06-08 13:30:08 -0700255 if (got_event) {
256 ClientRpcContext* ctx = ClientRpcContext::detag(got_tag);
257 if (ctx->RunNextState(ok, histogram) == false) {
258 // call the callback and then clone the ctx
259 ctx->RunNextState(ok, histogram);
260 ClientRpcContext* clone_ctx = ctx->StartNewClone();
261 if (closed_loop_) {
262 clone_ctx->Start(cli_cqs_[thread_idx].get());
263 } else {
264 // Remove the entry from the rpc deadlines list
265 rpc_deadlines_[thread_idx].erase(ctx->deadline_posn());
266 // Put the clone_ctx in the list of idle contexts for this channel
267 // Under lock
268 int ch = clone_ctx->channel_id();
269 std::lock_guard<std::mutex> g(channel_lock_[ch]);
270 contexts_[ch].push_front(clone_ctx);
271 }
272 // delete the old version
273 delete ctx;
274 }
275 if (!closed_loop_)
276 issue_allowed_[thread_idx] =
277 true; // may be ok now even if it hadn't been
278 }
279 if (!closed_loop_ && issue_allowed_[thread_idx] &&
280 grpc_time_source::now() >= next_issue_[thread_idx]) {
281 // Attempt to issue
282 bool issued = false;
283 for (int num_attempts = 0, channel_attempt = next_channel_[thread_idx];
284 num_attempts < channel_count_ && !issued; num_attempts++) {
285 bool can_issue = false;
286 ClientRpcContext* ctx = nullptr;
287 {
288 std::lock_guard<std::mutex> g(channel_lock_[channel_attempt]);
289 if (!contexts_[channel_attempt].empty()) {
290 // Get an idle context from the front of the list
291 ctx = *(contexts_[channel_attempt].begin());
292 contexts_[channel_attempt].pop_front();
293 can_issue = true;
294 }
295 }
296 if (can_issue) {
297 // do the work to issue
298 rpc_deadlines_[thread_idx].emplace_back(grpc_time_source::now() +
299 std::chrono::seconds(1));
300 auto it = rpc_deadlines_[thread_idx].end();
301 --it;
302 ctx->set_deadline_posn(it);
303 ctx->Start(cli_cqs_[thread_idx].get());
304 issued = true;
305 // If we did issue, then next time, try our thread's next
306 // preferred channel
307 next_channel_[thread_idx] += pref_channel_inc_;
308 if (next_channel_[thread_idx] >= channel_count_)
309 next_channel_[thread_idx] = (thread_idx % channel_count_);
310 } else {
311 // Do a modular increment of channel attempt if we couldn't issue
312 channel_attempt = (channel_attempt + 1) % channel_count_;
313 }
314 }
315 if (issued) {
316 // We issued one; see when we can issue the next
317 grpc_time next_issue;
318 NextIssueTime(thread_idx, &next_issue);
319 next_issue_[thread_idx] = next_issue;
320 } else {
321 issue_allowed_[thread_idx] = false;
322 }
323 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700324 return true;
Craig Tiller88568752015-03-04 10:50:43 -0800325 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700326
Vijay Paie10ebf152015-04-30 13:12:31 -0700327 private:
Vijay Paiab1dba72015-07-31 09:09:09 -0700328 class boolean { // exists only to avoid data-race on vector<bool>
Vijay Paie2980cd2015-07-01 09:38:20 -0700329 public:
Vijay Paiab1dba72015-07-31 09:09:09 -0700330 boolean() : val_(false) {}
331 boolean(bool b) : val_(b) {}
332 operator bool() const { return val_; }
333 boolean& operator=(bool b) {
334 val_ = b;
335 return *this;
336 }
337
Vijay Paie2980cd2015-07-01 09:38:20 -0700338 private:
339 bool val_;
340 };
Craig Tiller88568752015-03-04 10:50:43 -0800341 std::vector<std::unique_ptr<CompletionQueue>> cli_cqs_;
Vijay Pai372fd872015-06-08 13:30:08 -0700342
343 std::vector<deadline_list> rpc_deadlines_; // per thread deadlines
Vijay Paiab1dba72015-07-31 09:09:09 -0700344 std::vector<int> next_channel_; // per thread round-robin channel ctr
345 std::vector<boolean> issue_allowed_; // may this thread attempt to issue
346 std::vector<grpc_time> next_issue_; // when should it issue?
Vijay Pai372fd872015-06-08 13:30:08 -0700347
Vijay Pai90e73692015-08-05 19:15:36 -0700348 std::mutex*
349 channel_lock_; // a vector, but avoid std::vector for old compilers
Vijay Pai372fd872015-06-08 13:30:08 -0700350 std::vector<context_list> contexts_; // per-channel list of idle contexts
351 int max_outstanding_per_channel_;
352 int channel_count_;
353 int pref_channel_inc_;
Craig Tiller88568752015-03-04 10:50:43 -0800354};
355
Vijay Pai18c04772016-01-04 09:52:10 -0800356static std::unique_ptr<BenchmarkService::Stub> BenchmarkStubCreator(
357 std::shared_ptr<Channel> ch) {
358 return BenchmarkService::NewStub(ch);
359}
360
361class AsyncUnaryClient GRPC_FINAL
362 : public AsyncClient<BenchmarkService::Stub, SimpleRequest> {
Vijay Paie10ebf152015-04-30 13:12:31 -0700363 public:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700364 explicit AsyncUnaryClient(const ClientConfig& config)
Vijay Pai18c04772016-01-04 09:52:10 -0800365 : AsyncClient(config, SetupCtx, BenchmarkStubCreator) {
Vijay Paie10ebf152015-04-30 13:12:31 -0700366 StartThreads(config.async_client_threads());
367 }
368 ~AsyncUnaryClient() GRPC_OVERRIDE { EndThreads(); }
Vijay Paicf3fb092015-06-05 03:41:30 -0700369
370 private:
vjpai09d0b0c2015-07-31 08:39:54 -0700371 static void CheckDone(grpc::Status s, SimpleResponse* response) {}
372 static std::unique_ptr<grpc::ClientAsyncResponseReader<SimpleResponse>>
vjpai119c1032015-10-29 01:21:04 -0700373 StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx,
Vijay Paiab1dba72015-07-31 09:09:09 -0700374 const SimpleRequest& request, CompletionQueue* cq) {
vjpai09d0b0c2015-07-31 08:39:54 -0700375 return stub->AsyncUnaryCall(ctx, request, cq);
376 };
Vijay Paice846702015-11-04 00:30:12 -0800377 static ClientRpcContext* SetupCtx(int channel_id,
378 BenchmarkService::Stub* stub,
Vijay Pai372fd872015-06-08 13:30:08 -0700379 const SimpleRequest& req) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700380 return new ClientRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
381 channel_id, stub, req, AsyncUnaryClient::StartReq,
382 AsyncUnaryClient::CheckDone);
Vijay Paie10ebf152015-04-30 13:12:31 -0700383 }
384};
385
vjpai46f65232015-03-23 10:10:27 -0700386template <class RequestType, class ResponseType>
387class ClientRpcContextStreamingImpl : public ClientRpcContext {
388 public:
Vijay Paicf3fb092015-06-05 03:41:30 -0700389 ClientRpcContextStreamingImpl(
vjpai119c1032015-10-29 01:21:04 -0700390 int channel_id, BenchmarkService::Stub* stub, const RequestType& req,
Vijay Paice846702015-11-04 00:30:12 -0800391 std::function<std::unique_ptr<
392 grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>(
393 BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*,
Craig Tiller754bd262016-01-14 06:10:15 -0800394 void*)> start_req,
Craig Tiller5c8737d2015-05-21 11:42:17 -0700395 std::function<void(grpc::Status, ResponseType*)> on_done)
Vijay Pai372fd872015-06-08 13:30:08 -0700396 : ClientRpcContext(channel_id),
397 context_(),
vjpai46f65232015-03-23 10:10:27 -0700398 stub_(stub),
399 req_(req),
400 response_(),
401 next_state_(&ClientRpcContextStreamingImpl::ReqSent),
402 callback_(on_done),
403 start_req_(start_req),
Vijay Pai372fd872015-06-08 13:30:08 -0700404 start_(Timer::Now()) {}
vjpai46f65232015-03-23 10:10:27 -0700405 ~ClientRpcContextStreamingImpl() GRPC_OVERRIDE {}
Craig Tiller5c8737d2015-05-21 11:42:17 -0700406 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -0700407 return (this->*next_state_)(ok, hist);
408 }
Vijay Pai372fd872015-06-08 13:30:08 -0700409 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
410 return new ClientRpcContextStreamingImpl(channel_id_, stub_, req_,
411 start_req_, callback_);
412 }
413 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
414 stream_ = start_req_(stub_, &context_, cq, ClientRpcContext::tag(this));
Vijay Pai7b172b22015-06-05 02:03:18 -0700415 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700416
vjpai46f65232015-03-23 10:10:27 -0700417 private:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700418 bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); }
vjpai46f65232015-03-23 10:10:27 -0700419 bool StartWrite(bool ok) {
420 if (!ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700421 return (false);
vjpai46f65232015-03-23 10:10:27 -0700422 }
423 start_ = Timer::Now();
424 next_state_ = &ClientRpcContextStreamingImpl::WriteDone;
425 stream_->Write(req_, ClientRpcContext::tag(this));
426 return true;
427 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700428 bool WriteDone(bool ok, Histogram*) {
vjpai46f65232015-03-23 10:10:27 -0700429 if (!ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700430 return (false);
vjpai46f65232015-03-23 10:10:27 -0700431 }
432 next_state_ = &ClientRpcContextStreamingImpl::ReadDone;
Vijay Paie10ebf152015-04-30 13:12:31 -0700433 stream_->Read(&response_, ClientRpcContext::tag(this));
vjpai46f65232015-03-23 10:10:27 -0700434 return true;
435 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700436 bool ReadDone(bool ok, Histogram* hist) {
vjpai46f65232015-03-23 10:10:27 -0700437 hist->Add((Timer::Now() - start_) * 1e9);
438 return StartWrite(ok);
439 }
440 grpc::ClientContext context_;
vjpai119c1032015-10-29 01:21:04 -0700441 BenchmarkService::Stub* stub_;
vjpai46f65232015-03-23 10:10:27 -0700442 RequestType req_;
443 ResponseType response_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700444 bool (ClientRpcContextStreamingImpl::*next_state_)(bool, Histogram*);
445 std::function<void(grpc::Status, ResponseType*)> callback_;
Craig Tiller754bd262016-01-14 06:10:15 -0800446 std::function<
447 std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>(
448 BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*,
449 void*)> start_req_;
vjpai46f65232015-03-23 10:10:27 -0700450 grpc::Status status_;
451 double start_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700452 std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>
453 stream_;
vjpai46f65232015-03-23 10:10:27 -0700454};
455
Vijay Pai18c04772016-01-04 09:52:10 -0800456class AsyncStreamingClient GRPC_FINAL
457 : public AsyncClient<BenchmarkService::Stub, SimpleRequest> {
vjpai46f65232015-03-23 10:10:27 -0700458 public:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700459 explicit AsyncStreamingClient(const ClientConfig& config)
Vijay Pai18c04772016-01-04 09:52:10 -0800460 : AsyncClient(config, SetupCtx, BenchmarkStubCreator) {
vjpai754751e2015-10-28 09:16:22 -0700461 // async streaming currently only supports closed loop
462 GPR_ASSERT(closed_loop_);
Vijay Pai372fd872015-06-08 13:30:08 -0700463
vjpai46f65232015-03-23 10:10:27 -0700464 StartThreads(config.async_client_threads());
465 }
466
Vijay Paie10ebf152015-04-30 13:12:31 -0700467 ~AsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); }
Vijay Paicf3fb092015-06-05 03:41:30 -0700468
469 private:
vjpai09d0b0c2015-07-31 08:39:54 -0700470 static void CheckDone(grpc::Status s, SimpleResponse* response) {}
Vijay Paiab1dba72015-07-31 09:09:09 -0700471 static std::unique_ptr<
472 grpc::ClientAsyncReaderWriter<SimpleRequest, SimpleResponse>>
vjpai119c1032015-10-29 01:21:04 -0700473 StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx,
Vijay Paiab1dba72015-07-31 09:09:09 -0700474 CompletionQueue* cq, void* tag) {
vjpai09d0b0c2015-07-31 08:39:54 -0700475 auto stream = stub->AsyncStreamingCall(ctx, cq, tag);
476 return stream;
477 };
Vijay Paice846702015-11-04 00:30:12 -0800478 static ClientRpcContext* SetupCtx(int channel_id,
479 BenchmarkService::Stub* stub,
Vijay Pai372fd872015-06-08 13:30:08 -0700480 const SimpleRequest& req) {
Vijay Pai372fd872015-06-08 13:30:08 -0700481 return new ClientRpcContextStreamingImpl<SimpleRequest, SimpleResponse>(
Vijay Paiab1dba72015-07-31 09:09:09 -0700482 channel_id, stub, req, AsyncStreamingClient::StartReq,
483 AsyncStreamingClient::CheckDone);
vjpai46f65232015-03-23 10:10:27 -0700484 }
vjpai46f65232015-03-23 10:10:27 -0700485};
486
Vijay Pai9f991e22016-01-07 14:08:09 -0800487class ClientRpcContextGenericStreamingImpl : public ClientRpcContext {
vjpaib6df94a2015-11-30 15:52:50 -0800488 public:
Vijay Pai9f991e22016-01-07 14:08:09 -0800489 ClientRpcContextGenericStreamingImpl(
vjpaib6df94a2015-11-30 15:52:50 -0800490 int channel_id, grpc::GenericStub* stub, const ByteBuffer& req,
Vijay Pai18c04772016-01-04 09:52:10 -0800491 std::function<std::unique_ptr<grpc::GenericClientAsyncReaderWriter>(
492 grpc::GenericStub*, grpc::ClientContext*,
Craig Tiller754bd262016-01-14 06:10:15 -0800493 const grpc::string& method_name, CompletionQueue*, void*)> start_req,
vjpaib6df94a2015-11-30 15:52:50 -0800494 std::function<void(grpc::Status, ByteBuffer*)> on_done)
495 : ClientRpcContext(channel_id),
496 context_(),
497 stub_(stub),
498 req_(req),
499 response_(),
Vijay Pai9f991e22016-01-07 14:08:09 -0800500 next_state_(&ClientRpcContextGenericStreamingImpl::ReqSent),
vjpaib6df94a2015-11-30 15:52:50 -0800501 callback_(on_done),
502 start_req_(start_req),
503 start_(Timer::Now()) {}
Vijay Pai9f991e22016-01-07 14:08:09 -0800504 ~ClientRpcContextGenericStreamingImpl() GRPC_OVERRIDE {}
vjpaib6df94a2015-11-30 15:52:50 -0800505 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
506 return (this->*next_state_)(ok, hist);
507 }
508 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
Vijay Pai9f991e22016-01-07 14:08:09 -0800509 return new ClientRpcContextGenericStreamingImpl(channel_id_, stub_, req_,
Vijay Pai18c04772016-01-04 09:52:10 -0800510 start_req_, callback_);
vjpaib6df94a2015-11-30 15:52:50 -0800511 }
512 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
Vijay Pai18c04772016-01-04 09:52:10 -0800513 const grpc::string kMethodName(
514 "/grpc.testing.BenchmarkService/StreamingCall");
515 stream_ = start_req_(stub_, &context_, kMethodName, cq,
516 ClientRpcContext::tag(this));
vjpaib6df94a2015-11-30 15:52:50 -0800517 }
518
519 private:
520 bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); }
521 bool StartWrite(bool ok) {
522 if (!ok) {
523 return (false);
524 }
525 start_ = Timer::Now();
Vijay Pai9f991e22016-01-07 14:08:09 -0800526 next_state_ = &ClientRpcContextGenericStreamingImpl::WriteDone;
vjpaib6df94a2015-11-30 15:52:50 -0800527 stream_->Write(req_, ClientRpcContext::tag(this));
528 return true;
529 }
530 bool WriteDone(bool ok, Histogram*) {
531 if (!ok) {
532 return (false);
533 }
Vijay Pai9f991e22016-01-07 14:08:09 -0800534 next_state_ = &ClientRpcContextGenericStreamingImpl::ReadDone;
vjpaib6df94a2015-11-30 15:52:50 -0800535 stream_->Read(&response_, ClientRpcContext::tag(this));
536 return true;
537 }
538 bool ReadDone(bool ok, Histogram* hist) {
539 hist->Add((Timer::Now() - start_) * 1e9);
540 return StartWrite(ok);
541 }
542 grpc::ClientContext context_;
543 grpc::GenericStub* stub_;
544 ByteBuffer req_;
545 ByteBuffer response_;
Vijay Pai9f991e22016-01-07 14:08:09 -0800546 bool (ClientRpcContextGenericStreamingImpl::*next_state_)(bool, Histogram*);
vjpaib6df94a2015-11-30 15:52:50 -0800547 std::function<void(grpc::Status, ByteBuffer*)> callback_;
Vijay Pai18c04772016-01-04 09:52:10 -0800548 std::function<std::unique_ptr<grpc::GenericClientAsyncReaderWriter>(
549 grpc::GenericStub*, grpc::ClientContext*, const grpc::string&,
Craig Tiller754bd262016-01-14 06:10:15 -0800550 CompletionQueue*, void*)> start_req_;
vjpaib6df94a2015-11-30 15:52:50 -0800551 grpc::Status status_;
552 double start_;
553 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> stream_;
554};
555
Vijay Pai18c04772016-01-04 09:52:10 -0800556static std::unique_ptr<grpc::GenericStub> GenericStubCreator(
557 std::shared_ptr<Channel> ch) {
558 return std::unique_ptr<grpc::GenericStub>(new grpc::GenericStub(ch));
559}
560
561class GenericAsyncStreamingClient GRPC_FINAL
562 : public AsyncClient<grpc::GenericStub, ByteBuffer> {
vjpaib6df94a2015-11-30 15:52:50 -0800563 public:
564 explicit GenericAsyncStreamingClient(const ClientConfig& config)
Vijay Pai18c04772016-01-04 09:52:10 -0800565 : AsyncClient(config, SetupCtx, GenericStubCreator) {
vjpaib6df94a2015-11-30 15:52:50 -0800566 // async streaming currently only supports closed loop
567 GPR_ASSERT(closed_loop_);
568
569 StartThreads(config.async_client_threads());
570 }
571
572 ~GenericAsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); }
573
574 private:
575 static void CheckDone(grpc::Status s, ByteBuffer* response) {}
Vijay Pai18c04772016-01-04 09:52:10 -0800576 static std::unique_ptr<grpc::GenericClientAsyncReaderWriter> StartReq(
577 grpc::GenericStub* stub, grpc::ClientContext* ctx,
578 const grpc::string& method_name, CompletionQueue* cq, void* tag) {
vjpaib6df94a2015-11-30 15:52:50 -0800579 auto stream = stub->Call(ctx, method_name, cq, tag);
580 return stream;
581 };
Vijay Pai18c04772016-01-04 09:52:10 -0800582 static ClientRpcContext* SetupCtx(int channel_id, grpc::GenericStub* stub,
Vijay Paie4886682015-12-30 11:56:19 -0800583 const ByteBuffer& req) {
Vijay Pai9f991e22016-01-07 14:08:09 -0800584 return new ClientRpcContextGenericStreamingImpl(
Vijay Paie4886682015-12-30 11:56:19 -0800585 channel_id, stub, req, GenericAsyncStreamingClient::StartReq,
586 GenericAsyncStreamingClient::CheckDone);
vjpaib6df94a2015-11-30 15:52:50 -0800587 }
588};
589
Vijay Pai1da729a2015-03-23 12:52:56 -0700590std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args) {
vjpai46f65232015-03-23 10:10:27 -0700591 return std::unique_ptr<Client>(new AsyncUnaryClient(args));
592}
Vijay Pai1da729a2015-03-23 12:52:56 -0700593std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args) {
vjpai46f65232015-03-23 10:10:27 -0700594 return std::unique_ptr<Client>(new AsyncStreamingClient(args));
Craig Tiller88568752015-03-04 10:50:43 -0800595}
Vijay Pai18c04772016-01-04 09:52:10 -0800596std::unique_ptr<Client> CreateGenericAsyncStreamingClient(
597 const ClientConfig& args) {
vjpaib6df94a2015-11-30 15:52:50 -0800598 return std::unique_ptr<Client>(new GenericAsyncStreamingClient(args));
599}
Craig Tiller88568752015-03-04 10:50:43 -0800600
Craig Tillera182bf12015-03-04 13:54:39 -0800601} // namespace testing
murgatroid99eeb02ba2016-01-13 20:43:33 -0800602} // namespace grpc