callback_streaming_ping_pong.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. *
  3. * Copyright 2019 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. #ifndef TEST_CPP_MICROBENCHMARKS_CALLBACK_STREAMING_PING_PONG_H
  19. #define TEST_CPP_MICROBENCHMARKS_CALLBACK_STREAMING_PING_PONG_H
  20. #include <sstream>
  21. #include <benchmark/benchmark.h>
  22. #include "src/core/lib/profiling/timers.h"
  23. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  24. #include "test/cpp/microbenchmarks/callback_test_service.h"
  25. #include "test/cpp/microbenchmarks/fullstack_context_mutators.h"
  26. #include "test/cpp/microbenchmarks/fullstack_fixtures.h"
  27. namespace grpc {
  28. namespace testing {
  29. /*******************************************************************************
  30. * BENCHMARKING KERNELS
  31. */
  32. class BidiClient : public grpc::ClientBidiReactor<EchoRequest, EchoResponse> {
  33. public:
  34. BidiClient(benchmark::State* state, EchoTestService::Stub* stub,
  35. ClientContext* cli_ctx, EchoRequest* request,
  36. EchoResponse* response)
  37. : state_{state},
  38. stub_{stub},
  39. cli_ctx_{cli_ctx},
  40. request_{request},
  41. response_{response} {
  42. msgs_size_ = state->range(0);
  43. msgs_to_send_ = state->range(1);
  44. StartNewRpc();
  45. }
  46. void OnReadDone(bool ok) override {
  47. if (!ok) {
  48. gpr_log(GPR_ERROR, "Client read failed");
  49. return;
  50. }
  51. MaybeWrite();
  52. }
  53. void OnWriteDone(bool ok) override {
  54. if (!ok) {
  55. gpr_log(GPR_ERROR, "Client write failed");
  56. return;
  57. }
  58. writes_complete_++;
  59. StartRead(response_);
  60. }
  61. void OnDone(const Status& s) override {
  62. GPR_ASSERT(s.ok());
  63. GPR_ASSERT(writes_complete_ == msgs_to_send_);
  64. if (state_->KeepRunning()) {
  65. writes_complete_ = 0;
  66. StartNewRpc();
  67. } else {
  68. std::unique_lock<std::mutex> l(mu);
  69. done = true;
  70. cv.notify_one();
  71. }
  72. }
  73. void StartNewRpc() {
  74. cli_ctx_->~ClientContext();
  75. new (cli_ctx_) ClientContext();
  76. cli_ctx_->AddMetadata(kServerMessageSize, std::to_string(msgs_size_));
  77. stub_->async()->BidiStream(cli_ctx_, this);
  78. MaybeWrite();
  79. StartCall();
  80. }
  81. void Await() {
  82. std::unique_lock<std::mutex> l(mu);
  83. while (!done) {
  84. cv.wait(l);
  85. }
  86. }
  87. private:
  88. void MaybeWrite() {
  89. if (writes_complete_ < msgs_to_send_) {
  90. StartWrite(request_);
  91. } else {
  92. StartWritesDone();
  93. }
  94. }
  95. benchmark::State* state_;
  96. EchoTestService::Stub* stub_;
  97. ClientContext* cli_ctx_;
  98. EchoRequest* request_;
  99. EchoResponse* response_;
  100. int writes_complete_{0};
  101. int msgs_to_send_;
  102. int msgs_size_;
  103. std::mutex mu;
  104. std::condition_variable cv;
  105. bool done = false;
  106. };
  107. template <class Fixture, class ClientContextMutator, class ServerContextMutator>
  108. static void BM_CallbackBidiStreaming(benchmark::State& state) {
  109. int message_size = state.range(0);
  110. int max_ping_pongs = state.range(1);
  111. CallbackStreamingTestService service;
  112. std::unique_ptr<Fixture> fixture(new Fixture(&service));
  113. std::unique_ptr<EchoTestService::Stub> stub_(
  114. EchoTestService::NewStub(fixture->channel()));
  115. EchoRequest request;
  116. EchoResponse response;
  117. ClientContext cli_ctx;
  118. if (message_size > 0) {
  119. request.set_message(std::string(message_size, 'a'));
  120. } else {
  121. request.set_message("");
  122. }
  123. if (state.KeepRunning()) {
  124. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  125. BidiClient test{&state, stub_.get(), &cli_ctx, &request, &response};
  126. test.Await();
  127. }
  128. fixture->Finish(state);
  129. fixture.reset();
  130. state.SetBytesProcessed(2 * message_size * max_ping_pongs *
  131. state.iterations());
  132. }
  133. } // namespace testing
  134. } // namespace grpc
  135. #endif // TEST_CPP_MICROBENCHMARKS_CALLBACK_STREAMING_PING_PONG_H