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" |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 38 | #include "test/cpp/qps/interarrival.h" |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 39 | #include "test/cpp/qps/timer.h" |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 40 | #include "test/cpp/qps/qpstest.grpc.pb.h" |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 41 | |
Craig Tiller | e38ec21 | 2015-03-04 11:19:15 -0800 | [diff] [blame] | 42 | #include <condition_variable> |
| 43 | #include <mutex> |
| 44 | |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 45 | namespace grpc { |
| 46 | namespace testing { |
| 47 | |
vjpai | 924d459 | 2015-06-02 10:26:52 -0700 | [diff] [blame^] | 48 | typedef std::chrono::system_clock grpc_time_source; |
| 49 | typedef std::chrono::time_point<grpc_time_source> grpc_time; |
| 50 | |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 51 | class Client { |
| 52 | public: |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 53 | explicit Client(const ClientConfig& config) : timer_(new Timer), |
| 54 | interarrival_timer_() { |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 55 | for (int i = 0; i < config.client_channels(); i++) { |
| 56 | channels_.push_back(ClientChannelInfo( |
| 57 | config.server_targets(i % config.server_targets_size()), config)); |
| 58 | } |
| 59 | request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE); |
| 60 | request_.set_response_size(config.payload_size()); |
| 61 | } |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 62 | virtual ~Client() {} |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 63 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 64 | ClientStats Mark() { |
| 65 | Histogram latencies; |
| 66 | std::vector<Histogram> to_merge(threads_.size()); |
| 67 | for (size_t i = 0; i < threads_.size(); i++) { |
| 68 | threads_[i]->BeginSwap(&to_merge[i]); |
| 69 | } |
| 70 | std::unique_ptr<Timer> timer(new Timer); |
| 71 | timer_.swap(timer); |
| 72 | for (size_t i = 0; i < threads_.size(); i++) { |
| 73 | threads_[i]->EndSwap(); |
| 74 | latencies.Merge(&to_merge[i]); |
| 75 | } |
| 76 | |
| 77 | auto timer_result = timer->Mark(); |
| 78 | |
| 79 | ClientStats stats; |
| 80 | latencies.FillProto(stats.mutable_latencies()); |
| 81 | stats.set_time_elapsed(timer_result.wall); |
| 82 | stats.set_time_system(timer_result.system); |
| 83 | stats.set_time_user(timer_result.user); |
| 84 | return stats; |
| 85 | } |
| 86 | |
| 87 | protected: |
| 88 | SimpleRequest request_; |
vjpai | 37f7257 | 2015-05-12 10:29:07 -0700 | [diff] [blame] | 89 | bool closed_loop_; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 90 | |
| 91 | class ClientChannelInfo { |
| 92 | public: |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 93 | ClientChannelInfo(const grpc::string& target, const ClientConfig& config) |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 94 | : channel_(CreateTestChannel(target, config.enable_ssl())), |
| 95 | stub_(TestService::NewStub(channel_)) {} |
| 96 | ChannelInterface* get_channel() { return channel_.get(); } |
| 97 | TestService::Stub* get_stub() { return stub_.get(); } |
| 98 | |
| 99 | private: |
| 100 | std::shared_ptr<ChannelInterface> channel_; |
| 101 | std::unique_ptr<TestService::Stub> stub_; |
| 102 | }; |
| 103 | std::vector<ClientChannelInfo> channels_; |
| 104 | |
| 105 | void StartThreads(size_t num_threads) { |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 106 | for (size_t i = 0; i < num_threads; i++) { |
| 107 | threads_.emplace_back(new Thread(this, i)); |
| 108 | } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 111 | void EndThreads() { threads_.clear(); } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 112 | |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 113 | virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0; |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 114 | |
| 115 | void SetupLoadTest(const ClientConfig& config, size_t num_threads) { |
| 116 | // Set up the load distribution based on the number of threads |
| 117 | if (config.load_type() == CLOSED_LOOP) { |
| 118 | closed_loop_ = true; |
| 119 | } |
| 120 | else { |
| 121 | closed_loop_ = false; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 122 | |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 123 | std::unique_ptr<RandomDist> random_dist; |
| 124 | auto& load = config.load_params(); |
| 125 | switch (config.load_type()) { |
| 126 | case POISSON: |
| 127 | random_dist.reset |
| 128 | (new ExpDist(load.poisson().offered_load()/num_threads)); |
| 129 | break; |
| 130 | case UNIFORM: |
| 131 | random_dist.reset |
| 132 | (new UniformDist(load.uniform().interarrival_lo()*num_threads, |
| 133 | load.uniform().interarrival_hi()*num_threads)); |
| 134 | break; |
| 135 | case DETERMINISTIC: |
| 136 | random_dist.reset |
| 137 | (new DetDist(num_threads/load.determ().offered_load())); |
| 138 | break; |
| 139 | case PARETO: |
| 140 | random_dist.reset |
| 141 | (new ParetoDist(load.pareto().interarrival_base()*num_threads, |
| 142 | load.pareto().alpha())); |
| 143 | break; |
| 144 | default: |
| 145 | GPR_ASSERT(false); |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | interarrival_timer_.init(*random_dist, num_threads); |
| 150 | for (size_t i = 0; i<num_threads; i++) { |
vjpai | 924d459 | 2015-06-02 10:26:52 -0700 | [diff] [blame^] | 151 | next_time_.push_back(grpc_time_source::now() + |
| 152 | std::chrono::duration_cast<grpc_time_source::duration>(interarrival_timer_(i))); |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
vjpai | 924d459 | 2015-06-02 10:26:52 -0700 | [diff] [blame^] | 156 | bool NextIssueTime(int thread_idx, grpc_time *time_delay) { |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 157 | if (closed_loop_) { |
| 158 | return false; |
| 159 | } |
| 160 | else { |
| 161 | *time_delay = next_time_[thread_idx]; |
vjpai | 924d459 | 2015-06-02 10:26:52 -0700 | [diff] [blame^] | 162 | next_time_[thread_idx] += std::chrono::duration_cast<grpc_time_source::duration>(interarrival_timer_(thread_idx)); |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 167 | private: |
| 168 | class Thread { |
| 169 | public: |
| 170 | Thread(Client* client, size_t idx) |
| 171 | : done_(false), |
| 172 | new_(nullptr), |
| 173 | impl_([this, idx, client]() { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 174 | for (;;) { |
| 175 | // run the loop body |
| 176 | bool thread_still_ok = client->ThreadFunc(&histogram_, idx); |
| 177 | // lock, see if we're done |
| 178 | std::lock_guard<std::mutex> g(mu_); |
| 179 | if (!thread_still_ok) { |
| 180 | gpr_log(GPR_ERROR, "Finishing client thread due to RPC error"); |
| 181 | done_ = true; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 182 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 183 | if (done_) { |
| 184 | return; |
| 185 | } |
| 186 | // check if we're marking, swap out the histogram if so |
| 187 | if (new_) { |
| 188 | new_->Swap(&histogram_); |
| 189 | new_ = nullptr; |
| 190 | cv_.notify_one(); |
| 191 | } |
| 192 | } |
| 193 | }) {} |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 194 | |
| 195 | ~Thread() { |
| 196 | { |
| 197 | std::lock_guard<std::mutex> g(mu_); |
| 198 | done_ = true; |
| 199 | } |
| 200 | impl_.join(); |
| 201 | } |
| 202 | |
| 203 | void BeginSwap(Histogram* n) { |
| 204 | std::lock_guard<std::mutex> g(mu_); |
| 205 | new_ = n; |
| 206 | } |
| 207 | |
| 208 | void EndSwap() { |
| 209 | std::unique_lock<std::mutex> g(mu_); |
| 210 | cv_.wait(g, [this]() { return new_ == nullptr; }); |
| 211 | } |
| 212 | |
| 213 | private: |
| 214 | Thread(const Thread&); |
| 215 | Thread& operator=(const Thread&); |
| 216 | |
| 217 | TestService::Stub* stub_; |
| 218 | ClientConfig config_; |
| 219 | std::mutex mu_; |
| 220 | std::condition_variable cv_; |
| 221 | bool done_; |
| 222 | Histogram* new_; |
| 223 | Histogram histogram_; |
| 224 | std::thread impl_; |
| 225 | }; |
| 226 | |
| 227 | std::vector<std::unique_ptr<Thread>> threads_; |
| 228 | std::unique_ptr<Timer> timer_; |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 229 | |
vjpai | c6aa60e | 2015-04-29 10:20:41 -0700 | [diff] [blame] | 230 | InterarrivalTimer interarrival_timer_; |
vjpai | 924d459 | 2015-06-02 10:26:52 -0700 | [diff] [blame^] | 231 | std::vector<grpc_time> next_time_; |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 232 | }; |
| 233 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 234 | std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args); |
| 235 | std::unique_ptr<Client> CreateSynchronousStreamingClient( |
| 236 | const ClientConfig& args); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 237 | std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args); |
| 238 | std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args); |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 239 | |
| 240 | } // namespace testing |
| 241 | } // namespace grpc |
| 242 | |
| 243 | #endif |