blob: d1682caf066742a0077f90d606ed4792b9dd44ce [file] [log] [blame]
vpai80b6d012014-12-17 11:47:32 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * 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>
vpai80b6d012014-12-17 11:47:32 -080038#include <string>
39#include <thread>
40#include <vector>
41#include <sstream>
42
Craig Tiller5c004c62015-02-23 16:31:57 -080043#include <sys/signal.h>
44
vpai80b6d012014-12-17 11:47:32 -080045#include <grpc/grpc.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080046#include <grpc/support/alloc.h>
vpai80b6d012014-12-17 11:47:32 -080047#include <grpc/support/histogram.h>
48#include <grpc/support/log.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080049#include <grpc/support/host_port.h>
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010050#include <gflags/gflags.h>
vpai80b6d012014-12-17 11:47:32 -080051#include <grpc++/client_context.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080052#include <grpc++/server.h>
53#include <grpc++/server_builder.h>
vjpai46f65232015-03-23 10:10:27 -070054#include <grpc++/status.h>
55#include <grpc++/stream.h>
56#include <gtest/gtest.h>
yangg59dfc902014-12-19 14:00:14 -080057#include "test/cpp/util/create_test_channel.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080058#include "test/cpp/qps/client.h"
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020059#include "test/cpp/qps/qpstest.grpc.pb.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080060#include "test/cpp/qps/histogram.h"
Vijay Pai372fd872015-06-08 13:30:08 -070061#include "test/cpp/qps/interarrival.h"
Craig Tiller2d0f36c2015-02-23 23:16:17 -080062#include "test/cpp/qps/timer.h"
vpai80b6d012014-12-17 11:47:32 -080063
Craig Tiller6af9ed02015-03-02 22:42:10 -080064namespace grpc {
65namespace testing {
vpai80b6d012014-12-17 11:47:32 -080066
vjpai46f65232015-03-23 10:10:27 -070067class SynchronousClient : public Client {
Craig Tiller6af9ed02015-03-02 22:42:10 -080068 public:
vjpaic6aa60e2015-04-29 10:20:41 -070069 SynchronousClient(const ClientConfig& config) : Client(config) {
vjpai46f65232015-03-23 10:10:27 -070070 num_threads_ =
Vijay Pai36ff3f02015-05-04 14:40:21 -070071 config.outstanding_rpcs_per_channel() * config.client_channels();
vjpai46f65232015-03-23 10:10:27 -070072 responses_.resize(num_threads_);
Vijay Pai372fd872015-06-08 13:30:08 -070073 SetupLoadTest(config, num_threads_);
Craig Tiller6af9ed02015-03-02 22:42:10 -080074 }
vpai80b6d012014-12-17 11:47:32 -080075
Vijay Pai36ff3f02015-05-04 14:40:21 -070076 virtual ~SynchronousClient(){};
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010077
vjpai46f65232015-03-23 10:10:27 -070078 protected:
Vijay Pai372fd872015-06-08 13:30:08 -070079 void WaitToIssue(int thread_idx) {
80 grpc_time next_time;
81 if (NextIssueTime(thread_idx, &next_time)) {
82 std::this_thread::sleep_until(next_time);
83 }
84 }
85
vjpai46f65232015-03-23 10:10:27 -070086 size_t num_threads_;
87 std::vector<SimpleResponse> responses_;
88};
89
90class SynchronousUnaryClient GRPC_FINAL : public SynchronousClient {
91 public:
Vijay Pai36ff3f02015-05-04 14:40:21 -070092 SynchronousUnaryClient(const ClientConfig& config)
93 : SynchronousClient(config) {
94 StartThreads(num_threads_);
95 }
96 ~SynchronousUnaryClient() { EndThreads(); }
Vijay Pai0b941272015-04-29 13:58:36 -070097
Craig Tiller8a5a6662015-04-09 11:31:28 -070098 bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
Vijay Pai372fd872015-06-08 13:30:08 -070099 WaitToIssue(thread_idx);
Craig Tiller88568752015-03-04 10:50:43 -0800100 auto* stub = channels_[thread_idx % channels_.size()].get_stub();
101 double start = Timer::Now();
102 grpc::ClientContext context;
Craig Tillera182bf12015-03-04 13:54:39 -0800103 grpc::Status s =
104 stub->UnaryCall(&context, request_, &responses_[thread_idx]);
Craig Tiller88568752015-03-04 10:50:43 -0800105 histogram->Add((Timer::Now() - start) * 1e9);
Craig Tiller8a5a6662015-04-09 11:31:28 -0700106 return s.IsOk();
Craig Tiller6af9ed02015-03-02 22:42:10 -0800107 }
Craig Tiller5c004c62015-02-23 16:31:57 -0800108};
109
vjpai46f65232015-03-23 10:10:27 -0700110class SynchronousStreamingClient GRPC_FINAL : public SynchronousClient {
111 public:
Vijay Pai36ff3f02015-05-04 14:40:21 -0700112 SynchronousStreamingClient(const ClientConfig& config)
Craig Tiller5c8737d2015-05-21 11:42:17 -0700113 : SynchronousClient(config),
114 context_(num_threads_),
115 stream_(num_threads_) {
Vijay Pai36ff3f02015-05-04 14:40:21 -0700116 for (size_t thread_idx = 0; thread_idx < num_threads_; thread_idx++) {
vjpai46f65232015-03-23 10:10:27 -0700117 auto* stub = channels_[thread_idx % channels_.size()].get_stub();
vjpai0a412932015-05-04 16:39:18 -0700118 stream_[thread_idx] = stub->StreamingCall(&context_[thread_idx]);
vjpai46f65232015-03-23 10:10:27 -0700119 }
120 StartThreads(num_threads_);
121 }
122 ~SynchronousStreamingClient() {
Craig Tiller77e0d642015-04-27 10:52:54 -0700123 EndThreads();
vjpai0a412932015-05-04 16:39:18 -0700124 for (auto stream = stream_.begin(); stream != stream_.end(); stream++) {
125 if (*stream) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700126 (*stream)->WritesDone();
127 EXPECT_TRUE((*stream)->Finish().IsOk());
vjpai0a412932015-05-04 16:39:18 -0700128 }
vjpai46f65232015-03-23 10:10:27 -0700129 }
130 }
Vijay Pai55bb5bd2015-03-23 12:44:09 -0700131
Craig Tiller8a5a6662015-04-09 11:31:28 -0700132 bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
Vijay Pai372fd872015-06-08 13:30:08 -0700133 WaitToIssue(thread_idx);
vjpai46f65232015-03-23 10:10:27 -0700134 double start = Timer::Now();
vjpai0a412932015-05-04 16:39:18 -0700135 if (stream_[thread_idx]->Write(request_) &&
Craig Tiller5c8737d2015-05-21 11:42:17 -0700136 stream_[thread_idx]->Read(&responses_[thread_idx])) {
Craig Tilleraa2fca52015-04-09 10:56:06 -0700137 histogram->Add((Timer::Now() - start) * 1e9);
Craig Tiller8a5a6662015-04-09 11:31:28 -0700138 return true;
Craig Tilleraa2fca52015-04-09 10:56:06 -0700139 }
Craig Tiller8a5a6662015-04-09 11:31:28 -0700140 return false;
vjpai46f65232015-03-23 10:10:27 -0700141 }
Vijay Pai36ff3f02015-05-04 14:40:21 -0700142
143 private:
144 std::vector<grpc::ClientContext> context_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700145 std::vector<std::unique_ptr<
146 grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>>> stream_;
vjpai46f65232015-03-23 10:10:27 -0700147};
148
Vijay Pai36ff3f02015-05-04 14:40:21 -0700149std::unique_ptr<Client> CreateSynchronousUnaryClient(
150 const ClientConfig& config) {
vjpai46f65232015-03-23 10:10:27 -0700151 return std::unique_ptr<Client>(new SynchronousUnaryClient(config));
152}
Vijay Pai36ff3f02015-05-04 14:40:21 -0700153std::unique_ptr<Client> CreateSynchronousStreamingClient(
154 const ClientConfig& config) {
vjpai46f65232015-03-23 10:10:27 -0700155 return std::unique_ptr<Client>(new SynchronousStreamingClient(config));
vpai80b6d012014-12-17 11:47:32 -0800156}
157
Craig Tiller6af9ed02015-03-02 22:42:10 -0800158} // namespace testing
159} // namespace grpc