blob: fdfe1a567ae602d377e333d0203fa6128cca55e9 [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>
Vijay Paie4886682015-12-30 11:56:19 -080050#include <grpc++/generic/generic_stub.h>
yang-g9e2f90c2015-08-21 15:35:03 -070051
Craig Tiller88568752015-03-04 10:50:43 -080052#include "test/cpp/qps/timer.h"
53#include "test/cpp/qps/client.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 Pai372fd872015-06-08 13:30:08 -070096 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&,
Vijay Pai372fd872015-06-08 13:30:08 -0700142 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_;
159 using ClientImpl<StubType,RequestType>::channels_;
160 using ClientImpl<StubType,RequestType>::request_;
vjpaib6df94a2015-11-30 15:52:50 -0800161 AsyncClient(
Vijay Pai372fd872015-06-08 13:30:08 -0700162 const ClientConfig& config,
vjpaib6df94a2015-11-30 15:52:50 -0800163 std::function<ClientRpcContext*(int, StubType*, const RequestType&)> setup_ctx,
164 std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> create_stub)
Vijay Paie4886682015-12-30 11:56:19 -0800165 : ClientImpl<StubType,RequestType>(config, create_stub),
Vijay Pai90e73692015-08-05 19:15:36 -0700166 channel_lock_(new std::mutex[config.client_channels()]),
Vijay Pai372fd872015-06-08 13:30:08 -0700167 contexts_(config.client_channels()),
168 max_outstanding_per_channel_(config.outstanding_rpcs_per_channel()),
169 channel_count_(config.client_channels()),
170 pref_channel_inc_(config.async_client_threads()) {
171 SetupLoadTest(config, config.async_client_threads());
172
Craig Tiller88568752015-03-04 10:50:43 -0800173 for (int i = 0; i < config.async_client_threads(); i++) {
174 cli_cqs_.emplace_back(new CompletionQueue);
Vijay Pai372fd872015-06-08 13:30:08 -0700175 if (!closed_loop_) {
176 rpc_deadlines_.emplace_back();
177 next_channel_.push_back(i % channel_count_);
Vijay Paie2980cd2015-07-01 09:38:20 -0700178 issue_allowed_.emplace_back(true);
Vijay Pai372fd872015-06-08 13:30:08 -0700179
180 grpc_time next_issue;
181 NextIssueTime(i, &next_issue);
182 next_issue_.push_back(next_issue);
183 }
Craig Tiller88568752015-03-04 10:50:43 -0800184 }
Vijay Pai372fd872015-06-08 13:30:08 -0700185
vjpai6a608022015-05-18 09:16:53 -0700186 int t = 0;
187 for (int i = 0; i < config.outstanding_rpcs_per_channel(); i++) {
Vijay Pai372fd872015-06-08 13:30:08 -0700188 for (int ch = 0; ch < channel_count_; ch++) {
Vijay Pai7b172b22015-06-05 02:03:18 -0700189 auto* cq = cli_cqs_[t].get();
190 t = (t + 1) % cli_cqs_.size();
Vijay Pai372fd872015-06-08 13:30:08 -0700191 auto ctx = setup_ctx(ch, channels_[ch].get_stub(), request_);
192 if (closed_loop_) {
193 ctx->Start(cq);
194 } else {
195 contexts_[ch].push_front(ctx);
196 }
Craig Tiller88568752015-03-04 10:50:43 -0800197 }
198 }
Craig Tiller88568752015-03-04 10:50:43 -0800199 }
Vijay Paie10ebf152015-04-30 13:12:31 -0700200 virtual ~AsyncClient() {
Vijay Pai82dd80a2015-03-24 10:36:08 -0700201 for (auto cq = cli_cqs_.begin(); cq != cli_cqs_.end(); cq++) {
202 (*cq)->Shutdown();
Yang Gao6baa9b62015-03-17 10:49:39 -0700203 void* got_tag;
Craig Tilleref638392015-03-04 12:23:12 -0800204 bool ok;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700205 while ((*cq)->Next(&got_tag, &ok)) {
Craig Tilleref638392015-03-04 12:23:12 -0800206 delete ClientRpcContext::detag(got_tag);
207 }
208 }
Vijay Pai56061992015-07-01 13:39:48 -0700209 // Now clear out all the pre-allocated idle contexts
210 for (int ch = 0; ch < channel_count_; ch++) {
Vijay Pai13735d52015-07-01 14:00:08 -0700211 while (!contexts_[ch].empty()) {
Vijay Pai56061992015-07-01 13:39:48 -0700212 // Get an idle context from the front of the list
213 auto* ctx = *(contexts_[ch].begin());
214 contexts_[ch].pop_front();
215 delete ctx;
216 }
217 }
Vijay Paieed63fa2015-08-05 23:08:34 +0000218 delete[] channel_lock_;
Craig Tilleref638392015-03-04 12:23:12 -0800219 }
220
Craig Tiller5c8737d2015-05-21 11:42:17 -0700221 bool ThreadFunc(Histogram* histogram,
222 size_t thread_idx) GRPC_OVERRIDE GRPC_FINAL {
Yang Gao6baa9b62015-03-17 10:49:39 -0700223 void* got_tag;
Craig Tiller88568752015-03-04 10:50:43 -0800224 bool ok;
Vijay Pai372fd872015-06-08 13:30:08 -0700225 grpc_time deadline, short_deadline;
226 if (closed_loop_) {
227 deadline = grpc_time_source::now() + std::chrono::seconds(1);
228 short_deadline = deadline;
229 } else {
230 if (rpc_deadlines_[thread_idx].empty()) {
231 deadline = grpc_time_source::now() + std::chrono::seconds(1);
232 } else {
233 deadline = *(rpc_deadlines_[thread_idx].begin());
234 }
235 short_deadline =
236 issue_allowed_[thread_idx] ? next_issue_[thread_idx] : deadline;
237 }
238
239 bool got_event;
240
241 switch (cli_cqs_[thread_idx]->AsyncNext(&got_tag, &ok, short_deadline)) {
Vijay Paicf3fb092015-06-05 03:41:30 -0700242 case CompletionQueue::SHUTDOWN:
243 return false;
vjpai37f72572015-05-12 10:29:07 -0700244 case CompletionQueue::TIMEOUT:
Vijay Pai372fd872015-06-08 13:30:08 -0700245 got_event = false;
246 break;
vjpai37f72572015-05-12 10:29:07 -0700247 case CompletionQueue::GOT_EVENT:
Vijay Pai372fd872015-06-08 13:30:08 -0700248 got_event = true;
249 break;
250 default:
251 GPR_ASSERT(false);
Vijay Pai9dc5c152015-06-03 11:34:53 -0700252 break;
Craig Tiller1c61af72015-04-09 12:08:44 -0700253 }
Vijay Pai372fd872015-06-08 13:30:08 -0700254 if (got_event) {
255 ClientRpcContext* ctx = ClientRpcContext::detag(got_tag);
256 if (ctx->RunNextState(ok, histogram) == false) {
257 // call the callback and then clone the ctx
258 ctx->RunNextState(ok, histogram);
259 ClientRpcContext* clone_ctx = ctx->StartNewClone();
260 if (closed_loop_) {
261 clone_ctx->Start(cli_cqs_[thread_idx].get());
262 } else {
263 // Remove the entry from the rpc deadlines list
264 rpc_deadlines_[thread_idx].erase(ctx->deadline_posn());
265 // Put the clone_ctx in the list of idle contexts for this channel
266 // Under lock
267 int ch = clone_ctx->channel_id();
268 std::lock_guard<std::mutex> g(channel_lock_[ch]);
269 contexts_[ch].push_front(clone_ctx);
270 }
271 // delete the old version
272 delete ctx;
273 }
274 if (!closed_loop_)
275 issue_allowed_[thread_idx] =
276 true; // may be ok now even if it hadn't been
277 }
278 if (!closed_loop_ && issue_allowed_[thread_idx] &&
279 grpc_time_source::now() >= next_issue_[thread_idx]) {
280 // Attempt to issue
281 bool issued = false;
282 for (int num_attempts = 0, channel_attempt = next_channel_[thread_idx];
283 num_attempts < channel_count_ && !issued; num_attempts++) {
284 bool can_issue = false;
285 ClientRpcContext* ctx = nullptr;
286 {
287 std::lock_guard<std::mutex> g(channel_lock_[channel_attempt]);
288 if (!contexts_[channel_attempt].empty()) {
289 // Get an idle context from the front of the list
290 ctx = *(contexts_[channel_attempt].begin());
291 contexts_[channel_attempt].pop_front();
292 can_issue = true;
293 }
294 }
295 if (can_issue) {
296 // do the work to issue
297 rpc_deadlines_[thread_idx].emplace_back(grpc_time_source::now() +
298 std::chrono::seconds(1));
299 auto it = rpc_deadlines_[thread_idx].end();
300 --it;
301 ctx->set_deadline_posn(it);
302 ctx->Start(cli_cqs_[thread_idx].get());
303 issued = true;
304 // If we did issue, then next time, try our thread's next
305 // preferred channel
306 next_channel_[thread_idx] += pref_channel_inc_;
307 if (next_channel_[thread_idx] >= channel_count_)
308 next_channel_[thread_idx] = (thread_idx % channel_count_);
309 } else {
310 // Do a modular increment of channel attempt if we couldn't issue
311 channel_attempt = (channel_attempt + 1) % channel_count_;
312 }
313 }
314 if (issued) {
315 // We issued one; see when we can issue the next
316 grpc_time next_issue;
317 NextIssueTime(thread_idx, &next_issue);
318 next_issue_[thread_idx] = next_issue;
319 } else {
320 issue_allowed_[thread_idx] = false;
321 }
322 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700323 return true;
Craig Tiller88568752015-03-04 10:50:43 -0800324 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700325
Vijay Paie10ebf152015-04-30 13:12:31 -0700326 private:
Vijay Paiab1dba72015-07-31 09:09:09 -0700327 class boolean { // exists only to avoid data-race on vector<bool>
Vijay Paie2980cd2015-07-01 09:38:20 -0700328 public:
Vijay Paiab1dba72015-07-31 09:09:09 -0700329 boolean() : val_(false) {}
330 boolean(bool b) : val_(b) {}
331 operator bool() const { return val_; }
332 boolean& operator=(bool b) {
333 val_ = b;
334 return *this;
335 }
336
Vijay Paie2980cd2015-07-01 09:38:20 -0700337 private:
338 bool val_;
339 };
Craig Tiller88568752015-03-04 10:50:43 -0800340 std::vector<std::unique_ptr<CompletionQueue>> cli_cqs_;
Vijay Pai372fd872015-06-08 13:30:08 -0700341
342 std::vector<deadline_list> rpc_deadlines_; // per thread deadlines
Vijay Paiab1dba72015-07-31 09:09:09 -0700343 std::vector<int> next_channel_; // per thread round-robin channel ctr
344 std::vector<boolean> issue_allowed_; // may this thread attempt to issue
345 std::vector<grpc_time> next_issue_; // when should it issue?
Vijay Pai372fd872015-06-08 13:30:08 -0700346
Vijay Pai90e73692015-08-05 19:15:36 -0700347 std::mutex*
348 channel_lock_; // a vector, but avoid std::vector for old compilers
Vijay Pai372fd872015-06-08 13:30:08 -0700349 std::vector<context_list> contexts_; // per-channel list of idle contexts
350 int max_outstanding_per_channel_;
351 int channel_count_;
352 int pref_channel_inc_;
Craig Tiller88568752015-03-04 10:50:43 -0800353};
354
Vijay Paie4886682015-12-30 11:56:19 -0800355class AsyncUnaryClient GRPC_FINAL :
356 public AsyncClient<BenchmarkService::Stub, SimpleRequest> {
Vijay Paie10ebf152015-04-30 13:12:31 -0700357 public:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700358 explicit AsyncUnaryClient(const ClientConfig& config)
vjpaib6df94a2015-11-30 15:52:50 -0800359 : AsyncClient(config, SetupCtx, BenchmarkService::NewStub) {
Vijay Paie10ebf152015-04-30 13:12:31 -0700360 StartThreads(config.async_client_threads());
361 }
362 ~AsyncUnaryClient() GRPC_OVERRIDE { EndThreads(); }
Vijay Paicf3fb092015-06-05 03:41:30 -0700363
364 private:
vjpai09d0b0c2015-07-31 08:39:54 -0700365 static void CheckDone(grpc::Status s, SimpleResponse* response) {}
366 static std::unique_ptr<grpc::ClientAsyncResponseReader<SimpleResponse>>
vjpai119c1032015-10-29 01:21:04 -0700367 StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx,
Vijay Paiab1dba72015-07-31 09:09:09 -0700368 const SimpleRequest& request, CompletionQueue* cq) {
vjpai09d0b0c2015-07-31 08:39:54 -0700369 return stub->AsyncUnaryCall(ctx, request, cq);
370 };
Vijay Paice846702015-11-04 00:30:12 -0800371 static ClientRpcContext* SetupCtx(int channel_id,
372 BenchmarkService::Stub* stub,
Vijay Pai372fd872015-06-08 13:30:08 -0700373 const SimpleRequest& req) {
Vijay Paiab1dba72015-07-31 09:09:09 -0700374 return new ClientRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
375 channel_id, stub, req, AsyncUnaryClient::StartReq,
376 AsyncUnaryClient::CheckDone);
Vijay Paie10ebf152015-04-30 13:12:31 -0700377 }
378};
379
vjpai46f65232015-03-23 10:10:27 -0700380template <class RequestType, class ResponseType>
381class ClientRpcContextStreamingImpl : public ClientRpcContext {
382 public:
Vijay Paicf3fb092015-06-05 03:41:30 -0700383 ClientRpcContextStreamingImpl(
vjpai119c1032015-10-29 01:21:04 -0700384 int channel_id, BenchmarkService::Stub* stub, const RequestType& req,
Vijay Paice846702015-11-04 00:30:12 -0800385 std::function<std::unique_ptr<
386 grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>(
387 BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*,
388 void*)> start_req,
Craig Tiller5c8737d2015-05-21 11:42:17 -0700389 std::function<void(grpc::Status, ResponseType*)> on_done)
Vijay Pai372fd872015-06-08 13:30:08 -0700390 : ClientRpcContext(channel_id),
391 context_(),
vjpai46f65232015-03-23 10:10:27 -0700392 stub_(stub),
393 req_(req),
394 response_(),
395 next_state_(&ClientRpcContextStreamingImpl::ReqSent),
396 callback_(on_done),
397 start_req_(start_req),
Vijay Pai372fd872015-06-08 13:30:08 -0700398 start_(Timer::Now()) {}
vjpai46f65232015-03-23 10:10:27 -0700399 ~ClientRpcContextStreamingImpl() GRPC_OVERRIDE {}
Craig Tiller5c8737d2015-05-21 11:42:17 -0700400 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -0700401 return (this->*next_state_)(ok, hist);
402 }
Vijay Pai372fd872015-06-08 13:30:08 -0700403 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
404 return new ClientRpcContextStreamingImpl(channel_id_, stub_, req_,
405 start_req_, callback_);
406 }
407 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
408 stream_ = start_req_(stub_, &context_, cq, ClientRpcContext::tag(this));
Vijay Pai7b172b22015-06-05 02:03:18 -0700409 }
Vijay Paicf3fb092015-06-05 03:41:30 -0700410
vjpai46f65232015-03-23 10:10:27 -0700411 private:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700412 bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); }
vjpai46f65232015-03-23 10:10:27 -0700413 bool StartWrite(bool ok) {
414 if (!ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700415 return (false);
vjpai46f65232015-03-23 10:10:27 -0700416 }
417 start_ = Timer::Now();
418 next_state_ = &ClientRpcContextStreamingImpl::WriteDone;
419 stream_->Write(req_, ClientRpcContext::tag(this));
420 return true;
421 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700422 bool WriteDone(bool ok, Histogram*) {
vjpai46f65232015-03-23 10:10:27 -0700423 if (!ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700424 return (false);
vjpai46f65232015-03-23 10:10:27 -0700425 }
426 next_state_ = &ClientRpcContextStreamingImpl::ReadDone;
Vijay Paie10ebf152015-04-30 13:12:31 -0700427 stream_->Read(&response_, ClientRpcContext::tag(this));
vjpai46f65232015-03-23 10:10:27 -0700428 return true;
429 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700430 bool ReadDone(bool ok, Histogram* hist) {
vjpai46f65232015-03-23 10:10:27 -0700431 hist->Add((Timer::Now() - start_) * 1e9);
432 return StartWrite(ok);
433 }
434 grpc::ClientContext context_;
vjpai119c1032015-10-29 01:21:04 -0700435 BenchmarkService::Stub* stub_;
vjpai46f65232015-03-23 10:10:27 -0700436 RequestType req_;
437 ResponseType response_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700438 bool (ClientRpcContextStreamingImpl::*next_state_)(bool, Histogram*);
439 std::function<void(grpc::Status, ResponseType*)> callback_;
440 std::function<
441 std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>(
Vijay Paice846702015-11-04 00:30:12 -0800442 BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*,
443 void*)> start_req_;
vjpai46f65232015-03-23 10:10:27 -0700444 grpc::Status status_;
445 double start_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700446 std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>
447 stream_;
vjpai46f65232015-03-23 10:10:27 -0700448};
449
vjpaib6df94a2015-11-30 15:52:50 -0800450class AsyncStreamingClient GRPC_FINAL : public AsyncClient<BenchmarkService::Stub, SimpleRequest> {
vjpai46f65232015-03-23 10:10:27 -0700451 public:
Craig Tiller5c8737d2015-05-21 11:42:17 -0700452 explicit AsyncStreamingClient(const ClientConfig& config)
vjpaib6df94a2015-11-30 15:52:50 -0800453 : AsyncClient(config, SetupCtx, BenchmarkService::NewStub) {
vjpai754751e2015-10-28 09:16:22 -0700454 // async streaming currently only supports closed loop
455 GPR_ASSERT(closed_loop_);
Vijay Pai372fd872015-06-08 13:30:08 -0700456
vjpai46f65232015-03-23 10:10:27 -0700457 StartThreads(config.async_client_threads());
458 }
459
Vijay Paie10ebf152015-04-30 13:12:31 -0700460 ~AsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); }
Vijay Paicf3fb092015-06-05 03:41:30 -0700461
462 private:
vjpai09d0b0c2015-07-31 08:39:54 -0700463 static void CheckDone(grpc::Status s, SimpleResponse* response) {}
Vijay Paiab1dba72015-07-31 09:09:09 -0700464 static std::unique_ptr<
465 grpc::ClientAsyncReaderWriter<SimpleRequest, SimpleResponse>>
vjpai119c1032015-10-29 01:21:04 -0700466 StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx,
Vijay Paiab1dba72015-07-31 09:09:09 -0700467 CompletionQueue* cq, void* tag) {
vjpai09d0b0c2015-07-31 08:39:54 -0700468 auto stream = stub->AsyncStreamingCall(ctx, cq, tag);
469 return stream;
470 };
Vijay Paice846702015-11-04 00:30:12 -0800471 static ClientRpcContext* SetupCtx(int channel_id,
472 BenchmarkService::Stub* stub,
Vijay Pai372fd872015-06-08 13:30:08 -0700473 const SimpleRequest& req) {
Vijay Pai372fd872015-06-08 13:30:08 -0700474 return new ClientRpcContextStreamingImpl<SimpleRequest, SimpleResponse>(
Vijay Paiab1dba72015-07-31 09:09:09 -0700475 channel_id, stub, req, AsyncStreamingClient::StartReq,
476 AsyncStreamingClient::CheckDone);
vjpai46f65232015-03-23 10:10:27 -0700477 }
vjpai46f65232015-03-23 10:10:27 -0700478};
479
vjpaib6df94a2015-11-30 15:52:50 -0800480class ClientGenericRpcContextStreamingImpl : public ClientRpcContext {
481 public:
482 ClientGenericRpcContextStreamingImpl(
483 int channel_id, grpc::GenericStub* stub, const ByteBuffer& req,
484 std::function<std::unique_ptr<
485 grpc::GenericClientAsyncReaderWriter>(
486 grpc::GenericStub*, grpc::ClientContext*, const grpc::string& method_name,
487 CompletionQueue*, void*)> start_req,
488 std::function<void(grpc::Status, ByteBuffer*)> on_done)
489 : ClientRpcContext(channel_id),
490 context_(),
491 stub_(stub),
492 req_(req),
493 response_(),
494 next_state_(&ClientGenericRpcContextStreamingImpl::ReqSent),
495 callback_(on_done),
496 start_req_(start_req),
497 start_(Timer::Now()) {}
498 ~ClientGenericRpcContextStreamingImpl() GRPC_OVERRIDE {}
499 bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE {
500 return (this->*next_state_)(ok, hist);
501 }
502 ClientRpcContext* StartNewClone() GRPC_OVERRIDE {
503 return new ClientGenericRpcContextStreamingImpl(channel_id_, stub_, req_,
504 start_req_, callback_);
505 }
506 void Start(CompletionQueue* cq) GRPC_OVERRIDE {
507 const grpc::string kMethodName("/grpc.testing.BenchmarkService/StreamingCall");
508 stream_ = start_req_(stub_, &context_, kMethodName, cq, ClientRpcContext::tag(this));
509 }
510
511 private:
512 bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); }
513 bool StartWrite(bool ok) {
514 if (!ok) {
515 return (false);
516 }
517 start_ = Timer::Now();
518 next_state_ = &ClientGenericRpcContextStreamingImpl::WriteDone;
519 stream_->Write(req_, ClientRpcContext::tag(this));
520 return true;
521 }
522 bool WriteDone(bool ok, Histogram*) {
523 if (!ok) {
524 return (false);
525 }
526 next_state_ = &ClientGenericRpcContextStreamingImpl::ReadDone;
527 stream_->Read(&response_, ClientRpcContext::tag(this));
528 return true;
529 }
530 bool ReadDone(bool ok, Histogram* hist) {
531 hist->Add((Timer::Now() - start_) * 1e9);
532 return StartWrite(ok);
533 }
534 grpc::ClientContext context_;
535 grpc::GenericStub* stub_;
536 ByteBuffer req_;
537 ByteBuffer response_;
538 bool (ClientGenericRpcContextStreamingImpl::*next_state_)(bool, Histogram*);
539 std::function<void(grpc::Status, ByteBuffer*)> callback_;
540 std::function<
541 std::unique_ptr<grpc::GenericClientAsyncReaderWriter>(
542 grpc::GenericStub*, grpc::ClientContext*, const grpc::string&, CompletionQueue*,
543 void*)> start_req_;
544 grpc::Status status_;
545 double start_;
546 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> stream_;
547};
548
549class GenericAsyncStreamingClient GRPC_FINAL : public AsyncClient<grpc::GenericStub, ByteBuffer> {
550 public:
551 explicit GenericAsyncStreamingClient(const ClientConfig& config)
552 : AsyncClient(config, SetupCtx, grpc::GenericStub) {
553 // async streaming currently only supports closed loop
554 GPR_ASSERT(closed_loop_);
555
556 StartThreads(config.async_client_threads());
557 }
558
559 ~GenericAsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); }
560
561 private:
562 static void CheckDone(grpc::Status s, ByteBuffer* response) {}
563 static std::unique_ptr<grpc::GenericClientAsyncReaderWriter>
564 StartReq(grpc::GenericStub* stub, grpc::ClientContext* ctx,
565 const grpc::string& method_name, CompletionQueue* cq, void* tag) {
566 auto stream = stub->Call(ctx, method_name, cq, tag);
567 return stream;
568 };
569 static ClientRpcContext* SetupCtx(int channel_id,
570 grpc::GenericStub* stub,
Vijay Paie4886682015-12-30 11:56:19 -0800571 const ByteBuffer& req) {
572 return new ClientGenericRpcContextStreamingImpl(
573 channel_id, stub, req, GenericAsyncStreamingClient::StartReq,
574 GenericAsyncStreamingClient::CheckDone);
vjpaib6df94a2015-11-30 15:52:50 -0800575 }
576};
577
Vijay Pai1da729a2015-03-23 12:52:56 -0700578std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args) {
vjpai46f65232015-03-23 10:10:27 -0700579 return std::unique_ptr<Client>(new AsyncUnaryClient(args));
580}
Vijay Pai1da729a2015-03-23 12:52:56 -0700581std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args) {
vjpai46f65232015-03-23 10:10:27 -0700582 return std::unique_ptr<Client>(new AsyncStreamingClient(args));
Craig Tiller88568752015-03-04 10:50:43 -0800583}
vjpaib6df94a2015-11-30 15:52:50 -0800584std::unique_ptr<Client> CreateGenericAsyncStreamingClient(const ClientConfig& args) {
585 return std::unique_ptr<Client>(new GenericAsyncStreamingClient(args));
586}
Craig Tiller88568752015-03-04 10:50:43 -0800587
Craig Tillera182bf12015-03-04 13:54:39 -0800588} // namespace testing
589} // namespace grpc