| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef TEST_QPS_CLIENT_H |
| 20 |
#define TEST_QPS_CLIENT_H |
| 21 |
|
| 22 |
#include <inttypes.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <stdlib.h> |
| 25 |
|
| 26 |
#include <condition_variable> |
| 27 |
#include <mutex> |
| 28 |
#include <thread> |
| 29 |
#include <unordered_map> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
#include "absl/memory/memory.h" |
| 33 |
#include "absl/strings/match.h" |
| 34 |
|
| 35 |
#include <grpc/support/log.h> |
| 36 |
#include <grpc/support/time.h> |
| 37 |
#include <grpcpp/channel.h> |
| 38 |
#include <grpcpp/support/byte_buffer.h> |
| 39 |
#include <grpcpp/support/channel_arguments.h> |
| 40 |
#include <grpcpp/support/slice.h> |
| 41 |
|
| 42 |
#include "src/core/lib/gprpp/env.h" |
| 43 |
#include "src/proto/grpc/testing/benchmark_service.grpc.pb.h" |
| 44 |
#include "src/proto/grpc/testing/payloads.pb.h" |
| 45 |
#include "test/cpp/qps/histogram.h" |
| 46 |
#include "test/cpp/qps/interarrival.h" |
| 47 |
#include "test/cpp/qps/qps_worker.h" |
| 48 |
#include "test/cpp/qps/server.h" |
| 49 |
#include "test/cpp/qps/usage_timer.h" |
| 50 |
#include "test/cpp/util/create_test_channel.h" |
| 51 |
#include "test/cpp/util/test_credentials_provider.h" |
| 52 |
|
| 53 |
#define INPROC_NAME_PREFIX "qpsinproc:" |
| 54 |
|
| 55 |
namespace grpc { |
| 56 |
namespace testing { |
| 57 |
|
| 58 |
template <class RequestType> |
| 59 |
class ClientRequestCreator { |
| 60 |
public: |
| 61 |
ClientRequestCreator(RequestType* /*req*/, const PayloadConfig&) { |
| 62 |
// this template must be specialized |
| 63 |
// fail with an assertion rather than a compile-time |
| 64 |
// check since these only happen at the beginning anyway |
| 65 |
GPR_ASSERT(false); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
template <> |
| 70 |
class ClientRequestCreator<SimpleRequest> { |
| 71 |
public: |
| 72 |
ClientRequestCreator(SimpleRequest* req, |
| 73 |
const PayloadConfig& payload_config) { |
| 74 |
if (payload_config.has_bytebuf_params()) { |
| 75 |
gpr_log(GPR_ERROR, |
| 76 |
"Invalid PayloadConfig, config cannot have bytebuf_params: %s", |
| 77 |
payload_config.DebugString().c_str()); |
| 78 |
GPR_ASSERT(false); // not appropriate for this specialization |
| 79 |
} else if (payload_config.has_simple_params()) { |
| 80 |
req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE); |
| 81 |
req->set_response_size(payload_config.simple_params().resp_size()); |
| 82 |
req->mutable_payload()->set_type( |
| 83 |
grpc::testing::PayloadType::COMPRESSABLE); |
| 84 |
int size = payload_config.simple_params().req_size(); |
| 85 |
std::unique_ptr<char[]> body(new char[size]); |
| 86 |
req->mutable_payload()->set_body(body.get(), size); |
| 87 |
} else if (payload_config.has_complex_params()) { |
| 88 |
gpr_log(GPR_ERROR, |
| 89 |
"Invalid PayloadConfig, cannot have complex_params: %s", |
| 90 |
payload_config.DebugString().c_str()); |
| 91 |
GPR_ASSERT(false); // not appropriate for this specialization |
| 92 |
} else { |
| 93 |
// default should be simple proto without payloads |
| 94 |
req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE); |
| 95 |
req->set_response_size(0); |
| 96 |
req->mutable_payload()->set_type( |
| 97 |
grpc::testing::PayloadType::COMPRESSABLE); |
| 98 |
} |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
template <> |
| 103 |
class ClientRequestCreator<ByteBuffer> { |
| 104 |
public: |
| 105 |
ClientRequestCreator(ByteBuffer* req, const PayloadConfig& payload_config) { |
| 106 |
if (payload_config.has_bytebuf_params()) { |
| 107 |
size_t req_sz = |
| 108 |
static_cast<size_t>(payload_config.bytebuf_params().req_size()); |
| 109 |
std::unique_ptr<char[]> buf(new char[req_sz]); |
| 110 |
memset(buf.get(), 0, req_sz); |
| 111 |
Slice slice(buf.get(), req_sz); |
| 112 |
*req = ByteBuffer(&slice, 1); |
| 113 |
} else { |
| 114 |
gpr_log(GPR_ERROR, "Invalid PayloadConfig, missing bytebug_params: %s", |
| 115 |
payload_config.DebugString().c_str()); |
| 116 |
GPR_ASSERT(false); // not appropriate for this specialization |
| 117 |
} |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
class HistogramEntry final { |
| 122 |
public: |
| 123 |
HistogramEntry() : value_used_(false), status_used_(false) {} |
| 124 |
bool value_used() const { return value_used_; } |
| 125 |
double value() const { return value_; } |
| 126 |
void set_value(double v) { |
| 127 |
value_used_ = true; |
| 128 |
value_ = v; |
| 129 |
} |
| 130 |
bool status_used() const { return status_used_; } |
| 131 |
int status() const { return status_; } |
| 132 |
void set_status(int status) { |
| 133 |
status_used_ = true; |
| 134 |
status_ = status; |
| 135 |
} |
| 136 |
|
| 137 |
private: |
| 138 |
bool value_used_; |
| 139 |
double value_; |
| 140 |
bool status_used_; |
| 141 |
int status_; |
| 142 |
}; |
| 143 |
|
| 144 |
typedef std::unordered_map<int, int64_t> StatusHistogram; |
| 145 |
|
| 146 |
inline void MergeStatusHistogram(const StatusHistogram& from, |
| 147 |
StatusHistogram* to) { |
| 148 |
for (StatusHistogram::const_iterator it = from.begin(); it != from.end(); |
| 149 |
++it) { |
| 150 |
(*to)[it->first] += it->second; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
class Client { |
| 155 |
public: |
| 156 |
Client() |
| 157 |
: timer_(new UsageTimer), |
| 158 |
interarrival_timer_(), |
| 159 |
started_requests_(false), |
| 160 |
last_reset_poll_count_(0) { |
| 161 |
gpr_event_init(&start_requests_); |
| 162 |
} |
| 163 |
virtual ~Client() {} |
| 164 |
|
| 165 |
ClientStats Mark(bool reset) { |
| 166 |
Histogram latencies; |
| 167 |
StatusHistogram statuses; |
| 168 |
UsageTimer::Result timer_result; |
| 169 |
|
| 170 |
MaybeStartRequests(); |
| 171 |
|
| 172 |
int cur_poll_count = GetPollCount(); |
| 173 |
int poll_count = cur_poll_count - last_reset_poll_count_; |
| 174 |
if (reset) { |
| 175 |
std::vector<Histogram> to_merge(threads_.size()); |
| 176 |
std::vector<StatusHistogram> to_merge_status(threads_.size()); |
| 177 |
|
| 178 |
for (size_t i = 0; i < threads_.size(); i++) { |
| 179 |
threads_[i]->BeginSwap(&to_merge[i], &to_merge_status[i]); |
| 180 |
} |
| 181 |
std::unique_ptr<UsageTimer> timer(new UsageTimer); |
| 182 |
timer_.swap(timer); |
| 183 |
for (size_t i = 0; i < threads_.size(); i++) { |
| 184 |
latencies.Merge(to_merge[i]); |
| 185 |
MergeStatusHistogram(to_merge_status[i], &statuses); |
| 186 |
} |
| 187 |
timer_result = timer->Mark(); |
| 188 |
last_reset_poll_count_ = cur_poll_count; |
| 189 |
} else { |
| 190 |
// merge snapshots of each thread histogram |
| 191 |
for (size_t i = 0; i < threads_.size(); i++) { |
| 192 |
threads_[i]->MergeStatsInto(&latencies, &statuses); |
| 193 |
} |
| 194 |
timer_result = timer_->Mark(); |
| 195 |
} |
| 196 |
|
| 197 |
// Print the median latency per interval for one thread. |
| 198 |
// If the number of warmup seconds is x, then the first x + 1 numbers in the |
| 199 |
// vector are from the warmup period and should be discarded. |
| 200 |
if (median_latency_collection_interval_seconds_ > 0) { |
| 201 |
std::vector<double> medians_per_interval = |
| 202 |
threads_[0]->GetMedianPerIntervalList(); |
| 203 |
gpr_log(GPR_INFO, "Num threads: %zu", threads_.size()); |
| 204 |
gpr_log(GPR_INFO, "Number of medians: %zu", medians_per_interval.size()); |
| 205 |
for (size_t j = 0; j < medians_per_interval.size(); j++) { |
| 206 |
gpr_log(GPR_INFO, "%f", medians_per_interval[j]); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
ClientStats stats; |
| 211 |
latencies.FillProto(stats.mutable_latencies()); |
| 212 |
for (StatusHistogram::const_iterator it = statuses.begin(); |
| 213 |
it != statuses.end(); ++it) { |
| 214 |
RequestResultCount* rrc = stats.add_request_results(); |
| 215 |
rrc->set_status_code(it->first); |
| 216 |
rrc->set_count(it->second); |
| 217 |
} |
| 218 |
stats.set_time_elapsed(timer_result.wall); |
| 219 |
stats.set_time_system(timer_result.system); |
| 220 |
stats.set_time_user(timer_result.user); |
| 221 |
stats.set_cq_poll_count(poll_count); |
| 222 |
return stats; |
| 223 |
} |
| 224 |
|
| 225 |
// Must call AwaitThreadsCompletion before destructor to avoid a race |
| 226 |
// between destructor and invocation of virtual ThreadFunc |
| 227 |
void AwaitThreadsCompletion() { |
| 228 |
gpr_atm_rel_store(&thread_pool_done_, static_cast<gpr_atm>(true)); |
| 229 |
DestroyMultithreading(); |
| 230 |
std::unique_lock<std::mutex> g(thread_completion_mu_); |
| 231 |
while (threads_remaining_ != 0) { |
| 232 |
threads_complete_.wait(g); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// Returns the interval (in seconds) between collecting latency medians. If 0, |
| 237 |
// no periodic median latencies will be collected. |
| 238 |
double GetLatencyCollectionIntervalInSeconds() { |
| 239 |
return median_latency_collection_interval_seconds_; |
| 240 |
} |
| 241 |
|
| 242 |
virtual int GetPollCount() { |
| 243 |
// For sync client. |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
bool IsClosedLoop() { return closed_loop_; } |
| 248 |
|
| 249 |
gpr_timespec NextIssueTime(int thread_idx) { |
| 250 |
const gpr_timespec result = next_time_[thread_idx]; |
| 251 |
next_time_[thread_idx] = |
| 252 |
gpr_time_add(next_time_[thread_idx], |
| 253 |
gpr_time_from_nanos(interarrival_timer_.next(thread_idx), |
| 254 |
GPR_TIMESPAN)); |
| 255 |
return result; |
| 256 |
} |
| 257 |
|
| 258 |
bool ThreadCompleted() { |
| 259 |
return static_cast<bool>(gpr_atm_acq_load(&thread_pool_done_)); |
| 260 |
} |
| 261 |
|
| 262 |
class Thread { |
| 263 |
public: |
| 264 |
Thread(Client* client, size_t idx) |
| 265 |
: client_(client), idx_(idx), impl_(&Thread::ThreadFunc, this) {} |
| 266 |
|
| 267 |
~Thread() { impl_.join(); } |
| 268 |
|
| 269 |
void BeginSwap(Histogram* n, StatusHistogram* s) { |
| 270 |
std::lock_guard<std::mutex> g(mu_); |
| 271 |
n->Swap(&histogram_); |
| 272 |
s->swap(statuses_); |
| 273 |
} |
| 274 |
|
| 275 |
void MergeStatsInto(Histogram* hist, StatusHistogram* s) { |
| 276 |
std::unique_lock<std::mutex> g(mu_); |
| 277 |
hist->Merge(histogram_); |
| 278 |
MergeStatusHistogram(statuses_, s); |
| 279 |
} |
| 280 |
|
| 281 |
std::vector<double> GetMedianPerIntervalList() { |
| 282 |
return medians_each_interval_list_; |
| 283 |
} |
| 284 |
|
| 285 |
void UpdateHistogram(HistogramEntry* entry) { |
| 286 |
std::lock_guard<std::mutex> g(mu_); |
| 287 |
if (entry->value_used()) { |
| 288 |
histogram_.Add(entry->value()); |
| 289 |
if (client_->GetLatencyCollectionIntervalInSeconds() > 0) { |
| 290 |
histogram_per_interval_.Add(entry->value()); |
| 291 |
double now = UsageTimer::Now(); |
| 292 |
if ((now - interval_start_time_) >= |
| 293 |
client_->GetLatencyCollectionIntervalInSeconds()) { |
| 294 |
// Record the median latency of requests from the last interval. |
| 295 |
// Divide by 1e3 to get microseconds. |
| 296 |
medians_each_interval_list_.push_back( |
| 297 |
histogram_per_interval_.Percentile(50) / 1e3); |
| 298 |
histogram_per_interval_.Reset(); |
| 299 |
interval_start_time_ = now; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
if (entry->status_used()) { |
| 304 |
statuses_[entry->status()]++; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
private: |
| 309 |
Thread(const Thread&); |
| 310 |
Thread& operator=(const Thread&); |
| 311 |
|
| 312 |
void ThreadFunc() { |
| 313 |
int wait_loop = 0; |
| 314 |
while (!gpr_event_wait( |
| 315 |
&client_->start_requests_, |
| 316 |
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 317 |
gpr_time_from_seconds(20, GPR_TIMESPAN)))) { |
| 318 |
gpr_log(GPR_INFO, "%" PRIdPTR ": Waiting for benchmark to start (%d)", |
| 319 |
idx_, wait_loop); |
| 320 |
wait_loop++; |
| 321 |
} |
| 322 |
|
| 323 |
client_->ThreadFunc(idx_, this); |
| 324 |
client_->CompleteThread(); |
| 325 |
} |
| 326 |
|
| 327 |
std::mutex mu_; |
| 328 |
Histogram histogram_; |
| 329 |
StatusHistogram statuses_; |
| 330 |
Client* client_; |
| 331 |
const size_t idx_; |
| 332 |
std::thread impl_; |
| 333 |
// The following are used only if |
| 334 |
// median_latency_collection_interval_seconds_ is greater than 0 |
| 335 |
Histogram histogram_per_interval_; |
| 336 |
std::vector<double> medians_each_interval_list_; |
| 337 |
double interval_start_time_; |
| 338 |
}; |
| 339 |
|
| 340 |
protected: |
| 341 |
bool closed_loop_; |
| 342 |
gpr_atm thread_pool_done_; |
| 343 |
double median_latency_collection_interval_seconds_; // In seconds |
| 344 |
|
| 345 |
void StartThreads(size_t num_threads) { |
| 346 |
gpr_atm_rel_store(&thread_pool_done_, static_cast<gpr_atm>(false)); |
| 347 |
threads_remaining_ = num_threads; |
| 348 |
for (size_t i = 0; i < num_threads; i++) { |
| 349 |
threads_.emplace_back(new Thread(this, i)); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
void EndThreads() { |
| 354 |
MaybeStartRequests(); |
| 355 |
threads_.clear(); |
| 356 |
} |
| 357 |
|
| 358 |
virtual void DestroyMultithreading() = 0; |
| 359 |
|
| 360 |
void SetupLoadTest(const ClientConfig& config, size_t num_threads) { |
| 361 |
// Set up the load distribution based on the number of threads |
| 362 |
const auto& load = config.load_params(); |
| 363 |
|
| 364 |
std::unique_ptr<RandomDistInterface> random_dist; |
| 365 |
switch (load.load_case()) { |
| 366 |
case LoadParams::kClosedLoop: |
| 367 |
// Closed-loop doesn't use random dist at all |
| 368 |
break; |
| 369 |
case LoadParams::kPoisson: |
| 370 |
random_dist = std::make_unique<ExpDist>(load.poisson().offered_load() / |
| 371 |
num_threads); |
| 372 |
break; |
| 373 |
default: |
| 374 |
GPR_ASSERT(false); |
| 375 |
} |
| 376 |
|
| 377 |
// Set closed_loop_ based on whether or not random_dist is set |
| 378 |
if (!random_dist) { |
| 379 |
closed_loop_ = true; |
| 380 |
} else { |
| 381 |
closed_loop_ = false; |
| 382 |
// set up interarrival timer according to random dist |
| 383 |
interarrival_timer_.init(*random_dist, num_threads); |
| 384 |
const auto now = gpr_now(GPR_CLOCK_MONOTONIC); |
| 385 |
for (size_t i = 0; i < num_threads; i++) { |
| 386 |
next_time_.push_back(gpr_time_add( |
| 387 |
now, |
| 388 |
gpr_time_from_nanos(interarrival_timer_.next(i), GPR_TIMESPAN))); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
std::function<gpr_timespec()> NextIssuer(int thread_idx) { |
| 394 |
return closed_loop_ ? std::function<gpr_timespec()>() |
| 395 |
: std::bind(&Client::NextIssueTime, this, thread_idx); |
| 396 |
} |
| 397 |
|
| 398 |
virtual void ThreadFunc(size_t thread_idx, Client::Thread* t) = 0; |
| 399 |
|
| 400 |
std::vector<std::unique_ptr<Thread>> threads_; |
| 401 |
std::unique_ptr<UsageTimer> timer_; |
| 402 |
|
| 403 |
InterarrivalTimer interarrival_timer_; |
| 404 |
std::vector<gpr_timespec> next_time_; |
| 405 |
|
| 406 |
std::mutex thread_completion_mu_; |
| 407 |
size_t threads_remaining_; |
| 408 |
std::condition_variable threads_complete_; |
| 409 |
|
| 410 |
gpr_event start_requests_; |
| 411 |
bool started_requests_; |
| 412 |
|
| 413 |
int last_reset_poll_count_; |
| 414 |
|
| 415 |
void MaybeStartRequests() { |
| 416 |
if (!started_requests_) { |
| 417 |
started_requests_ = true; |
| 418 |
gpr_event_set(&start_requests_, reinterpret_cast<void*>(1)); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
void CompleteThread() { |
| 423 |
std::lock_guard<std::mutex> g(thread_completion_mu_); |
| 424 |
threads_remaining_--; |
| 425 |
if (threads_remaining_ == 0) { |
| 426 |
threads_complete_.notify_all(); |
| 427 |
} |
| 428 |
} |
| 429 |
}; |
| 430 |
|
| 431 |
template <class StubType, class RequestType> |
| 432 |
class ClientImpl : public Client { |
| 433 |
public: |
| 434 |
ClientImpl(const ClientConfig& config, |
| 435 |
std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> |
| 436 |
create_stub) |
| 437 |
: cores_(gpr_cpu_num_cores()), create_stub_(create_stub) { |
| 438 |
for (int i = 0; i < config.client_channels(); i++) { |
| 439 |
channels_.emplace_back( |
| 440 |
config.server_targets(i % config.server_targets_size()), config, |
| 441 |
create_stub_, i); |
| 442 |
} |
| 443 |
WaitForChannelsToConnect(); |
| 444 |
median_latency_collection_interval_seconds_ = |
| 445 |
config.median_latency_collection_interval_millis() / 1e3; |
| 446 |
ClientRequestCreator<RequestType> create_req(&request_, |
| 447 |
config.payload_config()); |
| 448 |
} |
| 449 |
~ClientImpl() override {} |
| 450 |
const RequestType* request() { return &request_; } |
| 451 |
|
| 452 |
void WaitForChannelsToConnect() { |
| 453 |
int connect_deadline_seconds = 10; |
| 454 |
/* Allow optionally overriding connect_deadline in order |
| 455 |
* to deal with benchmark environments in which the server |
| 456 |
* can take a long time to become ready. */ |
| 457 |
auto channel_connect_timeout_str = |
| 458 |
grpc_core::GetEnv("QPS_WORKER_CHANNEL_CONNECT_TIMEOUT"); |
| 459 |
if (channel_connect_timeout_str.has_value() && |
| 460 |
!channel_connect_timeout_str->empty()) { |
| 461 |
connect_deadline_seconds = atoi(channel_connect_timeout_str->c_str()); |
| 462 |
} |
| 463 |
gpr_log(GPR_INFO, |
| 464 |
"Waiting for up to %d seconds for all channels to connect", |
| 465 |
connect_deadline_seconds); |
| 466 |
gpr_timespec connect_deadline = gpr_time_add( |
| 467 |
gpr_now(GPR_CLOCK_REALTIME), |
| 468 |
gpr_time_from_seconds(connect_deadline_seconds, GPR_TIMESPAN)); |
| 469 |
CompletionQueue cq; |
| 470 |
size_t num_remaining = 0; |
| 471 |
for (auto& c : channels_) { |
| 472 |
if (!c.is_inproc()) { |
| 473 |
Channel* channel = c.get_channel(); |
| 474 |
grpc_connectivity_state last_observed = channel->GetState(true); |
| 475 |
if (last_observed == GRPC_CHANNEL_READY) { |
| 476 |
gpr_log(GPR_INFO, "Channel %p connected!", channel); |
| 477 |
} else { |
| 478 |
num_remaining++; |
| 479 |
channel->NotifyOnStateChange(last_observed, connect_deadline, &cq, |
| 480 |
channel); |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
while (num_remaining > 0) { |
| 485 |
bool ok = false; |
| 486 |
void* tag = nullptr; |
| 487 |
cq.Next(&tag, &ok); |
| 488 |
Channel* channel = static_cast<Channel*>(tag); |
| 489 |
if (!ok) { |
| 490 |
gpr_log(GPR_ERROR, "Channel %p failed to connect within the deadline", |
| 491 |
channel); |
| 492 |
abort(); |
| 493 |
} else { |
| 494 |
grpc_connectivity_state last_observed = channel->GetState(true); |
| 495 |
if (last_observed == GRPC_CHANNEL_READY) { |
| 496 |
gpr_log(GPR_INFO, "Channel %p connected!", channel); |
| 497 |
num_remaining--; |
| 498 |
} else { |
| 499 |
channel->NotifyOnStateChange(last_observed, connect_deadline, &cq, |
| 500 |
channel); |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
protected: |
| 507 |
const int cores_; |
| 508 |
RequestType request_; |
| 509 |
|
| 510 |
class ClientChannelInfo { |
| 511 |
public: |
| 512 |
ClientChannelInfo( |
| 513 |
const std::string& target, const ClientConfig& config, |
| 514 |
std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> |
| 515 |
create_stub, |
| 516 |
int shard) { |
| 517 |
ChannelArguments args; |
| 518 |
args.SetInt("shard_to_ensure_no_subchannel_merges", shard); |
| 519 |
set_channel_args(config, &args); |
| 520 |
|
| 521 |
std::string type; |
| 522 |
if (config.has_security_params() && |
| 523 |
config.security_params().cred_type().empty()) { |
| 524 |
type = kTlsCredentialsType; |
| 525 |
} else { |
| 526 |
type = config.security_params().cred_type(); |
| 527 |
} |
| 528 |
|
| 529 |
std::string inproc_pfx(INPROC_NAME_PREFIX); |
| 530 |
if (!absl::StartsWith(target, inproc_pfx)) { |
| 531 |
channel_ = CreateTestChannel( |
| 532 |
target, type, config.security_params().server_host_override(), |
| 533 |
!config.security_params().use_test_ca(), |
| 534 |
std::shared_ptr<CallCredentials>(), args); |
| 535 |
gpr_log(GPR_INFO, "Connecting to %s", target.c_str()); |
| 536 |
is_inproc_ = false; |
| 537 |
} else { |
| 538 |
std::string tgt = target; |
| 539 |
tgt.erase(0, inproc_pfx.length()); |
| 540 |
int srv_num = std::stoi(tgt); |
| 541 |
channel_ = (*g_inproc_servers)[srv_num]->InProcessChannel(args); |
| 542 |
is_inproc_ = true; |
| 543 |
} |
| 544 |
stub_ = create_stub(channel_); |
| 545 |
} |
| 546 |
Channel* get_channel() { return channel_.get(); } |
| 547 |
StubType* get_stub() { return stub_.get(); } |
| 548 |
bool is_inproc() { return is_inproc_; } |
| 549 |
|
| 550 |
private: |
| 551 |
void set_channel_args(const ClientConfig& config, ChannelArguments* args) { |
| 552 |
for (const auto& channel_arg : config.channel_args()) { |
| 553 |
if (channel_arg.value_case() == ChannelArg::kStrValue) { |
| 554 |
args->SetString(channel_arg.name(), channel_arg.str_value()); |
| 555 |
} else if (channel_arg.value_case() == ChannelArg::kIntValue) { |
| 556 |
args->SetInt(channel_arg.name(), channel_arg.int_value()); |
| 557 |
} else { |
| 558 |
gpr_log(GPR_ERROR, "Empty channel arg value."); |
| 559 |
} |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
std::shared_ptr<Channel> channel_; |
| 564 |
std::unique_ptr<StubType> stub_; |
| 565 |
bool is_inproc_; |
| 566 |
}; |
| 567 |
std::vector<ClientChannelInfo> channels_; |
| 568 |
std::function<std::unique_ptr<StubType>(const std::shared_ptr<Channel>&)> |
| 569 |
create_stub_; |
| 570 |
}; |
| 571 |
|
| 572 |
std::unique_ptr<Client> CreateSynchronousClient(const ClientConfig& config); |
| 573 |
std::unique_ptr<Client> CreateAsyncClient(const ClientConfig& config); |
| 574 |
std::unique_ptr<Client> CreateCallbackClient(const ClientConfig& config); |
| 575 |
std::unique_ptr<Client> CreateGenericAsyncStreamingClient( |
| 576 |
const ClientConfig& config); |
| 577 |
|
| 578 |
} // namespace testing |
| 579 |
} // namespace grpc |
| 580 |
|
| 581 |
#endif |