fullstack_streaming_pump.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_STREAMING_PUMP_H
  20. #define TEST_CPP_MICROBENCHMARKS_FULLSTACK_STREAMING_PUMP_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>
  34. static void BM_PumpStreamClientToServer(benchmark::State& state) {
  35. EchoTestService::AsyncService service;
  36. std::unique_ptr<Fixture> fixture(new Fixture(&service));
  37. {
  38. EchoRequest send_request;
  39. EchoRequest recv_request;
  40. if (state.range(0) > 0) {
  41. send_request.set_message(std::string(state.range(0), 'a'));
  42. }
  43. Status recv_status;
  44. ServerContext svr_ctx;
  45. ServerAsyncReaderWriter<EchoResponse, EchoRequest> response_rw(&svr_ctx);
  46. service.RequestBidiStream(&svr_ctx, &response_rw, fixture->cq(),
  47. fixture->cq(), tag(0));
  48. std::unique_ptr<EchoTestService::Stub> stub(
  49. EchoTestService::NewStub(fixture->channel()));
  50. ClientContext cli_ctx;
  51. auto request_rw = stub->AsyncBidiStream(&cli_ctx, fixture->cq(), tag(1));
  52. int need_tags = (1 << 0) | (1 << 1);
  53. void* t;
  54. bool ok;
  55. while (need_tags) {
  56. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  57. GPR_ASSERT(ok);
  58. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  59. GPR_ASSERT(need_tags & (1 << i));
  60. need_tags &= ~(1 << i);
  61. }
  62. response_rw.Read(&recv_request, tag(0));
  63. for (auto _ : state) {
  64. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  65. request_rw->Write(send_request, tag(1));
  66. while (true) {
  67. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  68. if (t == tag(0)) {
  69. response_rw.Read(&recv_request, tag(0));
  70. } else if (t == tag(1)) {
  71. break;
  72. } else {
  73. GPR_ASSERT(false);
  74. }
  75. }
  76. }
  77. request_rw->WritesDone(tag(1));
  78. need_tags = (1 << 0) | (1 << 1);
  79. while (need_tags) {
  80. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  81. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  82. GPR_ASSERT(need_tags & (1 << i));
  83. need_tags &= ~(1 << i);
  84. }
  85. response_rw.Finish(Status::OK, tag(0));
  86. Status final_status;
  87. request_rw->Finish(&final_status, tag(1));
  88. need_tags = (1 << 0) | (1 << 1);
  89. while (need_tags) {
  90. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  91. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  92. GPR_ASSERT(need_tags & (1 << i));
  93. need_tags &= ~(1 << i);
  94. }
  95. GPR_ASSERT(final_status.ok());
  96. }
  97. fixture->Finish(state);
  98. fixture.reset();
  99. state.SetBytesProcessed(state.range(0) * state.iterations());
  100. }
  101. template <class Fixture>
  102. static void BM_PumpStreamServerToClient(benchmark::State& state) {
  103. EchoTestService::AsyncService service;
  104. std::unique_ptr<Fixture> fixture(new Fixture(&service));
  105. {
  106. EchoResponse send_response;
  107. EchoResponse recv_response;
  108. if (state.range(0) > 0) {
  109. send_response.set_message(std::string(state.range(0), 'a'));
  110. }
  111. Status recv_status;
  112. ServerContext svr_ctx;
  113. ServerAsyncReaderWriter<EchoResponse, EchoRequest> response_rw(&svr_ctx);
  114. service.RequestBidiStream(&svr_ctx, &response_rw, fixture->cq(),
  115. fixture->cq(), tag(0));
  116. std::unique_ptr<EchoTestService::Stub> stub(
  117. EchoTestService::NewStub(fixture->channel()));
  118. ClientContext cli_ctx;
  119. auto request_rw = stub->AsyncBidiStream(&cli_ctx, fixture->cq(), tag(1));
  120. int need_tags = (1 << 0) | (1 << 1);
  121. void* t;
  122. bool ok;
  123. while (need_tags) {
  124. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  125. GPR_ASSERT(ok);
  126. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  127. GPR_ASSERT(need_tags & (1 << i));
  128. need_tags &= ~(1 << i);
  129. }
  130. request_rw->Read(&recv_response, tag(0));
  131. for (auto _ : state) {
  132. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  133. response_rw.Write(send_response, tag(1));
  134. while (true) {
  135. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  136. if (t == tag(0)) {
  137. request_rw->Read(&recv_response, tag(0));
  138. } else if (t == tag(1)) {
  139. break;
  140. } else {
  141. GPR_ASSERT(false);
  142. }
  143. }
  144. }
  145. response_rw.Finish(Status::OK, tag(1));
  146. need_tags = (1 << 0) | (1 << 1);
  147. while (need_tags) {
  148. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  149. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  150. GPR_ASSERT(need_tags & (1 << i));
  151. need_tags &= ~(1 << i);
  152. }
  153. }
  154. fixture->Finish(state);
  155. fixture.reset();
  156. state.SetBytesProcessed(state.range(0) * state.iterations());
  157. }
  158. } // namespace testing
  159. } // namespace grpc
  160. #endif // TEST_CPP_MICROBENCHMARKS_FULLSTACK_FIXTURES_H