fullstack_unary_ping_pong.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. *
  3. * Copyright 2016 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_FULLSTACK_UNARY_PING_PONG_H
  20. #define TEST_CPP_MICROBENCHMARKS_FULLSTACK_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/fullstack_context_mutators.h"
  26. #include "test/cpp/microbenchmarks/fullstack_fixtures.h"
  27. namespace grpc {
  28. namespace testing {
  29. /*******************************************************************************
  30. * BENCHMARKING KERNELS
  31. */
  32. static void* tag(intptr_t x) { return reinterpret_cast<void*>(x); }
  33. template <class Fixture, class ClientContextMutator, class ServerContextMutator>
  34. static void BM_UnaryPingPong(benchmark::State& state) {
  35. EchoTestService::AsyncService service;
  36. std::unique_ptr<Fixture> fixture(new Fixture(&service));
  37. EchoRequest send_request;
  38. EchoResponse send_response;
  39. EchoResponse recv_response;
  40. if (state.range(0) > 0) {
  41. send_request.set_message(std::string(state.range(0), 'a'));
  42. }
  43. if (state.range(1) > 0) {
  44. send_response.set_message(std::string(state.range(1), 'a'));
  45. }
  46. Status recv_status;
  47. struct ServerEnv {
  48. ServerContext ctx;
  49. EchoRequest recv_request;
  50. grpc::ServerAsyncResponseWriter<EchoResponse> response_writer;
  51. ServerEnv() : response_writer(&ctx) {}
  52. };
  53. uint8_t server_env_buffer[2 * sizeof(ServerEnv)];
  54. ServerEnv* server_env[2] = {
  55. reinterpret_cast<ServerEnv*>(server_env_buffer),
  56. reinterpret_cast<ServerEnv*>(server_env_buffer + sizeof(ServerEnv))};
  57. new (server_env[0]) ServerEnv;
  58. new (server_env[1]) ServerEnv;
  59. service.RequestEcho(&server_env[0]->ctx, &server_env[0]->recv_request,
  60. &server_env[0]->response_writer, fixture->cq(),
  61. fixture->cq(), tag(0));
  62. service.RequestEcho(&server_env[1]->ctx, &server_env[1]->recv_request,
  63. &server_env[1]->response_writer, fixture->cq(),
  64. fixture->cq(), tag(1));
  65. std::unique_ptr<EchoTestService::Stub> stub(
  66. EchoTestService::NewStub(fixture->channel()));
  67. for (auto _ : state) {
  68. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  69. recv_response.Clear();
  70. ClientContext cli_ctx;
  71. ClientContextMutator cli_ctx_mut(&cli_ctx);
  72. std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
  73. stub->AsyncEcho(&cli_ctx, send_request, fixture->cq()));
  74. response_reader->Finish(&recv_response, &recv_status, tag(4));
  75. void* t;
  76. bool ok;
  77. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  78. GPR_ASSERT(ok);
  79. GPR_ASSERT(t == tag(0) || t == tag(1));
  80. intptr_t slot = reinterpret_cast<intptr_t>(t);
  81. ServerEnv* senv = server_env[slot];
  82. ServerContextMutator svr_ctx_mut(&senv->ctx);
  83. senv->response_writer.Finish(send_response, Status::OK, tag(3));
  84. for (int i = (1 << 3) | (1 << 4); i != 0;) {
  85. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  86. GPR_ASSERT(ok);
  87. int tagnum = static_cast<int>(reinterpret_cast<intptr_t>(t));
  88. GPR_ASSERT(i & (1 << tagnum));
  89. i -= 1 << tagnum;
  90. }
  91. GPR_ASSERT(recv_status.ok());
  92. senv->~ServerEnv();
  93. senv = new (senv) ServerEnv();
  94. service.RequestEcho(&senv->ctx, &senv->recv_request, &senv->response_writer,
  95. fixture->cq(), fixture->cq(), tag(slot));
  96. }
  97. fixture->Finish(state);
  98. fixture.reset();
  99. server_env[0]->~ServerEnv();
  100. server_env[1]->~ServerEnv();
  101. state.SetBytesProcessed(state.range(0) * state.iterations() +
  102. state.range(1) * state.iterations());
  103. }
  104. } // namespace testing
  105. } // namespace grpc
  106. #endif // TEST_CPP_MICROBENCHMARKS_FULLSTACK_UNARY_PING_PONG_H