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" |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -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> |
vjpai | a9e0830 | 2015-07-31 07:55:06 -0700 | [diff] [blame] | 44 | #include <grpc++/config.h> |
| 45 | #include <grpc++/config.h> |
Craig Tiller | e38ec21 | 2015-03-04 11:19:15 -0800 | [diff] [blame] | 46 | |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 47 | namespace grpc { |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 48 | |
| 49 | #if defined(__APPLE__) |
| 50 | // Specialize Timepoint for high res clock as we need that |
| 51 | template <> |
| 52 | class TimePoint<std::chrono::high_resolution_clock::time_point> { |
| 53 | public: |
| 54 | TimePoint(const std::chrono::high_resolution_clock::time_point& time) { |
| 55 | TimepointHR2Timespec(time, &time_); |
| 56 | } |
| 57 | gpr_timespec raw_time() const { return time_; } |
| 58 | |
| 59 | private: |
| 60 | gpr_timespec time_; |
| 61 | }; |
| 62 | #endif |
| 63 | |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 64 | namespace testing { |
| 65 | |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 66 | typedef std::chrono::high_resolution_clock grpc_time_source; |
| 67 | typedef std::chrono::time_point<grpc_time_source> grpc_time; |
| 68 | |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 69 | class Client { |
| 70 | public: |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 71 | explicit Client(const ClientConfig& config) |
Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 72 | : channels_(config.client_channels()), |
| 73 | timer_(new Timer), |
| 74 | interarrival_timer_() { |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 75 | for (int i = 0; i < config.client_channels(); i++) { |
Vijay Pai | eed63fa | 2015-08-05 23:08:34 +0000 | [diff] [blame] | 76 | channels_[i].init(config.server_targets(i % config.server_targets_size()), |
Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 77 | config); |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 78 | } |
| 79 | request_.set_response_type(grpc::testing::PayloadType::COMPRESSABLE); |
| 80 | request_.set_response_size(config.payload_size()); |
| 81 | } |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 82 | virtual ~Client() {} |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 83 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 84 | ClientStats Mark() { |
| 85 | Histogram latencies; |
Vijay Pai | ad3e00c | 2015-08-07 17:21:08 +0000 | [diff] [blame^] | 86 | // avoid std::vector for old compilers |
| 87 | Histogram *to_merge = new Histogram[threads_.size()]; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 88 | for (size_t i = 0; i < threads_.size(); i++) { |
| 89 | threads_[i]->BeginSwap(&to_merge[i]); |
| 90 | } |
| 91 | std::unique_ptr<Timer> timer(new Timer); |
| 92 | timer_.swap(timer); |
| 93 | for (size_t i = 0; i < threads_.size(); i++) { |
| 94 | threads_[i]->EndSwap(); |
| 95 | latencies.Merge(&to_merge[i]); |
| 96 | } |
Vijay Pai | ad3e00c | 2015-08-07 17:21:08 +0000 | [diff] [blame^] | 97 | delete[] to_merge; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 98 | |
| 99 | auto timer_result = timer->Mark(); |
| 100 | |
| 101 | ClientStats stats; |
| 102 | latencies.FillProto(stats.mutable_latencies()); |
| 103 | stats.set_time_elapsed(timer_result.wall); |
| 104 | stats.set_time_system(timer_result.system); |
| 105 | stats.set_time_user(timer_result.user); |
| 106 | return stats; |
| 107 | } |
| 108 | |
| 109 | protected: |
| 110 | SimpleRequest request_; |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 111 | bool closed_loop_; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 112 | |
| 113 | class ClientChannelInfo { |
| 114 | public: |
Vijay Pai | eed63fa | 2015-08-05 23:08:34 +0000 | [diff] [blame] | 115 | ClientChannelInfo() {} |
Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 116 | ClientChannelInfo(const ClientChannelInfo& i) : channel_(), stub_() { |
| 117 | // The copy constructor is to satisfy old compilers |
| 118 | // that need it for using std::vector . It is only ever |
| 119 | // used for empty entries |
Vijay Pai | eed63fa | 2015-08-05 23:08:34 +0000 | [diff] [blame] | 120 | GPR_ASSERT(!i.channel_ && !i.stub_); |
| 121 | } |
| 122 | void init(const grpc::string& target, const ClientConfig& config) { |
Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 123 | // We have to use a 2-phase init like this with a default |
| 124 | // constructor followed by an initializer function to make |
| 125 | // old compilers happy with using this in std::vector |
Vijay Pai | eed63fa | 2015-08-05 23:08:34 +0000 | [diff] [blame] | 126 | channel_ = CreateTestChannel(target, config.enable_ssl()); |
| 127 | stub_ = TestService::NewStub(channel_); |
| 128 | } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 129 | ChannelInterface* get_channel() { return channel_.get(); } |
| 130 | TestService::Stub* get_stub() { return stub_.get(); } |
Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 131 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 132 | private: |
| 133 | std::shared_ptr<ChannelInterface> channel_; |
| 134 | std::unique_ptr<TestService::Stub> stub_; |
| 135 | }; |
| 136 | std::vector<ClientChannelInfo> channels_; |
| 137 | |
| 138 | void StartThreads(size_t num_threads) { |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 139 | for (size_t i = 0; i < num_threads; i++) { |
| 140 | threads_.emplace_back(new Thread(this, i)); |
| 141 | } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 144 | void EndThreads() { threads_.clear(); } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 145 | |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 146 | virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0; |
Vijay Pai | 8559485 | 2015-06-03 11:01:25 -0700 | [diff] [blame] | 147 | |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 148 | void SetupLoadTest(const ClientConfig& config, size_t num_threads) { |
| 149 | // Set up the load distribution based on the number of threads |
| 150 | if (config.load_type() == CLOSED_LOOP) { |
| 151 | closed_loop_ = true; |
| 152 | } else { |
| 153 | closed_loop_ = false; |
| 154 | |
| 155 | std::unique_ptr<RandomDist> random_dist; |
| 156 | const auto& load = config.load_params(); |
| 157 | switch (config.load_type()) { |
| 158 | case POISSON: |
| 159 | random_dist.reset( |
| 160 | new ExpDist(load.poisson().offered_load() / num_threads)); |
| 161 | break; |
| 162 | case UNIFORM: |
| 163 | random_dist.reset( |
| 164 | new UniformDist(load.uniform().interarrival_lo() * num_threads, |
| 165 | load.uniform().interarrival_hi() * num_threads)); |
| 166 | break; |
| 167 | case DETERMINISTIC: |
| 168 | random_dist.reset( |
| 169 | new DetDist(num_threads / load.determ().offered_load())); |
| 170 | break; |
| 171 | case PARETO: |
| 172 | random_dist.reset( |
| 173 | new ParetoDist(load.pareto().interarrival_base() * num_threads, |
| 174 | load.pareto().alpha())); |
| 175 | break; |
| 176 | default: |
| 177 | GPR_ASSERT(false); |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | interarrival_timer_.init(*random_dist, num_threads); |
| 182 | for (size_t i = 0; i < num_threads; i++) { |
| 183 | next_time_.push_back( |
| 184 | grpc_time_source::now() + |
| 185 | std::chrono::duration_cast<grpc_time_source::duration>( |
| 186 | interarrival_timer_(i))); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | bool NextIssueTime(int thread_idx, grpc_time* time_delay) { |
| 192 | if (closed_loop_) { |
| 193 | return false; |
| 194 | } else { |
| 195 | *time_delay = next_time_[thread_idx]; |
| 196 | next_time_[thread_idx] += |
| 197 | std::chrono::duration_cast<grpc_time_source::duration>( |
| 198 | interarrival_timer_(thread_idx)); |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 203 | private: |
| 204 | class Thread { |
| 205 | public: |
| 206 | Thread(Client* client, size_t idx) |
Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 207 | : done_(false), |
| 208 | new_(nullptr), |
| 209 | client_(client), |
| 210 | idx_(idx), |
| 211 | impl_(&Thread::ThreadFunc, this) {} |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 212 | |
| 213 | ~Thread() { |
| 214 | { |
| 215 | std::lock_guard<std::mutex> g(mu_); |
| 216 | done_ = true; |
| 217 | } |
| 218 | impl_.join(); |
| 219 | } |
| 220 | |
| 221 | void BeginSwap(Histogram* n) { |
| 222 | std::lock_guard<std::mutex> g(mu_); |
| 223 | new_ = n; |
| 224 | } |
| 225 | |
| 226 | void EndSwap() { |
| 227 | std::unique_lock<std::mutex> g(mu_); |
Vijay Pai | 784005b | 2015-07-31 10:53:42 -0700 | [diff] [blame] | 228 | while (new_ != nullptr) { |
| 229 | cv_.wait(g); |
| 230 | }; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | private: |
| 234 | Thread(const Thread&); |
| 235 | Thread& operator=(const Thread&); |
| 236 | |
vjpai | a9e0830 | 2015-07-31 07:55:06 -0700 | [diff] [blame] | 237 | void ThreadFunc() { |
| 238 | for (;;) { |
Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 239 | // run the loop body |
| 240 | bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_); |
| 241 | // lock, see if we're done |
| 242 | std::lock_guard<std::mutex> g(mu_); |
| 243 | if (!thread_still_ok) { |
| 244 | gpr_log(GPR_ERROR, "Finishing client thread due to RPC error"); |
| 245 | done_ = true; |
| 246 | } |
| 247 | if (done_) { |
| 248 | return; |
| 249 | } |
| 250 | // check if we're marking, swap out the histogram if so |
| 251 | if (new_) { |
| 252 | new_->Swap(&histogram_); |
| 253 | new_ = nullptr; |
| 254 | cv_.notify_one(); |
| 255 | } |
vjpai | a9e0830 | 2015-07-31 07:55:06 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 259 | TestService::Stub* stub_; |
| 260 | ClientConfig config_; |
| 261 | std::mutex mu_; |
| 262 | std::condition_variable cv_; |
| 263 | bool done_; |
| 264 | Histogram* new_; |
| 265 | Histogram histogram_; |
Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 266 | Client* client_; |
vjpai | a9e0830 | 2015-07-31 07:55:06 -0700 | [diff] [blame] | 267 | size_t idx_; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 268 | std::thread impl_; |
| 269 | }; |
| 270 | |
| 271 | std::vector<std::unique_ptr<Thread>> threads_; |
| 272 | std::unique_ptr<Timer> timer_; |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 273 | |
| 274 | InterarrivalTimer interarrival_timer_; |
| 275 | std::vector<grpc_time> next_time_; |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 276 | }; |
| 277 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 278 | std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args); |
| 279 | std::unique_ptr<Client> CreateSynchronousStreamingClient( |
| 280 | const ClientConfig& args); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 281 | std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args); |
| 282 | std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args); |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 283 | |
| 284 | } // namespace testing |
| 285 | } // namespace grpc |
| 286 | |
| 287 | #endif |