blob: 9594179822ded7a74ca1542e9c671ea45dbdcfaf [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>
vjpaidea740f2015-02-26 16:35:35 -080040#include <string>
41#include <thread>
42#include <vector>
43#include <sstream>
44
45#include <grpc/grpc.h>
46#include <grpc/support/histogram.h>
47#include <grpc/support/log.h>
48#include <gflags/gflags.h>
vjpaidea740f2015-02-26 16:35:35 -080049#include <grpc++/client_context.h>
yang-g9e2f90c2015-08-21 15:35:03 -070050
Craig Tiller88568752015-03-04 10:50:43 -080051#include "test/cpp/qps/timer.h"
52#include "test/cpp/qps/client.h"
yang-g9e2f90c2015-08-21 15:35:03 -070053#include "test/cpp/util/create_test_channel.h"
vjpaid08a7382015-11-02 16:45:08 -080054#include "test/proto/benchmarks/services.grpc.pb.h"
vjpaidea740f2015-02-26 16:35:35 -080055
Craig Tiller88568752015-03-04 10:50:43 -080056namespace grpc {
57namespace testing {
vjpaidea740f2015-02-26 16:35:35 -080058
Vijay Pai372fd872015-06-08 13:30:08 -070059typedef std::list<grpc_time> deadline_list;
60
Vijay Pai64ac47f2015-02-26 17:59:51 -080061class ClientRpcContext {
62 public:
Yang Gaob6d57e72015-06-09 22:11:12 -070063 explicit ClientRpcContext(int ch) : channel_id_(ch) {}
Vijay Pai64ac47f2015-02-26 17:59:51 -080064 virtual ~ClientRpcContext() {}
vjpai46f65232015-03-23 10:10:27 -070065 // next state, return false if done. Collect stats when appropriate
Vijay Pai1da729a2015-03-23 12:52:56 -070066 virtual bool RunNextState(bool, Histogram* hist) = 0;
Vijay Pai372fd872015-06-08 13:30:08 -070067 virtual ClientRpcContext* StartNewClone() = 0;
Yang Gao6baa9b62015-03-17 10:49:39 -070068 static void* tag(ClientRpcContext* c) { return reinterpret_cast<void*>(c); }
69 static ClientRpcContext* detag(void* t) {
70 return reinterpret_cast<ClientRpcContext*>(t);
Vijay Pai64ac47f2015-02-26 17:59:51 -080071 }
Vijay Pai372fd872015-06-08 13:30:08 -070072
73 deadline_list::iterator deadline_posn() const { return deadline_posn_; }
74 void set_deadline_posn(const deadline_list::iterator& it) {
75 deadline_posn_ = it;
76 }
77 virtual void Start(CompletionQueue* cq) = 0;
78 int channel_id() const { return channel_id_; }
79
80 protected:
81 int channel_id_;
82
83 private:
84 deadline_list::iterator deadline_posn_;
Vijay Pai64ac47f2015-02-26 17:59:51 -080085};
Craig Tiller88568752015-03-04 10:50:43 -080086
Vijay Pai64ac47f2015-02-26 17:59:51 -080087template <class RequestType, class ResponseType>
88class ClientRpcContextUnaryImpl : public ClientRpcContext {
89 public:
Vijay Paicf3fb092015-06-05 03:41:30 -070090 ClientRpcContextUnaryImpl(
vjpai119c1032015-10-29 01:21:04 -070091 int channel_id, BenchmarkService::Stub* stub, const RequestType& req,
vjpai4e1e1bc2015-02-27 23:47:12 -080092 std::function<
93 std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>(
vjpai119c1032015-10-29 01:21:04 -070094 BenchmarkService::Stub*, grpc::ClientContext*, const RequestType&,
Vijay Pai372fd872015-06-08 13:30:08 -070095 CompletionQueue*)> start_req,
Yang Gao6baa9b62015-03-17 10:49:39 -070096 std::function<void(grpc::Status, ResponseType*)> on_done)
Vijay Pai372fd872015-06-08 13:30:08 -070097 : ClientRpcContext(channel_id),
98 context_(),
Craig Tiller88568752015-03-04 10:50:43 -080099 stub_(stub),
Vijay Pai64ac47f2015-02-26 17:59:51 -0800100 req_(req),
101 response_(),
Craig Tiller3676b382015-05-06 13:01:05 -0700102 next_state_(&ClientRpcContextUnaryImpl::RespDone),
Craig Tillera182bf12015-03-04 13:54:39 -0800103 callback_(on_done),
Vijay Pai372fd872015-06-08 13:30:08 -0700104 start_req_(start_req) {}
105 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
106 start_ = Timer::Now();
107 response_reader_ = start_req_(stub_, &context_, req_, cq);
Craig Tiller3676b382015-05-06 13:01:05 -0700108 response_reader_->Finish(&response_, &status_, ClientRpcContext::tag(this));
109 }
vjpai3c110662015-02-27 07:17:16 -0800110 ~ClientRpcContextUnaryImpl() GRPC_OVERRIDE {}
Vijay Pai1da729a2015-03-23 12:52:56 -0700111 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -0700112 bool ret = (this->*next_state_)(ok);
113 if (!ret) {
114 hist->Add((Timer::Now() - start_) * 1e9);
115 }
116 return ret;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800117 }
118
Vijay Pai372fd872015-06-08 13:30:08 -0700119 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
120 return new ClientRpcContextUnaryImpl(channel_id_, stub_, req_, start_req_,
121 callback_);
Craig Tilleref638392015-03-04 12:23:12 -0800122 }
123
Vijay Pai64ac47f2015-02-26 17:59:51 -0800124 private:
vjpai46f65232015-03-23 10:10:27 -0700125 bool RespDone(bool) {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800126 next_state_ = &ClientRpcContextUnaryImpl::DoCallBack;
127 return false;
128 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700129 bool DoCallBack(bool) {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800130 callback_(status_, &response_);
Vijay Pai372fd872015-06-08 13:30:08 -0700131 return true; // we're done, this'll be ignored
Vijay Pai64ac47f2015-02-26 17:59:51 -0800132 }
133 grpc::ClientContext context_;
vjpai119c1032015-10-29 01:21:04 -0700134 BenchmarkService::Stub* stub_;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800135 RequestType req_;
136 ResponseType response_;
vjpai46f65232015-03-23 10:10:27 -0700137 bool (ClientRpcContextUnaryImpl::*next_state_)(bool);
Yang Gao6baa9b62015-03-17 10:49:39 -0700138 std::function<void(grpc::Status, ResponseType*)> callback_;
Craig Tillera182bf12015-03-04 13:54:39 -0800139 std::function<std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>(
vjpai119c1032015-10-29 01:21:04 -0700140 BenchmarkService::Stub*, grpc::ClientContext*, const RequestType&,
Vijay Pai372fd872015-06-08 13:30:08 -0700141 CompletionQueue*)> start_req_;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800142 grpc::Status status_;
143 double start_;
144 std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>
145 response_reader_;
146};
vjpaidea740f2015-02-26 16:35:35 -0800147
Vijay Pai372fd872015-06-08 13:30:08 -0700148typedef std::forward_list<ClientRpcContext*> context_list;
149
Vijay Paie10ebf12015-04-30 13:12:31 -0700150class AsyncClient : public Client {
Craig Tiller88568752015-03-04 10:50:43 -0800151 public:
Vijay Pai372fd872015-06-08 13:30:08 -0700152 explicit AsyncClient(
153 const ClientConfig& config,
vjpai119c1032015-10-29 01:21:04 -0700154 std::function<ClientRpcContext*(int, BenchmarkService::Stub*,
Vijay Pai372fd872015-06-08 13:30:08 -0700155 const SimpleRequest&)> setup_ctx)
156 : Client(config),
Vijay Pai90e73692015-08-05 19:15:36 -0700157 channel_lock_(new std::mutex[config.client_channels()]),
Vijay Pai372fd872015-06-08 13:30:08 -0700158 contexts_(config.client_channels()),
159 max_outstanding_per_channel_(config.outstanding_rpcs_per_channel()),
160 channel_count_(config.client_channels()),
161 pref_channel_inc_(config.async_client_threads()) {
162 SetupLoadTest(config, config.async_client_threads());
163
Craig Tiller88568752015-03-04 10:50:43 -0800164 for (int i = 0; i < config.async_client_threads(); i++) {
165 cli_cqs_.emplace_back(new CompletionQueue);
Vijay Pai372fd872015-06-08 13:30:08 -0700166 if (!closed_loop_) {
167 rpc_deadlines_.emplace_back();
168 next_channel_.push_back(i % channel_count_);
Vijay Paie2980cd2015-07-01 09:38:20 -0700169 issue_allowed_.emplace_back(true);
Vijay Pai372fd872015-06-08 13:30:08 -0700170
171 grpc_time next_issue;
172 NextIssueTime(i, &next_issue);
173 next_issue_.push_back(next_issue);
174 }
Craig Tiller88568752015-03-04 10:50:43 -0800175 }
Vijay Pai372fd872015-06-08 13:30:08 -0700176
vjpai6a608022015-05-18 09:16:53 -0700177 int t = 0;
178 for (int i = 0; i < config.outstanding_rpcs_per_channel(); i++) {
Vijay Pai372fd872015-06-08 13:30:08 -0700179 for (int ch = 0; ch < channel_count_; ch++) {
Vijay Pai7b172b22015-06-05 02:03:18 -0700180 auto* cq = cli_cqs_[t].get();
181 t = (t + 1) % cli_cqs_.size();
Vijay Pai372fd872015-06-08 13:30:08 -0700182 auto ctx = setup_ctx(ch, channels_[ch].get_stub(), request_);
183 if (closed_loop_) {
184 ctx->Start(cq);
185 } else {
186 contexts_[ch].push_front(ctx);
187 }
Craig Tiller88568752015-03-04 10:50:43 -0800188 }
189 }
Craig Tiller88568752015-03-04 10:50:43 -0800190 }
Vijay Paie10ebf12015-04-30 13:12:31 -0700191 virtual ~AsyncClient() {
Vijay Pai82dd80a2015-03-24 10:36:08 -0700192 for (auto cq = cli_cqs_.begin(); cq != cli_cqs_.end(); cq++) {
193 (*cq)->Shutdown();
Yang Gao6baa9b62015-03-17 10:49:39 -0700194 void* got_tag;
Craig Tilleref638392015-03-04 12:23:12 -0800195 bool ok;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700196 while ((*cq)->Next(&got_tag, &ok)) {
Craig Tilleref638392015-03-04 12:23:12 -0800197 delete ClientRpcContext::detag(got_tag);
198 }
199 }
Vijay Pai56061992015-07-01 13:39:48 -0700200 // Now clear out all the pre-allocated idle contexts
201 for (int ch = 0; ch < channel_count_; ch++) {
Vijay Pai13735d52015-07-01 14:00:08 -0700202 while (!contexts_[ch].empty()) {
Vijay Pai56061992015-07-01 13:39:48 -0700203 // Get an idle context from the front of the list
204 auto* ctx = *(contexts_[ch].begin());
205 contexts_[ch].pop_front();
206 delete ctx;
207 }
208 }
Vijay Paieed63fa2015-08-05 23:08:34 +0000209 delete[] channel_lock_;
Craig Tilleref638392015-03-04 12:23:12 -0800210 }
211
Craig Tiller5c8737d2015-05-21 11:42:17 -0700212 bool ThreadFunc(Histogram* histogram,
213 size_t thread_idx) GRPC_OVERRIDE GRPC_FINAL {
Yang Gao6baa9b62015-03-17 10:49:39 -0700214 void* got_tag;
Craig Tiller88568752015-03-04 10:50:43 -0800215 bool ok;
Vijay Pai372fd872015-06-08 13:30:08 -0700216 grpc_time deadline, short_deadline;
217 if (closed_loop_) {
218 deadline = grpc_time_source::now() + std::chrono::seconds(1);
219 short_deadline = deadline;
220 } else {
221 if (rpc_deadlines_[thread_idx].empty()) {
222 deadline = grpc_time_source::now() + std::chrono::seconds(1);
223 } else {
224 deadline = *(rpc_deadlines_[thread_idx].begin());
225 }
226 short_deadline =
227 issue_allowed_[thread_idx] ? next_issue_[thread_idx] : deadline;
228 }
229
230 bool got_event;
231
232 switch (cli_cqs_[thread_idx]->AsyncNext(&got_tag, &ok, short_deadline)) {
Vijay Paicf3fb092015-06-05 03:41:30 -0700233 case CompletionQueue::SHUTDOWN:
234 return false;
vjpai37f72572015-05-12 10:29:07 -0700235 case CompletionQueue::TIMEOUT:
Vijay Pai372fd872015-06-08 13:30:08 -0700236 got_event = false;
237 break;
vjpai37f72572015-05-12 10:29:07 -0700238 case CompletionQueue::GOT_EVENT:
Vijay Pai372fd872015-06-08 13:30:08 -0700239 got_event = true;
240 break;
241 default:
242 GPR_ASSERT(false);
Vijay Pai9dc5c152015-06-03 11:34:53 -0700243 break;
Craig Tiller1c61af72015-04-09 12:08:44 -0700244 }
Vijay Pai372fd872015-06-08 13:30:08 -0700245 if (got_event) {
246 ClientRpcContext* ctx = ClientRpcContext::detag(got_tag);
247 if (ctx->RunNextState(ok, histogram) == false) {
248 // call the callback and then clone the ctx
249 ctx->RunNextState(ok, histogram);
250 ClientRpcContext* clone_ctx = ctx->StartNewClone();
251 if (closed_loop_) {
252 clone_ctx->Start(cli_cqs_[thread_idx].get());
253 } else {
254 // Remove the entry from the rpc deadlines list
255 rpc_deadlines_[thread_idx].erase(ctx->deadline_posn());
256 // Put the clone_ctx in the list of idle contexts for this channel
257 // Under lock
258 int ch = clone_ctx->channel_id();
259 std::lock_guard<std::mutex> g(channel_lock_[ch]);
260 contexts_[ch].push_front(clone_ctx);
261 }
262 // delete the old version
263 delete ctx;
264 }
265 if (!closed_loop_)
266 issue_allowed_[thread_idx] =
267 true; // may be ok now even if it hadn't been
268 }
269 if (!closed_loop_ && issue_allowed_[thread_idx] &&
270 grpc_time_source::now() >= next_issue_[thread_idx]) {
271 // Attempt to issue
272 bool issued = false;
273 for (int num_attempts = 0, channel_attempt = next_channel_[thread_idx];
274 num_attempts < channel_count_ && !issued; num_attempts++) {
275 bool can_issue = false;
276 ClientRpcContext* ctx = nullptr;
277 {
278 std::lock_guard<std::mutex> g(channel_lock_[channel_attempt]);
279 if (!contexts_[channel_attempt].empty()) {
280 // Get an idle context from the front of the list
281 ctx = *(contexts_[channel_attempt].begin());
282 contexts_[channel_attempt].pop_front();
283 can_issue = true;
284 }
285 }
286 if (can_issue) {
287 // do the work to issue
288 rpc_deadlines_[thread_idx].emplace_back(grpc_time_source::now() +
289 std::chrono::seconds(1));
290 auto it = rpc_deadlines_[thread_idx].end();
291 --it;
292 ctx->set_deadline_posn(it);
293 ctx->Start(cli_cqs_[thread_idx].get());
294 issued = true;
295 // If we did issue, then next time, try our thread's next
296 // preferred channel
297 next_channel_[thread_idx] += pref_channel_inc_;
298 if (next_channel_[thread_idx] >= channel_count_)
299 next_channel_[thread_idx] = (thread_idx % channel_count_);
300 } else {
301 // Do a modular increment of channel attempt if we couldn't issue
302 channel_attempt = (channel_attempt + 1) % channel_count_;
303 }
304 }
305 if (issued) {
306 // We issued one; see when we can issue the next
307 grpc_time next_issue;
308 NextIssueTime(thread_idx, &next_issue);
309 next_issue_[thread_idx] = next_issue;
310 } else {
311 issue_allowed_[thread_idx] = false;
312 }
313 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700314 return true;
Craig Tiller88568752015-03-04 10:50:43 -0800315 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700316
Vijay Paie10ebf12015-04-30 13:12:31 -0700317 private:
Vijay Paiab1dba72015-07-31 09:09:09 -0700318 class boolean { // exists only to avoid data-race on vector<bool>
Vijay Paie2980cd2015-07-01 09:38:20 -0700319 public:
Vijay Paiab1dba72015-07-31 09:09:09 -0700320 boolean() : val_(false) {}
321 boolean(bool b) : val_(b) {}
322 operator bool() const { return val_; }
323 boolean& operator=(bool b) {
324 val_ = b;
325 return *this;
326 }
327
Vijay Paie2980cd2015-07-01 09:38:20 -0700328 private:
329 bool val_;
330 };
Craig Tiller88568752015-03-04 10:50:43 -0800331 std::vector<std::unique_ptr<CompletionQueue>> cli_cqs_;
Vijay Pai372fd872015-06-08 13:30:08 -0700332
333 std::vector<deadline_list> rpc_deadlines_; // per thread deadlines
Vijay Paiab1dba72015-07-31 09:09:09 -0700334 std::vector<int> next_channel_; // per thread round-robin channel ctr
335 std::vector<boolean> issue_allowed_; // may this thread attempt to issue
336 std::vector<grpc_time> next_issue_; // when should it issue?
Vijay Pai372fd872015-06-08 13:30:08 -0700337
Vijay Pai90e73692015-08-05 19:15:36 -0700338 std::mutex*
339 channel_lock_; // a vector, but avoid std::vector for old compilers
Vijay Pai372fd872015-06-08 13:30:08 -0700340 std::vector<context_list> contexts_; // per-channel list of idle contexts
341 int max_outstanding_per_channel_;
342 int channel_count_;
343 int pref_channel_inc_;
Craig Tiller88568752015-03-04 10:50:43 -0800344};
345
Vijay Paie10ebf12015-04-30 13:12:31 -0700346class AsyncUnaryClient GRPC_FINAL : public AsyncClient {
347 public:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700348 explicit AsyncUnaryClient(const ClientConfig& config)
349 : AsyncClient(config, SetupCtx) {
Vijay Paie10ebf12015-04-30 13:12:31 -0700350 StartThreads(config.async_client_threads());
351 }
352 ~AsyncUnaryClient() GRPC_OVERRIDE { EndThreads(); }
Vijay Paicf3fb092015-06-05 03:41:30 -0700353
354 private:
vjpai09d0b0c2015-07-31 08:39:54 -0700355 static void CheckDone(grpc::Status s, SimpleResponse* response) {}
356 static std::unique_ptr<grpc::ClientAsyncResponseReader<SimpleResponse>>
vjpai119c1032015-10-29 01:21:04 -0700357 StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx,
Vijay Paiab1dba72015-07-31 09:09:09 -0700358 const SimpleRequest& request, CompletionQueue* cq) {
vjpai09d0b0c2015-07-31 08:39:54 -0700359 return stub->AsyncUnaryCall(ctx, request, cq);
360 };
Vijay Paice846702015-11-04 00:30:12 -0800361 static ClientRpcContext* SetupCtx(int channel_id,
362 BenchmarkService::Stub* stub,
Vijay Pai372fd872015-06-08 13:30:08 -0700363 const SimpleRequest& req) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700364 return new ClientRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
365 channel_id, stub, req, AsyncUnaryClient::StartReq,
366 AsyncUnaryClient::CheckDone);
Vijay Paie10ebf12015-04-30 13:12:31 -0700367 }
368};
369
vjpai46f65232015-03-23 10:10:27 -0700370template <class RequestType, class ResponseType>
371class ClientRpcContextStreamingImpl : public ClientRpcContext {
372 public:
Vijay Paicf3fb092015-06-05 03:41:30 -0700373 ClientRpcContextStreamingImpl(
vjpai119c1032015-10-29 01:21:04 -0700374 int channel_id, BenchmarkService::Stub* stub, const RequestType& req,
Vijay Paice846702015-11-04 00:30:12 -0800375 std::function<std::unique_ptr<
376 grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>(
377 BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*,
378 void*)> start_req,
Craig Tiller5c8737d2015-05-21 11:42:17 -0700379 std::function<void(grpc::Status, ResponseType*)> on_done)
Vijay Pai372fd872015-06-08 13:30:08 -0700380 : ClientRpcContext(channel_id),
381 context_(),
vjpai46f65232015-03-23 10:10:27 -0700382 stub_(stub),
383 req_(req),
384 response_(),
385 next_state_(&ClientRpcContextStreamingImpl::ReqSent),
386 callback_(on_done),
387 start_req_(start_req),
Vijay Pai372fd872015-06-08 13:30:08 -0700388 start_(Timer::Now()) {}
vjpai46f65232015-03-23 10:10:27 -0700389 ~ClientRpcContextStreamingImpl() GRPC_OVERRIDE {}
Craig Tiller5c8737d2015-05-21 11:42:17 -0700390 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -0700391 return (this->*next_state_)(ok, hist);
392 }
Vijay Pai372fd872015-06-08 13:30:08 -0700393 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
394 return new ClientRpcContextStreamingImpl(channel_id_, stub_, req_,
395 start_req_, callback_);
396 }
397 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
398 stream_ = start_req_(stub_, &context_, cq, ClientRpcContext::tag(this));
Vijay Pai7b172b22015-06-05 02:03:18 -0700399 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700400
vjpai46f65232015-03-23 10:10:27 -0700401 private:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700402 bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); }
vjpai46f65232015-03-23 10:10:27 -0700403 bool StartWrite(bool ok) {
404 if (!ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700405 return (false);
vjpai46f65232015-03-23 10:10:27 -0700406 }
407 start_ = Timer::Now();
408 next_state_ = &ClientRpcContextStreamingImpl::WriteDone;
409 stream_->Write(req_, ClientRpcContext::tag(this));
410 return true;
411 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700412 bool WriteDone(bool ok, Histogram*) {
vjpai46f65232015-03-23 10:10:27 -0700413 if (!ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700414 return (false);
vjpai46f65232015-03-23 10:10:27 -0700415 }
416 next_state_ = &ClientRpcContextStreamingImpl::ReadDone;
Vijay Paie10ebf12015-04-30 13:12:31 -0700417 stream_->Read(&response_, ClientRpcContext::tag(this));
vjpai46f65232015-03-23 10:10:27 -0700418 return true;
419 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700420 bool ReadDone(bool ok, Histogram* hist) {
vjpai46f65232015-03-23 10:10:27 -0700421 hist->Add((Timer::Now() - start_) * 1e9);
422 return StartWrite(ok);
423 }
424 grpc::ClientContext context_;
vjpai119c1032015-10-29 01:21:04 -0700425 BenchmarkService::Stub* stub_;
vjpai46f65232015-03-23 10:10:27 -0700426 RequestType req_;
427 ResponseType response_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700428 bool (ClientRpcContextStreamingImpl::*next_state_)(bool, Histogram*);
429 std::function<void(grpc::Status, ResponseType*)> callback_;
430 std::function<
431 std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>(
Vijay Paice846702015-11-04 00:30:12 -0800432 BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*,
433 void*)> start_req_;
vjpai46f65232015-03-23 10:10:27 -0700434 grpc::Status status_;
435 double start_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700436 std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>
437 stream_;
vjpai46f65232015-03-23 10:10:27 -0700438};
439
Vijay Paie10ebf12015-04-30 13:12:31 -0700440class AsyncStreamingClient GRPC_FINAL : public AsyncClient {
vjpai46f65232015-03-23 10:10:27 -0700441 public:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700442 explicit AsyncStreamingClient(const ClientConfig& config)
443 : AsyncClient(config, SetupCtx) {
vjpai754751e2015-10-28 09:16:22 -0700444 // async streaming currently only supports closed loop
445 GPR_ASSERT(closed_loop_);
Vijay Pai372fd872015-06-08 13:30:08 -0700446
vjpai46f65232015-03-23 10:10:27 -0700447 StartThreads(config.async_client_threads());
448 }
449
Vijay Paie10ebf12015-04-30 13:12:31 -0700450 ~AsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); }
Vijay Paicf3fb092015-06-05 03:41:30 -0700451
452 private:
vjpai09d0b0c2015-07-31 08:39:54 -0700453 static void CheckDone(grpc::Status s, SimpleResponse* response) {}
Vijay Paiab1dba72015-07-31 09:09:09 -0700454 static std::unique_ptr<
455 grpc::ClientAsyncReaderWriter<SimpleRequest, SimpleResponse>>
vjpai119c1032015-10-29 01:21:04 -0700456 StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx,
Vijay Paiab1dba72015-07-31 09:09:09 -0700457 CompletionQueue* cq, void* tag) {
vjpai09d0b0c2015-07-31 08:39:54 -0700458 auto stream = stub->AsyncStreamingCall(ctx, cq, tag);
459 return stream;
460 };
Vijay Paice846702015-11-04 00:30:12 -0800461 static ClientRpcContext* SetupCtx(int channel_id,
462 BenchmarkService::Stub* stub,
Vijay Pai372fd872015-06-08 13:30:08 -0700463 const SimpleRequest& req) {
Vijay Pai372fd872015-06-08 13:30:08 -0700464 return new ClientRpcContextStreamingImpl<SimpleRequest, SimpleResponse>(
Vijay Paiab1dba72015-07-31 09:09:09 -0700465 channel_id, stub, req, AsyncStreamingClient::StartReq,
466 AsyncStreamingClient::CheckDone);
vjpai46f65232015-03-23 10:10:27 -0700467 }
vjpai46f65232015-03-23 10:10:27 -0700468};
469
Vijay Pai1da729a2015-03-23 12:52:56 -0700470std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args) {
vjpai46f65232015-03-23 10:10:27 -0700471 return std::unique_ptr<Client>(new AsyncUnaryClient(args));
472}
Vijay Pai1da729a2015-03-23 12:52:56 -0700473std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args) {
vjpai46f65232015-03-23 10:10:27 -0700474 return std::unique_ptr<Client>(new AsyncStreamingClient(args));
Craig Tiller88568752015-03-04 10:50:43 -0800475}
476
Craig Tillera182bf12015-03-04 13:54:39 -0800477} // namespace testing
478} // namespace grpc