blob: c88e95b80e5dabcf3aec7c15b6c7ab9861b4c30f [file] [log] [blame]
vpai80b6d012014-12-17 11:47:32 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
vpai80b6d012014-12-17 11:47:32 -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
vpai80b6d012014-12-17 11:47:32 -080034#include <cassert>
Vijay Pai372fd872015-06-08 13:30:08 -070035#include <chrono>
vpai80b6d012014-12-17 11:47:32 -080036#include <memory>
Craig Tiller5c004c62015-02-23 16:31:57 -080037#include <mutex>
Vijay Pai18c04772016-01-04 09:52:10 -080038#include <sstream>
vpai80b6d012014-12-17 11:47:32 -080039#include <string>
40#include <thread>
41#include <vector>
vpai80b6d012014-12-17 11:47:32 -080042
David Garcia Quintas6a484052016-01-25 19:12:37 -080043#include <grpc++/channel.h>
Vijay Pai18c04772016-01-04 09:52:10 -080044#include <grpc++/client_context.h>
45#include <grpc++/server.h>
46#include <grpc++/server_builder.h>
vpai80b6d012014-12-17 11:47:32 -080047#include <grpc/grpc.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080048#include <grpc/support/alloc.h>
vpai80b6d012014-12-17 11:47:32 -080049#include <grpc/support/histogram.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080050#include <grpc/support/host_port.h>
Vijay Paibc78c202015-07-31 11:24:06 -070051#include <grpc/support/log.h>
52#include <grpc/support/time.h>
vjpai46f65232015-03-23 10:10:27 -070053#include <gtest/gtest.h>
yang-g9e2f90c2015-08-21 15:35:03 -070054
Craig Tiller9533d042016-03-25 17:11:06 -070055#include "src/core/lib/profiling/timers.h"
Vijay Pai5bd6f2e2016-01-11 10:33:39 -080056#include "src/proto/grpc/testing/services.grpc.pb.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080057#include "test/cpp/qps/client.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080058#include "test/cpp/qps/histogram.h"
Vijay Pai372fd872015-06-08 13:30:08 -070059#include "test/cpp/qps/interarrival.h"
Craig Tiller732a8752016-02-22 15:59:19 -080060#include "test/cpp/qps/usage_timer.h"
Craig Tiller86253ca2015-10-08 13:31:02 -070061
Craig Tiller6af9ed02015-03-02 22:42:10 -080062namespace grpc {
63namespace testing {
vpai80b6d012014-12-17 11:47:32 -080064
Vijay Pai18c04772016-01-04 09:52:10 -080065static std::unique_ptr<BenchmarkService::Stub> BenchmarkStubCreator(
66 std::shared_ptr<Channel> ch) {
67 return BenchmarkService::NewStub(ch);
68}
69
70class SynchronousClient
71 : public ClientImpl<BenchmarkService::Stub, SimpleRequest> {
Craig Tiller6af9ed02015-03-02 22:42:10 -080072 public:
Vijay Pai18c04772016-01-04 09:52:10 -080073 SynchronousClient(const ClientConfig& config)
74 : ClientImpl<BenchmarkService::Stub, SimpleRequest>(
75 config, BenchmarkStubCreator) {
vjpai46f65232015-03-23 10:10:27 -070076 num_threads_ =
Vijay Pai36ff3f02015-05-04 14:40:21 -070077 config.outstanding_rpcs_per_channel() * config.client_channels();
vjpai46f65232015-03-23 10:10:27 -070078 responses_.resize(num_threads_);
Vijay Pai372fd872015-06-08 13:30:08 -070079 SetupLoadTest(config, num_threads_);
Craig Tiller6af9ed02015-03-02 22:42:10 -080080 }
vpai80b6d012014-12-17 11:47:32 -080081
Vijay Pai36ff3f02015-05-04 14:40:21 -070082 virtual ~SynchronousClient(){};
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010083
vjpai46f65232015-03-23 10:10:27 -070084 protected:
Vijay Pai372fd872015-06-08 13:30:08 -070085 void WaitToIssue(int thread_idx) {
Vijay Pai1132c6b2016-02-11 06:05:24 -080086 if (!closed_loop_) {
87 gpr_sleep_until(NextIssueTime(thread_idx));
Vijay Pai372fd872015-06-08 13:30:08 -070088 }
89 }
90
vjpai46f65232015-03-23 10:10:27 -070091 size_t num_threads_;
92 std::vector<SimpleResponse> responses_;
93};
94
95class SynchronousUnaryClient GRPC_FINAL : public SynchronousClient {
96 public:
Vijay Pai36ff3f02015-05-04 14:40:21 -070097 SynchronousUnaryClient(const ClientConfig& config)
98 : SynchronousClient(config) {
99 StartThreads(num_threads_);
100 }
101 ~SynchronousUnaryClient() { EndThreads(); }
Vijay Pai0b941272015-04-29 13:58:36 -0700102
Craig Tiller8a5a6662015-04-09 11:31:28 -0700103 bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
Vijay Pai372fd872015-06-08 13:30:08 -0700104 WaitToIssue(thread_idx);
Craig Tiller88568752015-03-04 10:50:43 -0800105 auto* stub = channels_[thread_idx % channels_.size()].get_stub();
vjpaie2d39e02016-02-24 12:36:15 -0800106 double start = UsageTimer::Now();
Craig Tiller0ba432d2015-10-09 16:57:11 -0700107 GPR_TIMER_SCOPE("SynchronousUnaryClient::ThreadFunc", 0);
Craig Tiller88568752015-03-04 10:50:43 -0800108 grpc::ClientContext context;
Craig Tillera182bf12015-03-04 13:54:39 -0800109 grpc::Status s =
110 stub->UnaryCall(&context, request_, &responses_[thread_idx]);
vjpaie2d39e02016-02-24 12:36:15 -0800111 histogram->Add((UsageTimer::Now() - start) * 1e9);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700112 return s.ok();
Craig Tiller6af9ed02015-03-02 22:42:10 -0800113 }
Craig Tiller5c004c62015-02-23 16:31:57 -0800114};
115
vjpai46f65232015-03-23 10:10:27 -0700116class SynchronousStreamingClient GRPC_FINAL : public SynchronousClient {
117 public:
Vijay Pai36ff3f02015-05-04 14:40:21 -0700118 SynchronousStreamingClient(const ClientConfig& config)
Vijay Pai90e73692015-08-05 19:15:36 -0700119 : SynchronousClient(config) {
Vijay Paieed63fa2015-08-05 23:08:34 +0000120 context_ = new grpc::ClientContext[num_threads_];
121 stream_ = new std::unique_ptr<
Vijay Pai90e73692015-08-05 19:15:36 -0700122 grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>>[num_threads_];
Vijay Pai36ff3f02015-05-04 14:40:21 -0700123 for (size_t thread_idx = 0; thread_idx < num_threads_; thread_idx++) {
vjpai46f65232015-03-23 10:10:27 -0700124 auto* stub = channels_[thread_idx % channels_.size()].get_stub();
vjpai0a412932015-05-04 16:39:18 -0700125 stream_[thread_idx] = stub->StreamingCall(&context_[thread_idx]);
vjpai46f65232015-03-23 10:10:27 -0700126 }
127 StartThreads(num_threads_);
128 }
129 ~SynchronousStreamingClient() {
Craig Tiller77e0d642015-04-27 10:52:54 -0700130 EndThreads();
Vijay Pai90e73692015-08-05 19:15:36 -0700131 for (auto stream = &stream_[0]; stream != &stream_[num_threads_];
132 stream++) {
vjpai0a412932015-05-04 16:39:18 -0700133 if (*stream) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700134 (*stream)->WritesDone();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700135 EXPECT_TRUE((*stream)->Finish().ok());
vjpai0a412932015-05-04 16:39:18 -0700136 }
vjpai46f65232015-03-23 10:10:27 -0700137 }
Vijay Paieed63fa2015-08-05 23:08:34 +0000138 delete[] stream_;
139 delete[] context_;
vjpai46f65232015-03-23 10:10:27 -0700140 }
Vijay Pai55bb5bd2015-03-23 12:44:09 -0700141
Craig Tiller8a5a6662015-04-09 11:31:28 -0700142 bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
Vijay Pai372fd872015-06-08 13:30:08 -0700143 WaitToIssue(thread_idx);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700144 GPR_TIMER_SCOPE("SynchronousStreamingClient::ThreadFunc", 0);
vjpaie2d39e02016-02-24 12:36:15 -0800145 double start = UsageTimer::Now();
vjpai0a412932015-05-04 16:39:18 -0700146 if (stream_[thread_idx]->Write(request_) &&
Craig Tiller5c8737d2015-05-21 11:42:17 -0700147 stream_[thread_idx]->Read(&responses_[thread_idx])) {
vjpaie2d39e02016-02-24 12:36:15 -0800148 histogram->Add((UsageTimer::Now() - start) * 1e9);
Craig Tiller8a5a6662015-04-09 11:31:28 -0700149 return true;
Craig Tilleraa2fca52015-04-09 10:56:06 -0700150 }
Craig Tiller8a5a6662015-04-09 11:31:28 -0700151 return false;
vjpai46f65232015-03-23 10:10:27 -0700152 }
Vijay Pai36ff3f02015-05-04 14:40:21 -0700153
154 private:
Vijay Paieed63fa2015-08-05 23:08:34 +0000155 // These are both conceptually std::vector but cannot be for old compilers
156 // that expect contained classes to support copy constructors
Vijay Pai90e73692015-08-05 19:15:36 -0700157 grpc::ClientContext* context_;
158 std::unique_ptr<grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>>*
159 stream_;
vjpai46f65232015-03-23 10:10:27 -0700160};
161
Vijay Pai36ff3f02015-05-04 14:40:21 -0700162std::unique_ptr<Client> CreateSynchronousUnaryClient(
163 const ClientConfig& config) {
vjpai46f65232015-03-23 10:10:27 -0700164 return std::unique_ptr<Client>(new SynchronousUnaryClient(config));
165}
Vijay Pai36ff3f02015-05-04 14:40:21 -0700166std::unique_ptr<Client> CreateSynchronousStreamingClient(
167 const ClientConfig& config) {
vjpai46f65232015-03-23 10:10:27 -0700168 return std::unique_ptr<Client>(new SynchronousStreamingClient(config));
vpai80b6d012014-12-17 11:47:32 -0800169}
170
Craig Tiller6af9ed02015-03-02 22:42:10 -0800171} // namespace testing
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800172} // namespace grpc