vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, 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 | 105ff2e | 2015-04-26 01:04:23 -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> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 38 | #include <string> |
| 39 | #include <thread> |
| 40 | #include <vector> |
| 41 | #include <sstream> |
| 42 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 43 | #include <sys/signal.h> |
| 44 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 45 | #include <grpc/grpc.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 46 | #include <grpc/support/alloc.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 47 | #include <grpc/support/histogram.h> |
| 48 | #include <grpc/support/log.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 49 | #include <grpc/support/host_port.h> |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 50 | #include <gflags/gflags.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 51 | #include <grpc++/client_context.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 52 | #include <grpc++/server.h> |
| 53 | #include <grpc++/server_builder.h> |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 54 | #include <grpc++/status.h> |
| 55 | #include <grpc++/stream.h> |
| 56 | #include <gtest/gtest.h> |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 57 | #include "test/cpp/util/create_test_channel.h" |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 58 | #include "test/cpp/qps/client.h" |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 59 | #include "test/cpp/qps/qpstest.grpc.pb.h" |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 60 | #include "test/cpp/qps/histogram.h" |
Vijay Pai | 105ff2e | 2015-04-26 01:04:23 -0700 | [diff] [blame] | 61 | #include "test/cpp/qps/interarrival.h" |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 62 | #include "test/cpp/qps/timer.h" |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 63 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 64 | namespace grpc { |
| 65 | namespace testing { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 66 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 67 | class SynchronousClient : public Client { |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 68 | public: |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 69 | SynchronousClient(const ClientConfig& config) : Client(config) { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 70 | num_threads_ = |
| 71 | config.outstanding_rpcs_per_channel() * config.client_channels(); |
| 72 | responses_.resize(num_threads_); |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 73 | SetupLoadTest(config, num_threads_); |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 74 | } |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 75 | |
vjpai | 9ba29cd | 2015-04-29 10:40:26 -0700 | [diff] [blame] | 76 | virtual ~SynchronousClient() {}; |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 77 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 78 | protected: |
Vijay Pai | 105ff2e | 2015-04-26 01:04:23 -0700 | [diff] [blame] | 79 | void WaitToIssue(int thread_idx) { |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 80 | std::chrono::time_point<std::chrono::high_resolution_clock> next_time; |
| 81 | if (NextIssueTime(thread_idx, &next_time)) { |
| 82 | std::this_thread::sleep_until(next_time); |
Vijay Pai | 105ff2e | 2015-04-26 01:04:23 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 86 | size_t num_threads_; |
| 87 | std::vector<SimpleResponse> responses_; |
| 88 | }; |
| 89 | |
| 90 | class SynchronousUnaryClient GRPC_FINAL : public SynchronousClient { |
| 91 | public: |
| 92 | SynchronousUnaryClient(const ClientConfig& config): |
| 93 | SynchronousClient(config) {StartThreads(num_threads_);} |
vjpai | 9ba29cd | 2015-04-29 10:40:26 -0700 | [diff] [blame] | 94 | ~SynchronousUnaryClient() {EndThreads();} |
Vijay Pai | 0b94127 | 2015-04-29 13:58:36 -0700 | [diff] [blame^] | 95 | |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 96 | bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE { |
Vijay Pai | 105ff2e | 2015-04-26 01:04:23 -0700 | [diff] [blame] | 97 | WaitToIssue(thread_idx); |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 98 | auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 99 | double start = Timer::Now(); |
| 100 | grpc::ClientContext context; |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 101 | grpc::Status s = |
| 102 | stub->UnaryCall(&context, request_, &responses_[thread_idx]); |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 103 | histogram->Add((Timer::Now() - start) * 1e9); |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 104 | return s.IsOk(); |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 105 | } |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 108 | class SynchronousStreamingClient GRPC_FINAL : public SynchronousClient { |
| 109 | public: |
| 110 | SynchronousStreamingClient(const ClientConfig& config): |
| 111 | SynchronousClient(config) { |
| 112 | for (size_t thread_idx=0;thread_idx<num_threads_;thread_idx++){ |
| 113 | auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 114 | stream_ = stub->StreamingCall(&context_); |
| 115 | } |
| 116 | StartThreads(num_threads_); |
| 117 | } |
| 118 | ~SynchronousStreamingClient() { |
Craig Tiller | 77e0d64 | 2015-04-27 10:52:54 -0700 | [diff] [blame] | 119 | EndThreads(); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 120 | if (stream_) { |
| 121 | SimpleResponse response; |
| 122 | stream_->WritesDone(); |
Vijay Pai | 55bb5bd | 2015-03-23 12:44:09 -0700 | [diff] [blame] | 123 | EXPECT_TRUE(stream_->Finish().IsOk()); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
Vijay Pai | 55bb5bd | 2015-03-23 12:44:09 -0700 | [diff] [blame] | 126 | |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 127 | bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE { |
Vijay Pai | 105ff2e | 2015-04-26 01:04:23 -0700 | [diff] [blame] | 128 | WaitToIssue(thread_idx); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 129 | double start = Timer::Now(); |
Craig Tiller | aa2fca5 | 2015-04-09 10:56:06 -0700 | [diff] [blame] | 130 | if (stream_->Write(request_) && stream_->Read(&responses_[thread_idx])) { |
| 131 | histogram->Add((Timer::Now() - start) * 1e9); |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 132 | return true; |
Craig Tiller | aa2fca5 | 2015-04-09 10:56:06 -0700 | [diff] [blame] | 133 | } |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 134 | return false; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 135 | } |
| 136 | private: |
| 137 | grpc::ClientContext context_; |
Vijay Pai | 55bb5bd | 2015-03-23 12:44:09 -0700 | [diff] [blame] | 138 | std::unique_ptr<grpc::ClientReaderWriter<SimpleRequest, |
| 139 | SimpleResponse>> stream_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | std::unique_ptr<Client> |
| 143 | CreateSynchronousUnaryClient(const ClientConfig& config) { |
| 144 | return std::unique_ptr<Client>(new SynchronousUnaryClient(config)); |
| 145 | } |
| 146 | std::unique_ptr<Client> |
| 147 | CreateSynchronousStreamingClient(const ClientConfig& config) { |
| 148 | return std::unique_ptr<Client>(new SynchronousStreamingClient(config)); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 151 | } // namespace testing |
| 152 | } // namespace grpc |