| 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 |
#include <chrono> |
| 20 |
#include <memory> |
| 21 |
#include <mutex> |
| 22 |
#include <sstream> |
| 23 |
#include <string> |
| 24 |
#include <thread> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include <grpc/grpc.h> |
| 28 |
#include <grpc/support/alloc.h> |
| 29 |
#include <grpc/support/log.h> |
| 30 |
#include <grpc/support/time.h> |
| 31 |
#include <grpcpp/channel.h> |
| 32 |
#include <grpcpp/client_context.h> |
| 33 |
#include <grpcpp/server.h> |
| 34 |
#include <grpcpp/server_builder.h> |
| 35 |
|
| 36 |
#include "src/proto/grpc/testing/benchmark_service.grpc.pb.h" |
| 37 |
#include "test/cpp/qps/client.h" |
| 38 |
#include "test/cpp/qps/interarrival.h" |
| 39 |
#include "test/cpp/qps/usage_timer.h" |
| 40 |
|
| 41 |
namespace grpc { |
| 42 |
namespace testing { |
| 43 |
|
| 44 |
static std::unique_ptr<BenchmarkService::Stub> BenchmarkStubCreator( |
| 45 |
const std::shared_ptr<Channel>& ch) { |
| 46 |
return BenchmarkService::NewStub(ch); |
| 47 |
} |
| 48 |
|
| 49 |
class SynchronousClient |
| 50 |
: public ClientImpl<BenchmarkService::Stub, SimpleRequest> { |
| 51 |
public: |
| 52 |
explicit SynchronousClient(const ClientConfig& config) |
| 53 |
: ClientImpl<BenchmarkService::Stub, SimpleRequest>( |
| 54 |
config, BenchmarkStubCreator) { |
| 55 |
num_threads_ = |
| 56 |
config.outstanding_rpcs_per_channel() * config.client_channels(); |
| 57 |
responses_.resize(num_threads_); |
| 58 |
SetupLoadTest(config, num_threads_); |
| 59 |
} |
| 60 |
|
| 61 |
~SynchronousClient() override {} |
| 62 |
|
| 63 |
virtual bool InitThreadFuncImpl(size_t thread_idx) = 0; |
| 64 |
virtual bool ThreadFuncImpl(HistogramEntry* entry, size_t thread_idx) = 0; |
| 65 |
|
| 66 |
void ThreadFunc(size_t thread_idx, Thread* t) override { |
| 67 |
if (!InitThreadFuncImpl(thread_idx)) { |
| 68 |
return; |
| 69 |
} |
| 70 |
for (;;) { |
| 71 |
// run the loop body |
| 72 |
HistogramEntry entry; |
| 73 |
const bool thread_still_ok = ThreadFuncImpl(&entry, thread_idx); |
| 74 |
t->UpdateHistogram(&entry); |
| 75 |
if (!thread_still_ok || ThreadCompleted()) { |
| 76 |
return; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
protected: |
| 82 |
// WaitToIssue returns false if we realize that we need to break out |
| 83 |
bool WaitToIssue(int thread_idx) { |
| 84 |
if (!closed_loop_) { |
| 85 |
const gpr_timespec next_issue_time = NextIssueTime(thread_idx); |
| 86 |
// Avoid sleeping for too long continuously because we might |
| 87 |
// need to terminate before then. This is an issue since |
| 88 |
// exponential distribution can occasionally produce bad outliers |
| 89 |
while (true) { |
| 90 |
const gpr_timespec one_sec_delay = |
| 91 |
gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), |
| 92 |
gpr_time_from_seconds(1, GPR_TIMESPAN)); |
| 93 |
if (gpr_time_cmp(next_issue_time, one_sec_delay) <= 0) { |
| 94 |
gpr_sleep_until(next_issue_time); |
| 95 |
return true; |
| 96 |
} else { |
| 97 |
gpr_sleep_until(one_sec_delay); |
| 98 |
if (gpr_atm_acq_load(&thread_pool_done_) != static_cast<gpr_atm>(0)) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
size_t num_threads_; |
| 108 |
std::vector<SimpleResponse> responses_; |
| 109 |
}; |
| 110 |
|
| 111 |
class SynchronousUnaryClient final : public SynchronousClient { |
| 112 |
public: |
| 113 |
explicit SynchronousUnaryClient(const ClientConfig& config) |
| 114 |
: SynchronousClient(config) { |
| 115 |
StartThreads(num_threads_); |
| 116 |
} |
| 117 |
~SynchronousUnaryClient() override {} |
| 118 |
|
| 119 |
bool InitThreadFuncImpl(size_t /*thread_idx*/) override { return true; } |
| 120 |
|
| 121 |
bool ThreadFuncImpl(HistogramEntry* entry, size_t thread_idx) override { |
| 122 |
if (!WaitToIssue(thread_idx)) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 126 |
double start = UsageTimer::Now(); |
| 127 |
grpc::ClientContext context; |
| 128 |
grpc::Status s = |
| 129 |
stub->UnaryCall(&context, request_, &responses_[thread_idx]); |
| 130 |
if (s.ok()) { |
| 131 |
entry->set_value((UsageTimer::Now() - start) * 1e9); |
| 132 |
} |
| 133 |
entry->set_status(s.error_code()); |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
private: |
| 138 |
void DestroyMultithreading() final { EndThreads(); } |
| 139 |
}; |
| 140 |
|
| 141 |
template <class StreamType> |
| 142 |
class SynchronousStreamingClient : public SynchronousClient { |
| 143 |
public: |
| 144 |
explicit SynchronousStreamingClient(const ClientConfig& config) |
| 145 |
: SynchronousClient(config), |
| 146 |
context_(num_threads_), |
| 147 |
stream_(num_threads_), |
| 148 |
stream_mu_(num_threads_), |
| 149 |
shutdown_(num_threads_), |
| 150 |
messages_per_stream_(config.messages_per_stream()), |
| 151 |
messages_issued_(num_threads_) { |
| 152 |
StartThreads(num_threads_); |
| 153 |
} |
| 154 |
~SynchronousStreamingClient() override { |
| 155 |
CleanupAllStreams([this](size_t thread_idx) { |
| 156 |
// Don't log any kind of error since we may have canceled this |
| 157 |
stream_[thread_idx]->Finish().IgnoreError(); |
| 158 |
}); |
| 159 |
} |
| 160 |
|
| 161 |
protected: |
| 162 |
std::vector<grpc::ClientContext> context_; |
| 163 |
std::vector<std::unique_ptr<StreamType>> stream_; |
| 164 |
// stream_mu_ is only needed when changing an element of stream_ or context_ |
| 165 |
std::vector<std::mutex> stream_mu_; |
| 166 |
// use struct Bool rather than bool because vector<bool> is not concurrent |
| 167 |
struct Bool { |
| 168 |
bool val; |
| 169 |
Bool() : val(false) {} |
| 170 |
}; |
| 171 |
std::vector<Bool> shutdown_; |
| 172 |
const int messages_per_stream_; |
| 173 |
std::vector<int> messages_issued_; |
| 174 |
|
| 175 |
void FinishStream(HistogramEntry* entry, size_t thread_idx) { |
| 176 |
Status s = stream_[thread_idx]->Finish(); |
| 177 |
// don't set the value since the stream is failed and shouldn't be timed |
| 178 |
entry->set_status(s.error_code()); |
| 179 |
if (!s.ok()) { |
| 180 |
std::lock_guard<std::mutex> l(stream_mu_[thread_idx]); |
| 181 |
if (!shutdown_[thread_idx].val) { |
| 182 |
gpr_log(GPR_ERROR, "Stream %" PRIuPTR " received an error %s", |
| 183 |
thread_idx, s.error_message().c_str()); |
| 184 |
} |
| 185 |
} |
| 186 |
// Lock the stream_mu_ now because the client context could change |
| 187 |
std::lock_guard<std::mutex> l(stream_mu_[thread_idx]); |
| 188 |
context_[thread_idx].~ClientContext(); |
| 189 |
new (&context_[thread_idx]) ClientContext(); |
| 190 |
} |
| 191 |
|
| 192 |
void CleanupAllStreams(const std::function<void(size_t)>& cleaner) { |
| 193 |
std::vector<std::thread> cleanup_threads; |
| 194 |
for (size_t i = 0; i < num_threads_; i++) { |
| 195 |
cleanup_threads.emplace_back([this, i, cleaner] { |
| 196 |
std::lock_guard<std::mutex> l(stream_mu_[i]); |
| 197 |
shutdown_[i].val = true; |
| 198 |
if (stream_[i]) { |
| 199 |
cleaner(i); |
| 200 |
} |
| 201 |
}); |
| 202 |
} |
| 203 |
for (auto& th : cleanup_threads) { |
| 204 |
th.join(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
private: |
| 209 |
void DestroyMultithreading() final { |
| 210 |
CleanupAllStreams( |
| 211 |
[this](size_t thread_idx) { context_[thread_idx].TryCancel(); }); |
| 212 |
EndThreads(); |
| 213 |
} |
| 214 |
}; |
| 215 |
|
| 216 |
class SynchronousStreamingPingPongClient final |
| 217 |
: public SynchronousStreamingClient< |
| 218 |
grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>> { |
| 219 |
public: |
| 220 |
explicit SynchronousStreamingPingPongClient(const ClientConfig& config) |
| 221 |
: SynchronousStreamingClient(config) {} |
| 222 |
~SynchronousStreamingPingPongClient() override { |
| 223 |
CleanupAllStreams( |
| 224 |
[this](size_t thread_idx) { stream_[thread_idx]->WritesDone(); }); |
| 225 |
} |
| 226 |
|
| 227 |
private: |
| 228 |
bool InitThreadFuncImpl(size_t thread_idx) override { |
| 229 |
auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 230 |
std::lock_guard<std::mutex> l(stream_mu_[thread_idx]); |
| 231 |
if (!shutdown_[thread_idx].val) { |
| 232 |
stream_[thread_idx] = stub->StreamingCall(&context_[thread_idx]); |
| 233 |
} else { |
| 234 |
return false; |
| 235 |
} |
| 236 |
messages_issued_[thread_idx] = 0; |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
bool ThreadFuncImpl(HistogramEntry* entry, size_t thread_idx) override { |
| 241 |
if (!WaitToIssue(thread_idx)) { |
| 242 |
return true; |
| 243 |
} |
| 244 |
double start = UsageTimer::Now(); |
| 245 |
if (stream_[thread_idx]->Write(request_) && |
| 246 |
stream_[thread_idx]->Read(&responses_[thread_idx])) { |
| 247 |
entry->set_value((UsageTimer::Now() - start) * 1e9); |
| 248 |
// don't set the status since there isn't one yet |
| 249 |
if ((messages_per_stream_ != 0) && |
| 250 |
(++messages_issued_[thread_idx] < messages_per_stream_)) { |
| 251 |
return true; |
| 252 |
} else if (messages_per_stream_ == 0) { |
| 253 |
return true; |
| 254 |
} else { |
| 255 |
// Fall through to the below resetting code after finish |
| 256 |
} |
| 257 |
} |
| 258 |
stream_[thread_idx]->WritesDone(); |
| 259 |
FinishStream(entry, thread_idx); |
| 260 |
auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 261 |
std::lock_guard<std::mutex> l(stream_mu_[thread_idx]); |
| 262 |
if (!shutdown_[thread_idx].val) { |
| 263 |
stream_[thread_idx] = stub->StreamingCall(&context_[thread_idx]); |
| 264 |
} else { |
| 265 |
stream_[thread_idx].reset(); |
| 266 |
return false; |
| 267 |
} |
| 268 |
messages_issued_[thread_idx] = 0; |
| 269 |
return true; |
| 270 |
} |
| 271 |
}; |
| 272 |
|
| 273 |
class SynchronousStreamingFromClientClient final |
| 274 |
: public SynchronousStreamingClient<grpc::ClientWriter<SimpleRequest>> { |
| 275 |
public: |
| 276 |
explicit SynchronousStreamingFromClientClient(const ClientConfig& config) |
| 277 |
: SynchronousStreamingClient(config), last_issue_(num_threads_) {} |
| 278 |
~SynchronousStreamingFromClientClient() override { |
| 279 |
CleanupAllStreams( |
| 280 |
[this](size_t thread_idx) { stream_[thread_idx]->WritesDone(); }); |
| 281 |
} |
| 282 |
|
| 283 |
private: |
| 284 |
std::vector<double> last_issue_; |
| 285 |
|
| 286 |
bool InitThreadFuncImpl(size_t thread_idx) override { |
| 287 |
auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 288 |
std::lock_guard<std::mutex> l(stream_mu_[thread_idx]); |
| 289 |
if (!shutdown_[thread_idx].val) { |
| 290 |
stream_[thread_idx] = stub->StreamingFromClient(&context_[thread_idx], |
| 291 |
&responses_[thread_idx]); |
| 292 |
} else { |
| 293 |
return false; |
| 294 |
} |
| 295 |
last_issue_[thread_idx] = UsageTimer::Now(); |
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
bool ThreadFuncImpl(HistogramEntry* entry, size_t thread_idx) override { |
| 300 |
// Figure out how to make histogram sensible if this is rate-paced |
| 301 |
if (!WaitToIssue(thread_idx)) { |
| 302 |
return true; |
| 303 |
} |
| 304 |
if (stream_[thread_idx]->Write(request_)) { |
| 305 |
double now = UsageTimer::Now(); |
| 306 |
entry->set_value((now - last_issue_[thread_idx]) * 1e9); |
| 307 |
last_issue_[thread_idx] = now; |
| 308 |
return true; |
| 309 |
} |
| 310 |
stream_[thread_idx]->WritesDone(); |
| 311 |
FinishStream(entry, thread_idx); |
| 312 |
auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 313 |
std::lock_guard<std::mutex> l(stream_mu_[thread_idx]); |
| 314 |
if (!shutdown_[thread_idx].val) { |
| 315 |
stream_[thread_idx] = stub->StreamingFromClient(&context_[thread_idx], |
| 316 |
&responses_[thread_idx]); |
| 317 |
} else { |
| 318 |
stream_[thread_idx].reset(); |
| 319 |
return false; |
| 320 |
} |
| 321 |
return true; |
| 322 |
} |
| 323 |
}; |
| 324 |
|
| 325 |
class SynchronousStreamingFromServerClient final |
| 326 |
: public SynchronousStreamingClient<grpc::ClientReader<SimpleResponse>> { |
| 327 |
public: |
| 328 |
explicit SynchronousStreamingFromServerClient(const ClientConfig& config) |
| 329 |
: SynchronousStreamingClient(config), last_recv_(num_threads_) {} |
| 330 |
~SynchronousStreamingFromServerClient() override {} |
| 331 |
|
| 332 |
private: |
| 333 |
std::vector<double> last_recv_; |
| 334 |
|
| 335 |
bool InitThreadFuncImpl(size_t thread_idx) override { |
| 336 |
auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 337 |
std::lock_guard<std::mutex> l(stream_mu_[thread_idx]); |
| 338 |
if (!shutdown_[thread_idx].val) { |
| 339 |
stream_[thread_idx] = |
| 340 |
stub->StreamingFromServer(&context_[thread_idx], request_); |
| 341 |
} else { |
| 342 |
return false; |
| 343 |
} |
| 344 |
last_recv_[thread_idx] = UsageTimer::Now(); |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
bool ThreadFuncImpl(HistogramEntry* entry, size_t thread_idx) override { |
| 349 |
if (stream_[thread_idx]->Read(&responses_[thread_idx])) { |
| 350 |
double now = UsageTimer::Now(); |
| 351 |
entry->set_value((now - last_recv_[thread_idx]) * 1e9); |
| 352 |
last_recv_[thread_idx] = now; |
| 353 |
return true; |
| 354 |
} |
| 355 |
FinishStream(entry, thread_idx); |
| 356 |
auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 357 |
std::lock_guard<std::mutex> l(stream_mu_[thread_idx]); |
| 358 |
if (!shutdown_[thread_idx].val) { |
| 359 |
stream_[thread_idx] = |
| 360 |
stub->StreamingFromServer(&context_[thread_idx], request_); |
| 361 |
} else { |
| 362 |
stream_[thread_idx].reset(); |
| 363 |
return false; |
| 364 |
} |
| 365 |
return true; |
| 366 |
} |
| 367 |
}; |
| 368 |
|
| 369 |
class SynchronousStreamingBothWaysClient final |
| 370 |
: public SynchronousStreamingClient< |
| 371 |
grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>> { |
| 372 |
public: |
| 373 |
explicit SynchronousStreamingBothWaysClient(const ClientConfig& config) |
| 374 |
: SynchronousStreamingClient(config) {} |
| 375 |
~SynchronousStreamingBothWaysClient() override { |
| 376 |
CleanupAllStreams( |
| 377 |
[this](size_t thread_idx) { stream_[thread_idx]->WritesDone(); }); |
| 378 |
} |
| 379 |
|
| 380 |
private: |
| 381 |
bool InitThreadFuncImpl(size_t thread_idx) override { |
| 382 |
auto* stub = channels_[thread_idx % channels_.size()].get_stub(); |
| 383 |
std::lock_guard<std::mutex> l(stream_mu_[thread_idx]); |
| 384 |
if (!shutdown_[thread_idx].val) { |
| 385 |
stream_[thread_idx] = stub->StreamingBothWays(&context_[thread_idx]); |
| 386 |
} else { |
| 387 |
return false; |
| 388 |
} |
| 389 |
return true; |
| 390 |
} |
| 391 |
|
| 392 |
bool ThreadFuncImpl(HistogramEntry* /*entry*/, |
| 393 |
size_t /*thread_idx*/) override { |
| 394 |
// TODO (vjpai): Do this |
| 395 |
return true; |
| 396 |
} |
| 397 |
}; |
| 398 |
|
| 399 |
std::unique_ptr<Client> CreateSynchronousClient(const ClientConfig& config) { |
| 400 |
GPR_ASSERT(!config.use_coalesce_api()); // not supported yet. |
| 401 |
switch (config.rpc_type()) { |
| 402 |
case UNARY: |
| 403 |
return std::unique_ptr<Client>(new SynchronousUnaryClient(config)); |
| 404 |
case STREAMING: |
| 405 |
return std::unique_ptr<Client>( |
| 406 |
new SynchronousStreamingPingPongClient(config)); |
| 407 |
case STREAMING_FROM_CLIENT: |
| 408 |
return std::unique_ptr<Client>( |
| 409 |
new SynchronousStreamingFromClientClient(config)); |
| 410 |
case STREAMING_FROM_SERVER: |
| 411 |
return std::unique_ptr<Client>( |
| 412 |
new SynchronousStreamingFromServerClient(config)); |
| 413 |
case STREAMING_BOTH_WAYS: |
| 414 |
return std::unique_ptr<Client>( |
| 415 |
new SynchronousStreamingBothWaysClient(config)); |
| 416 |
default: |
| 417 |
assert(false); |
| 418 |
return nullptr; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
} // namespace testing |
| 423 |
} // namespace grpc |