OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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> |
| 35 #include <chrono> |
| 36 #include <memory> |
| 37 #include <mutex> |
| 38 #include <sstream> |
| 39 #include <string> |
| 40 #include <thread> |
| 41 #include <vector> |
| 42 |
| 43 #include <gflags/gflags.h> |
| 44 #include <grpc++/channel.h> |
| 45 #include <grpc++/client_context.h> |
| 46 #include <grpc++/server.h> |
| 47 #include <grpc++/server_builder.h> |
| 48 #include <grpc/grpc.h> |
| 49 #include <grpc/support/alloc.h> |
| 50 #include <grpc/support/histogram.h> |
| 51 #include <grpc/support/host_port.h> |
| 52 #include <grpc/support/log.h> |
| 53 #include <grpc/support/time.h> |
| 54 #include <gtest/gtest.h> |
| 55 |
| 56 #include "src/core/profiling/timers.h" |
| 57 #include "src/proto/grpc/testing/services.grpc.pb.h" |
| 58 #include "test/cpp/qps/client.h" |
| 59 #include "test/cpp/qps/histogram.h" |
| 60 #include "test/cpp/qps/interarrival.h" |
| 61 #include "test/cpp/qps/usage_timer.h" |
| 62 |
| 63 namespace grpc { |
| 64 namespace testing { |
| 65 |
| 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> { |
| 73 public: |
| 74 SynchronousClient(const ClientConfig& config) |
| 75 : ClientImpl<BenchmarkService::Stub, SimpleRequest>( |
| 76 config, BenchmarkStubCreator) { |
| 77 num_threads_ = |
| 78 config.outstanding_rpcs_per_channel() * config.client_channels(); |
| 79 responses_.resize(num_threads_); |
| 80 SetupLoadTest(config, num_threads_); |
| 81 } |
| 82 |
| 83 virtual ~SynchronousClient(){}; |
| 84 |
| 85 protected: |
| 86 void WaitToIssue(int thread_idx) { |
| 87 if (!closed_loop_) { |
| 88 gpr_sleep_until(NextIssueTime(thread_idx)); |
| 89 } |
| 90 } |
| 91 |
| 92 size_t num_threads_; |
| 93 std::vector<SimpleResponse> responses_; |
| 94 }; |
| 95 |
| 96 class SynchronousUnaryClient GRPC_FINAL : public SynchronousClient { |
| 97 public: |
| 98 SynchronousUnaryClient(const ClientConfig& config) |
| 99 : SynchronousClient(config) { |
| 100 StartThreads(num_threads_); |
| 101 } |
| 102 ~SynchronousUnaryClient() { EndThreads(); } |
| 103 |
| 104 bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE { |
| 105 WaitToIssue(thread_idx); |
| 106 auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 107 double start = UsageTimer::Now(); |
| 108 GPR_TIMER_SCOPE("SynchronousUnaryClient::ThreadFunc", 0); |
| 109 grpc::ClientContext context; |
| 110 grpc::Status s = |
| 111 stub->UnaryCall(&context, request_, &responses_[thread_idx]); |
| 112 histogram->Add((UsageTimer::Now() - start) * 1e9); |
| 113 return s.ok(); |
| 114 } |
| 115 }; |
| 116 |
| 117 class SynchronousStreamingClient GRPC_FINAL : public SynchronousClient { |
| 118 public: |
| 119 SynchronousStreamingClient(const ClientConfig& config) |
| 120 : SynchronousClient(config) { |
| 121 context_ = new grpc::ClientContext[num_threads_]; |
| 122 stream_ = new std::unique_ptr< |
| 123 grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>>[num_threads_]; |
| 124 for (size_t thread_idx = 0; thread_idx < num_threads_; thread_idx++) { |
| 125 auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 126 stream_[thread_idx] = stub->StreamingCall(&context_[thread_idx]); |
| 127 } |
| 128 StartThreads(num_threads_); |
| 129 } |
| 130 ~SynchronousStreamingClient() { |
| 131 EndThreads(); |
| 132 for (auto stream = &stream_[0]; stream != &stream_[num_threads_]; |
| 133 stream++) { |
| 134 if (*stream) { |
| 135 (*stream)->WritesDone(); |
| 136 EXPECT_TRUE((*stream)->Finish().ok()); |
| 137 } |
| 138 } |
| 139 delete[] stream_; |
| 140 delete[] context_; |
| 141 } |
| 142 |
| 143 bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE { |
| 144 WaitToIssue(thread_idx); |
| 145 GPR_TIMER_SCOPE("SynchronousStreamingClient::ThreadFunc", 0); |
| 146 double start = UsageTimer::Now(); |
| 147 if (stream_[thread_idx]->Write(request_) && |
| 148 stream_[thread_idx]->Read(&responses_[thread_idx])) { |
| 149 histogram->Add((UsageTimer::Now() - start) * 1e9); |
| 150 return true; |
| 151 } |
| 152 return false; |
| 153 } |
| 154 |
| 155 private: |
| 156 // These are both conceptually std::vector but cannot be for old compilers |
| 157 // that expect contained classes to support copy constructors |
| 158 grpc::ClientContext* context_; |
| 159 std::unique_ptr<grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>>* |
| 160 stream_; |
| 161 }; |
| 162 |
| 163 std::unique_ptr<Client> CreateSynchronousUnaryClient( |
| 164 const ClientConfig& config) { |
| 165 return std::unique_ptr<Client>(new SynchronousUnaryClient(config)); |
| 166 } |
| 167 std::unique_ptr<Client> CreateSynchronousStreamingClient( |
| 168 const ClientConfig& config) { |
| 169 return std::unique_ptr<Client>(new SynchronousStreamingClient(config)); |
| 170 } |
| 171 |
| 172 } // namespace testing |
| 173 } // namespace grpc |
OLD | NEW |