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 | |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 37 | #include <condition_variable> |
| 38 | #include <mutex> |
| 39 | |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 40 | #include <grpc++/support/byte_buffer.h> |
| 41 | #include <grpc++/support/slice.h> |
| 42 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 43 | #include "test/cpp/qps/histogram.h" |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 44 | #include "test/cpp/qps/interarrival.h" |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 45 | #include "test/cpp/qps/timer.h" |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 46 | #include "test/cpp/util/create_test_channel.h" |
vjpai | 780a7f2 | 2015-11-04 00:19:09 -0800 | [diff] [blame] | 47 | #include "test/proto/benchmarks/payloads.grpc.pb.h" |
vjpai | d08a738 | 2015-11-02 16:45:08 -0800 | [diff] [blame] | 48 | #include "test/proto/benchmarks/services.grpc.pb.h" |
Craig Tiller | e38ec21 | 2015-03-04 11:19:15 -0800 | [diff] [blame] | 49 | |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 50 | namespace grpc { |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 51 | |
| 52 | #if defined(__APPLE__) |
| 53 | // Specialize Timepoint for high res clock as we need that |
| 54 | template <> |
| 55 | class TimePoint<std::chrono::high_resolution_clock::time_point> { |
| 56 | public: |
| 57 | TimePoint(const std::chrono::high_resolution_clock::time_point& time) { |
| 58 | TimepointHR2Timespec(time, &time_); |
| 59 | } |
| 60 | gpr_timespec raw_time() const { return time_; } |
| 61 | |
| 62 | private: |
| 63 | gpr_timespec time_; |
| 64 | }; |
| 65 | #endif |
| 66 | |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 67 | namespace testing { |
| 68 | |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 69 | typedef std::chrono::high_resolution_clock grpc_time_source; |
| 70 | typedef std::chrono::time_point<grpc_time_source> grpc_time; |
| 71 | |
vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 72 | template <class RequestType> |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 73 | class ClientRequestCreator { |
| 74 | public: |
| 75 | ClientRequestCreator(RequestType* req, const PayloadConfig&) { |
| 76 | // this template must be specialized |
| 77 | // fail with an assertion rather than a compile-time |
| 78 | // check since these only happen at the beginning anyway |
| 79 | GPR_ASSERT(false); |
| 80 | } |
| 81 | }; |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 82 | |
vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 83 | template <> |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 84 | class ClientRequestCreator<SimpleRequest> { |
| 85 | public: |
| 86 | ClientRequestCreator(SimpleRequest* req, |
| 87 | const PayloadConfig& payload_config) { |
| 88 | if (payload_config.has_bytebuf_params()) { |
vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 89 | GPR_ASSERT(false); // not appropriate for this specialization |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 90 | } else if (payload_config.has_simple_params()) { |
| 91 | req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE); |
| 92 | req->set_response_size(payload_config.simple_params().resp_size()); |
| 93 | req->mutable_payload()->set_type( |
| 94 | grpc::testing::PayloadType::COMPRESSABLE); |
| 95 | int size = payload_config.simple_params().req_size(); |
| 96 | std::unique_ptr<char[]> body(new char[size]); |
| 97 | req->mutable_payload()->set_body(body.get(), size); |
| 98 | } else if (payload_config.has_complex_params()) { |
| 99 | GPR_ASSERT(false); // not appropriate for this specialization |
| 100 | } else { |
| 101 | // default should be simple proto without payloads |
| 102 | req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE); |
| 103 | req->set_response_size(0); |
| 104 | req->mutable_payload()->set_type( |
| 105 | grpc::testing::PayloadType::COMPRESSABLE); |
vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 108 | }; |
| 109 | |
| 110 | template <> |
| 111 | class ClientRequestCreator<ByteBuffer> { |
| 112 | public: |
| 113 | ClientRequestCreator(ByteBuffer* req, const PayloadConfig& payload_config) { |
| 114 | if (payload_config.has_bytebuf_params()) { |
| 115 | if (payload_config.bytebuf_params().req_size() > 0) { |
| 116 | std::unique_ptr<char> buf( |
| 117 | new char[payload_config.bytebuf_params().req_size()]); |
| 118 | gpr_slice s = gpr_slice_from_copied_buffer( |
| 119 | buf.get(), payload_config.bytebuf_params().req_size()); |
| 120 | Slice slice(s, Slice::STEAL_REF); |
| 121 | std::unique_ptr<ByteBuffer> bbuf(new ByteBuffer(&slice, 1)); |
| 122 | req->MoveFrom(bbuf.get()); |
| 123 | } else { |
| 124 | GPR_ASSERT(false); // not appropriate for this specialization |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | }; |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 129 | |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 130 | class Client { |
| 131 | public: |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 132 | Client() : timer_(new Timer), interarrival_timer_() {} |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 133 | virtual ~Client() {} |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 134 | |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 135 | ClientStats Mark(bool reset) { |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 136 | Histogram latencies; |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 137 | Timer::Result timer_result; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 138 | |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 139 | // avoid std::vector for old compilers that expect a copy constructor |
| 140 | if (reset) { |
| 141 | Histogram* to_merge = new Histogram[threads_.size()]; |
| 142 | for (size_t i = 0; i < threads_.size(); i++) { |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 143 | threads_[i]->BeginSwap(&to_merge[i]); |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 144 | } |
| 145 | std::unique_ptr<Timer> timer(new Timer); |
| 146 | timer_.swap(timer); |
| 147 | for (size_t i = 0; i < threads_.size(); i++) { |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 148 | threads_[i]->EndSwap(); |
| 149 | latencies.Merge(to_merge[i]); |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 150 | } |
| 151 | delete[] to_merge; |
| 152 | timer_result = timer->Mark(); |
| 153 | } else { |
| 154 | // merge snapshots of each thread histogram |
| 155 | for (size_t i = 0; i < threads_.size(); i++) { |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 156 | threads_[i]->MergeStatsInto(&latencies); |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 157 | } |
| 158 | timer_result = timer_->Mark(); |
| 159 | } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 160 | |
| 161 | ClientStats stats; |
| 162 | latencies.FillProto(stats.mutable_latencies()); |
| 163 | stats.set_time_elapsed(timer_result.wall); |
| 164 | stats.set_time_system(timer_result.system); |
| 165 | stats.set_time_user(timer_result.user); |
| 166 | return stats; |
| 167 | } |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 168 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 169 | protected: |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 170 | bool closed_loop_; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 171 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 172 | void StartThreads(size_t num_threads) { |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 173 | for (size_t i = 0; i < num_threads; i++) { |
| 174 | threads_.emplace_back(new Thread(this, i)); |
| 175 | } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 178 | void EndThreads() { threads_.clear(); } |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 179 | |
Craig Tiller | 8a5a666 | 2015-04-09 11:31:28 -0700 | [diff] [blame] | 180 | virtual bool ThreadFunc(Histogram* histogram, size_t thread_idx) = 0; |
Vijay Pai | 8559485 | 2015-06-03 11:01:25 -0700 | [diff] [blame] | 181 | |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 182 | void SetupLoadTest(const ClientConfig& config, size_t num_threads) { |
| 183 | // Set up the load distribution based on the number of threads |
vjpai | 754751e | 2015-10-28 09:16:22 -0700 | [diff] [blame] | 184 | const auto& load = config.load_params(); |
| 185 | |
| 186 | std::unique_ptr<RandomDist> random_dist; |
vjpai | fba20c9 | 2015-11-04 14:49:17 -0800 | [diff] [blame] | 187 | switch (load.load_case()) { |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 188 | case LoadParams::kClosedLoop: |
| 189 | // Closed-loop doesn't use random dist at all |
| 190 | break; |
| 191 | case LoadParams::kPoisson: |
| 192 | random_dist.reset( |
| 193 | new ExpDist(load.poisson().offered_load() / num_threads)); |
| 194 | break; |
| 195 | case LoadParams::kUniform: |
| 196 | random_dist.reset( |
| 197 | new UniformDist(load.uniform().interarrival_lo() * num_threads, |
| 198 | load.uniform().interarrival_hi() * num_threads)); |
| 199 | break; |
| 200 | case LoadParams::kDeterm: |
| 201 | random_dist.reset( |
| 202 | new DetDist(num_threads / load.determ().offered_load())); |
| 203 | break; |
| 204 | case LoadParams::kPareto: |
| 205 | random_dist.reset( |
| 206 | new ParetoDist(load.pareto().interarrival_base() * num_threads, |
| 207 | load.pareto().alpha())); |
| 208 | break; |
| 209 | default: |
| 210 | GPR_ASSERT(false); |
vjpai | 754751e | 2015-10-28 09:16:22 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Set closed_loop_ based on whether or not random_dist is set |
| 214 | if (!random_dist) { |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 215 | closed_loop_ = true; |
| 216 | } else { |
| 217 | closed_loop_ = false; |
vjpai | 754751e | 2015-10-28 09:16:22 -0700 | [diff] [blame] | 218 | // set up interarrival timer according to random dist |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 219 | interarrival_timer_.init(*random_dist, num_threads); |
| 220 | for (size_t i = 0; i < num_threads; i++) { |
| 221 | next_time_.push_back( |
| 222 | grpc_time_source::now() + |
| 223 | std::chrono::duration_cast<grpc_time_source::duration>( |
| 224 | interarrival_timer_(i))); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | bool NextIssueTime(int thread_idx, grpc_time* time_delay) { |
| 230 | if (closed_loop_) { |
| 231 | return false; |
| 232 | } else { |
| 233 | *time_delay = next_time_[thread_idx]; |
| 234 | next_time_[thread_idx] += |
| 235 | std::chrono::duration_cast<grpc_time_source::duration>( |
| 236 | interarrival_timer_(thread_idx)); |
| 237 | return true; |
| 238 | } |
| 239 | } |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 240 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 241 | private: |
| 242 | class Thread { |
| 243 | public: |
| 244 | Thread(Client* client, size_t idx) |
Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 245 | : done_(false), |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 246 | new_stats_(nullptr), |
Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 247 | client_(client), |
| 248 | idx_(idx), |
| 249 | impl_(&Thread::ThreadFunc, this) {} |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 250 | |
| 251 | ~Thread() { |
| 252 | { |
| 253 | std::lock_guard<std::mutex> g(mu_); |
| 254 | done_ = true; |
| 255 | } |
| 256 | impl_.join(); |
| 257 | } |
| 258 | |
| 259 | void BeginSwap(Histogram* n) { |
| 260 | std::lock_guard<std::mutex> g(mu_); |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 261 | new_stats_ = n; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void EndSwap() { |
| 265 | std::unique_lock<std::mutex> g(mu_); |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 266 | while (new_stats_ != nullptr) { |
Vijay Pai | 784005b | 2015-07-31 10:53:42 -0700 | [diff] [blame] | 267 | cv_.wait(g); |
| 268 | }; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 269 | } |
| 270 | |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 271 | void MergeStatsInto(Histogram* hist) { |
| 272 | std::unique_lock<std::mutex> g(mu_); |
| 273 | hist->Merge(histogram_); |
| 274 | } |
| 275 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 276 | private: |
| 277 | Thread(const Thread&); |
| 278 | Thread& operator=(const Thread&); |
| 279 | |
vjpai | a9e0830 | 2015-07-31 07:55:06 -0700 | [diff] [blame] | 280 | void ThreadFunc() { |
| 281 | for (;;) { |
Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 282 | // run the loop body |
vjpai | b1db869 | 2015-08-11 22:41:02 -0700 | [diff] [blame] | 283 | const bool thread_still_ok = client_->ThreadFunc(&histogram_, idx_); |
Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 284 | // lock, see if we're done |
| 285 | std::lock_guard<std::mutex> g(mu_); |
| 286 | if (!thread_still_ok) { |
| 287 | gpr_log(GPR_ERROR, "Finishing client thread due to RPC error"); |
| 288 | done_ = true; |
| 289 | } |
| 290 | if (done_) { |
| 291 | return; |
| 292 | } |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 293 | // check if we're resetting stats, swap out the histogram if so |
| 294 | if (new_stats_) { |
| 295 | new_stats_->Swap(&histogram_); |
| 296 | new_stats_ = nullptr; |
Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 297 | cv_.notify_one(); |
| 298 | } |
vjpai | a9e0830 | 2015-07-31 07:55:06 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 302 | std::mutex mu_; |
| 303 | std::condition_variable cv_; |
| 304 | bool done_; |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 305 | Histogram* new_stats_; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 306 | Histogram histogram_; |
Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 307 | Client* client_; |
vjpai | a9e0830 | 2015-07-31 07:55:06 -0700 | [diff] [blame] | 308 | size_t idx_; |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 309 | std::thread impl_; |
| 310 | }; |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 311 | |
Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 312 | std::vector<std::unique_ptr<Thread>> threads_; |
| 313 | std::unique_ptr<Timer> timer_; |
Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 314 | |
| 315 | InterarrivalTimer interarrival_timer_; |
| 316 | std::vector<grpc_time> next_time_; |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 317 | }; |
| 318 | |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 319 | template <class StubType, class RequestType> |
| 320 | class ClientImpl : public Client { |
| 321 | public: |
| 322 | ClientImpl(const ClientConfig& config, |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 323 | std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> |
| 324 | create_stub) |
| 325 | : channels_(config.client_channels()), create_stub_(create_stub) { |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 326 | for (int i = 0; i < config.client_channels(); i++) { |
| 327 | channels_[i].init(config.server_targets(i % config.server_targets_size()), |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 328 | config, create_stub_); |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 331 | ClientRequestCreator<RequestType> create_req(&request_, |
| 332 | config.payload_config()); |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 333 | } |
| 334 | virtual ~ClientImpl() {} |
| 335 | |
| 336 | protected: |
| 337 | RequestType request_; |
| 338 | |
| 339 | class ClientChannelInfo { |
| 340 | public: |
| 341 | ClientChannelInfo() {} |
| 342 | ClientChannelInfo(const ClientChannelInfo& i) { |
| 343 | // The copy constructor is to satisfy old compilers |
| 344 | // that need it for using std::vector . It is only ever |
| 345 | // used for empty entries |
| 346 | GPR_ASSERT(!i.channel_ && !i.stub_); |
| 347 | } |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 348 | void init(const grpc::string& target, const ClientConfig& config, |
| 349 | std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> |
| 350 | create_stub) { |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 351 | // We have to use a 2-phase init like this with a default |
| 352 | // constructor followed by an initializer function to make |
| 353 | // old compilers happy with using this in std::vector |
| 354 | channel_ = CreateTestChannel( |
| 355 | target, config.security_params().server_host_override(), |
| 356 | config.has_security_params(), |
| 357 | !config.security_params().use_test_ca()); |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 358 | stub_ = create_stub(channel_); |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 359 | } |
| 360 | Channel* get_channel() { return channel_.get(); } |
| 361 | StubType* get_stub() { return stub_.get(); } |
| 362 | |
| 363 | private: |
| 364 | std::shared_ptr<Channel> channel_; |
| 365 | std::unique_ptr<StubType> stub_; |
| 366 | }; |
| 367 | std::vector<ClientChannelInfo> channels_; |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame^] | 368 | std::function<std::unique_ptr<StubType>(const std::shared_ptr<Channel>&)> |
| 369 | create_stub_; |
Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 370 | }; |
| 371 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 372 | std::unique_ptr<Client> CreateSynchronousUnaryClient(const ClientConfig& args); |
| 373 | std::unique_ptr<Client> CreateSynchronousStreamingClient( |
| 374 | const ClientConfig& args); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 375 | std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args); |
| 376 | std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args); |
Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 377 | |
| 378 | } // namespace testing |
| 379 | } // namespace grpc |
| 380 | |
| 381 | #endif |