vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | eeb02ba | 2016-01-13 20:43:33 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 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 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 34 | #include <cassert> |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 35 | #include <chrono> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 36 | #include <memory> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 37 | #include <mutex> |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 38 | #include <sstream> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 39 | #include <string> |
| 40 | #include <thread> |
| 41 | #include <vector> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 42 | |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 43 | #include <gflags/gflags.h> |
Alistair Veitch | 75d5c0f | 2016-02-02 09:43:02 -0800 | [diff] [blame^] | 44 | #include <grpc++/channel.h> |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 45 | #include <grpc++/client_context.h> |
| 46 | #include <grpc++/server.h> |
| 47 | #include <grpc++/server_builder.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 48 | #include <grpc/grpc.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 49 | #include <grpc/support/alloc.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 50 | #include <grpc/support/histogram.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 51 | #include <grpc/support/host_port.h> |
Vijay Pai | bc78c20 | 2015-07-31 11:24:06 -0700 | [diff] [blame] | 52 | #include <grpc/support/log.h> |
| 53 | #include <grpc/support/time.h> |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 54 | #include <gtest/gtest.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 55 | |
Vijay Pai | 5bd6f2e | 2016-01-11 10:33:39 -0800 | [diff] [blame] | 56 | #include "src/core/profiling/timers.h" |
| 57 | #include "src/proto/grpc/testing/services.grpc.pb.h" |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 58 | #include "test/cpp/qps/client.h" |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 59 | #include "test/cpp/qps/histogram.h" |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 60 | #include "test/cpp/qps/interarrival.h" |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 61 | #include "test/cpp/qps/timer.h" |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 62 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 63 | namespace grpc { |
| 64 | namespace testing { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 65 | |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 66 | static std::unique_ptr<BenchmarkService::Stub> BenchmarkStubCreator( |
| 67 | std::shared_ptr<Channel> ch) { |
| 68 | return BenchmarkService::NewStub(ch); |
| 69 | } |
| 70 | |
| 71 | class SynchronousClient |
| 72 | : public ClientImpl<BenchmarkService::Stub, SimpleRequest> { |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 73 | public: |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 74 | SynchronousClient(const ClientConfig& config) |
| 75 | : ClientImpl<BenchmarkService::Stub, SimpleRequest>( |
| 76 | config, BenchmarkStubCreator) { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 77 | num_threads_ = |
Vijay Pai | 36ff3f0 | 2015-05-04 14:40:21 -0700 | [diff] [blame] | 78 | config.outstanding_rpcs_per_channel() * config.client_channels(); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 79 | responses_.resize(num_threads_); |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 80 | SetupLoadTest(config, num_threads_); |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 81 | } |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 82 | |
Vijay Pai | 36ff3f0 | 2015-05-04 14:40:21 -0700 | [diff] [blame] | 83 | virtual ~SynchronousClient(){}; |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 84 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 85 | protected: |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 86 | void WaitToIssue(int thread_idx) { |
| 87 | grpc_time next_time; |
| 88 | if (NextIssueTime(thread_idx, &next_time)) { |
Vijay Pai | bc78c20 | 2015-07-31 11:24:06 -0700 | [diff] [blame] | 89 | gpr_timespec next_timespec; |
| 90 | TimepointHR2Timespec(next_time, &next_timespec); |
| 91 | gpr_sleep_until(next_timespec); |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 95 | size_t num_threads_; |
| 96 | std::vector<SimpleResponse> responses_; |
| 97 | }; |
| 98 | |
| 99 | class SynchronousUnaryClient GRPC_FINAL : public SynchronousClient { |
| 100 | public: |
Vijay Pai | 36ff3f0 | 2015-05-04 14:40:21 -0700 | [diff] [blame] | 101 | SynchronousUnaryClient(const ClientConfig& config) |
| 102 | : SynchronousClient(config) { |
| 103 | StartThreads(num_threads_); |
| 104 | } |
| 105 | ~SynchronousUnaryClient() { EndThreads(); } |
Vijay Pai | 0b94127 | 2015-04-29 13:58:36 -0700 | [diff] [blame] | 106 | |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 107 | bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE { |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 108 | WaitToIssue(thread_idx); |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 109 | auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 110 | double start = Timer::Now(); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 111 | GPR_TIMER_SCOPE("SynchronousUnaryClient::ThreadFunc", 0); |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 112 | grpc::ClientContext context; |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 113 | grpc::Status s = |
| 114 | stub->UnaryCall(&context, request_, &responses_[thread_idx]); |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 115 | histogram->Add((Timer::Now() - start) * 1e9); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 116 | return s.ok(); |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 117 | } |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 120 | class SynchronousStreamingClient GRPC_FINAL : public SynchronousClient { |
| 121 | public: |
Vijay Pai | 36ff3f0 | 2015-05-04 14:40:21 -0700 | [diff] [blame] | 122 | SynchronousStreamingClient(const ClientConfig& config) |
Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 123 | : SynchronousClient(config) { |
Vijay Pai | eed63fa | 2015-08-05 23:08:34 +0000 | [diff] [blame] | 124 | context_ = new grpc::ClientContext[num_threads_]; |
| 125 | stream_ = new std::unique_ptr< |
Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 126 | grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>>[num_threads_]; |
Vijay Pai | 36ff3f0 | 2015-05-04 14:40:21 -0700 | [diff] [blame] | 127 | for (size_t thread_idx = 0; thread_idx < num_threads_; thread_idx++) { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 128 | auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
vjpai | 0a41293 | 2015-05-04 16:39:18 -0700 | [diff] [blame] | 129 | stream_[thread_idx] = stub->StreamingCall(&context_[thread_idx]); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 130 | } |
| 131 | StartThreads(num_threads_); |
| 132 | } |
| 133 | ~SynchronousStreamingClient() { |
Craig Tiller | 77e0d64 | 2015-04-27 10:52:54 -0700 | [diff] [blame] | 134 | EndThreads(); |
Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 135 | for (auto stream = &stream_[0]; stream != &stream_[num_threads_]; |
| 136 | stream++) { |
vjpai | 0a41293 | 2015-05-04 16:39:18 -0700 | [diff] [blame] | 137 | if (*stream) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 138 | (*stream)->WritesDone(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 139 | EXPECT_TRUE((*stream)->Finish().ok()); |
vjpai | 0a41293 | 2015-05-04 16:39:18 -0700 | [diff] [blame] | 140 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 141 | } |
Vijay Pai | eed63fa | 2015-08-05 23:08:34 +0000 | [diff] [blame] | 142 | delete[] stream_; |
| 143 | delete[] context_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 144 | } |
Vijay Pai | 55bb5bd | 2015-03-23 12:44:09 -0700 | [diff] [blame] | 145 | |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 146 | bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE { |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 147 | WaitToIssue(thread_idx); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 148 | GPR_TIMER_SCOPE("SynchronousStreamingClient::ThreadFunc", 0); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 149 | double start = Timer::Now(); |
vjpai | 0a41293 | 2015-05-04 16:39:18 -0700 | [diff] [blame] | 150 | if (stream_[thread_idx]->Write(request_) && |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 151 | stream_[thread_idx]->Read(&responses_[thread_idx])) { |
Craig Tiller | aa2fca5 | 2015-04-09 10:56:06 -0700 | [diff] [blame] | 152 | histogram->Add((Timer::Now() - start) * 1e9); |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 153 | return true; |
Craig Tiller | aa2fca5 | 2015-04-09 10:56:06 -0700 | [diff] [blame] | 154 | } |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 155 | return false; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 156 | } |
Vijay Pai | 36ff3f0 | 2015-05-04 14:40:21 -0700 | [diff] [blame] | 157 | |
| 158 | private: |
Vijay Pai | eed63fa | 2015-08-05 23:08:34 +0000 | [diff] [blame] | 159 | // These are both conceptually std::vector but cannot be for old compilers |
| 160 | // that expect contained classes to support copy constructors |
Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 161 | grpc::ClientContext* context_; |
| 162 | std::unique_ptr<grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>>* |
| 163 | stream_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Vijay Pai | 36ff3f0 | 2015-05-04 14:40:21 -0700 | [diff] [blame] | 166 | std::unique_ptr<Client> CreateSynchronousUnaryClient( |
| 167 | const ClientConfig& config) { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 168 | return std::unique_ptr<Client>(new SynchronousUnaryClient(config)); |
| 169 | } |
Vijay Pai | 36ff3f0 | 2015-05-04 14:40:21 -0700 | [diff] [blame] | 170 | std::unique_ptr<Client> CreateSynchronousStreamingClient( |
| 171 | const ClientConfig& config) { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 172 | return std::unique_ptr<Client>(new SynchronousStreamingClient(config)); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 175 | } // namespace testing |
David Garcia Quintas | 2bf574f | 2016-01-14 15:27:08 -0800 | [diff] [blame] | 176 | } // namespace grpc |