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