callback_unary_ping_pong.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /* Benchmark gRPC end2end in various configurations */
  19. #ifndef TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_PING_PONG_H
  20. #define TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_PING_PONG_H
  21. #include <sstream>
  22. #include <benchmark/benchmark.h>
  23. #include "src/core/lib/profiling/timers.h"
  24. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  25. #include "test/cpp/microbenchmarks/callback_test_service.h"
  26. #include "test/cpp/microbenchmarks/fullstack_context_mutators.h"
  27. #include "test/cpp/microbenchmarks/fullstack_fixtures.h"
  28. namespace grpc {
  29. namespace testing {
  30. /*******************************************************************************
  31. * BENCHMARKING KERNELS
  32. */
  33. inline void SendCallbackUnaryPingPong(
  34. benchmark::State* state, ClientContext* cli_ctx, EchoRequest* request,
  35. EchoResponse* response, EchoTestService::Stub* stub_, bool* done,
  36. std::mutex* mu, std::condition_variable* cv) {
  37. int response_msgs_size = state->range(1);
  38. cli_ctx->AddMetadata(kServerMessageSize, std::to_string(response_msgs_size));
  39. stub_->async()->Echo(
  40. cli_ctx, request, response,
  41. [state, cli_ctx, request, response, stub_, done, mu, cv](Status s) {
  42. GPR_ASSERT(s.ok());
  43. if (state->KeepRunning()) {
  44. cli_ctx->~ClientContext();
  45. new (cli_ctx) ClientContext();
  46. SendCallbackUnaryPingPong(state, cli_ctx, request, response, stub_,
  47. done, mu, cv);
  48. } else {
  49. std::lock_guard<std::mutex> l(*mu);
  50. *done = true;
  51. cv->notify_one();
  52. }
  53. });
  54. };
  55. template <class Fixture, class ClientContextMutator, class ServerContextMutator>
  56. static void BM_CallbackUnaryPingPong(benchmark::State& state) {
  57. int request_msgs_size = state.range(0);
  58. int response_msgs_size = state.range(1);
  59. CallbackStreamingTestService service;
  60. std::unique_ptr<Fixture> fixture(new Fixture(&service));
  61. std::unique_ptr<EchoTestService::Stub> stub_(
  62. EchoTestService::NewStub(fixture->channel()));
  63. EchoRequest request;
  64. EchoResponse response;
  65. ClientContext cli_ctx;
  66. if (request_msgs_size > 0) {
  67. request.set_message(std::string(request_msgs_size, 'a'));
  68. } else {
  69. request.set_message("");
  70. }
  71. std::mutex mu;
  72. std::condition_variable cv;
  73. bool done = false;
  74. if (state.KeepRunning()) {
  75. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  76. SendCallbackUnaryPingPong(&state, &cli_ctx, &request, &response,
  77. stub_.get(), &done, &mu, &cv);
  78. }
  79. std::unique_lock<std::mutex> l(mu);
  80. while (!done) {
  81. cv.wait(l);
  82. }
  83. fixture->Finish(state);
  84. fixture.reset();
  85. state.SetBytesProcessed(request_msgs_size * state.iterations() +
  86. response_msgs_size * state.iterations());
  87. }
  88. } // namespace testing
  89. } // namespace grpc
  90. #endif // TEST_CPP_MICROBENCHMARKS_FULLSTACK_UNARY_PING_PONG_H