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