Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, 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 | #ifndef TEST_QPS_CLIENT_H |
| 35 | #define TEST_QPS_CLIENT_H |
| 36 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 37 | #include "test/cpp/qps/histogram.h" |
| 38 | #include "test/cpp/qps/timer.h" |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 39 | #include "test/cpp/qps/qpstest.pb.h" |
| 40 | |
Craig Tiller | e38ec21 | 2015-03-04 11:19:15 -0800 | [diff] [blame] | 41 | #include <condition_variable> |
| 42 | #include <mutex> |
| 43 | |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 44 | namespace grpc { |
| 45 | namespace testing { |
| 46 | |
| 47 | class Client { |
| 48 | public: |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 49 | explicit Client(const ClientConfig& config) : timer_(new Timer) { |
| 50 | for (int i = 0; i < config.client_channels(); i++) { |
| 51 | channels_.push_back(ClientChannelInfo( |
| 52 | config.server_targets(i % config.server_targets_size()), config)); |
| 53 | } |
| 54 | request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE); |
| 55 | request_.set_response_size(config.payload_size()); |
| 56 | } |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 57 | virtual ~Client() {} |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 58 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 59 | ClientStats Mark() { |
| 60 | Histogram latencies; |
| 61 | std::vector<Histogram> to_merge(threads_.size()); |
| 62 | for (size_t i = 0; i < threads_.size(); i++) { |
| 63 | threads_[i]->BeginSwap(&to_merge[i]); |
| 64 | } |
| 65 | std::unique_ptr<Timer> timer(new Timer); |
| 66 | timer_.swap(timer); |
| 67 | for (size_t i = 0; i < threads_.size(); i++) { |
| 68 | threads_[i]->EndSwap(); |
| 69 | latencies.Merge(&to_merge[i]); |
| 70 | } |
| 71 | |
| 72 | auto timer_result = timer->Mark(); |
| 73 | |
| 74 | ClientStats stats; |
| 75 | latencies.FillProto(stats.mutable_latencies()); |
| 76 | stats.set_time_elapsed(timer_result.wall); |
| 77 | stats.set_time_system(timer_result.system); |
| 78 | stats.set_time_user(timer_result.user); |
| 79 | return stats; |
| 80 | } |
| 81 | |
| 82 | protected: |
| 83 | SimpleRequest request_; |
| 84 | |
| 85 | class ClientChannelInfo { |
| 86 | public: |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 87 | ClientChannelInfo(const grpc::string& target, const ClientConfig& config) |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 88 | : channel_(CreateTestChannel(target, config.enable_ssl())), |
| 89 | stub_(TestService::NewStub(channel_)) {} |
| 90 | ChannelInterface* get_channel() { return channel_.get(); } |
| 91 | TestService::Stub* get_stub() { return stub_.get(); } |
| 92 | |
| 93 | private: |
| 94 | std::shared_ptr<ChannelInterface> channel_; |
| 95 | std::unique_ptr<TestService::Stub> stub_; |
| 96 | }; |
| 97 | std::vector<ClientChannelInfo> channels_; |
| 98 | |
| 99 | void StartThreads(size_t num_threads) { |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 100 | for (size_t i = 0; i < num_threads; i++) { |
| 101 | threads_.emplace_back(new Thread(this, i)); |
| 102 | } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 105 | void EndThreads() { threads_.clear(); } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 106 | |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame^] | 107 | virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | class Thread { |
| 111 | public: |
| 112 | Thread(Client* client, size_t idx) |
| 113 | : done_(false), |
| 114 | new_(nullptr), |
| 115 | impl_([this, idx, client]() { |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame^] | 116 | for (;;) { |
| 117 | // run the loop body |
| 118 | bool thread_still_ok = client->ThreadFunc(&histogram_, idx); |
| 119 | // lock, see if we're done |
| 120 | std::lock_guard<std::mutex> g(mu_); |
| 121 | if (!thread_still_ok) { |
| 122 | gpr_log(GPR_ERROR, "Finishing client thread due to RPC error"); |
| 123 | done_ = true; |
| 124 | } |
| 125 | if (done_) {return;} |
| 126 | // check if we're marking, swap out the histogram if so |
| 127 | if (new_) { |
| 128 | new_->Swap(&histogram_); |
| 129 | new_ = nullptr; |
| 130 | cv_.notify_one(); |
| 131 | } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 132 | } |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame^] | 133 | }) {} |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 134 | |
| 135 | ~Thread() { |
| 136 | { |
| 137 | std::lock_guard<std::mutex> g(mu_); |
| 138 | done_ = true; |
| 139 | } |
| 140 | impl_.join(); |
| 141 | } |
| 142 | |
| 143 | void BeginSwap(Histogram* n) { |
| 144 | std::lock_guard<std::mutex> g(mu_); |
| 145 | new_ = n; |
| 146 | } |
| 147 | |
| 148 | void EndSwap() { |
| 149 | std::unique_lock<std::mutex> g(mu_); |
| 150 | cv_.wait(g, [this]() { return new_ == nullptr; }); |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | Thread(const Thread&); |
| 155 | Thread& operator=(const Thread&); |
| 156 | |
| 157 | TestService::Stub* stub_; |
| 158 | ClientConfig config_; |
| 159 | std::mutex mu_; |
| 160 | std::condition_variable cv_; |
| 161 | bool done_; |
| 162 | Histogram* new_; |
| 163 | Histogram histogram_; |
| 164 | std::thread impl_; |
| 165 | }; |
| 166 | |
| 167 | std::vector<std::unique_ptr<Thread>> threads_; |
| 168 | std::unique_ptr<Timer> timer_; |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 171 | std::unique_ptr<Client> |
| 172 | CreateSynchronousUnaryClient(const ClientConfig& args); |
| 173 | std::unique_ptr<Client> |
| 174 | CreateSynchronousStreamingClient(const ClientConfig& args); |
| 175 | std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args); |
| 176 | std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args); |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 177 | |
| 178 | } // namespace testing |
| 179 | } // namespace grpc |
| 180 | |
| 181 | #endif |