fullstack_streaming_ping_pong.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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_PING_PONG_H
  20. #define TEST_CPP_MICROBENCHMARKS_FULLSTACK_STREAMING_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. // Repeatedly makes Streaming Bidi calls (exchanging a configurable number of
  34. // messages in each call) in a loop on a single channel
  35. //
  36. // First parmeter (i.e state.range(0)): Message size (in bytes) to use
  37. // Second parameter (i.e state.range(1)): Number of ping pong messages.
  38. // Note: One ping-pong means two messages (one from client to server and
  39. // the other from server to client):
  40. template <class Fixture, class ClientContextMutator, class ServerContextMutator>
  41. static void BM_StreamingPingPong(benchmark::State& state) {
  42. const int msg_size = state.range(0);
  43. const int max_ping_pongs = state.range(1);
  44. EchoTestService::AsyncService service;
  45. std::unique_ptr<Fixture> fixture(new Fixture(&service));
  46. {
  47. EchoResponse send_response;
  48. EchoResponse recv_response;
  49. EchoRequest send_request;
  50. EchoRequest recv_request;
  51. if (msg_size > 0) {
  52. send_request.set_message(std::string(msg_size, 'a'));
  53. send_response.set_message(std::string(msg_size, 'b'));
  54. }
  55. std::unique_ptr<EchoTestService::Stub> stub(
  56. EchoTestService::NewStub(fixture->channel()));
  57. for (auto _ : state) {
  58. ServerContext svr_ctx;
  59. ServerContextMutator svr_ctx_mut(&svr_ctx);
  60. ServerAsyncReaderWriter<EchoResponse, EchoRequest> response_rw(&svr_ctx);
  61. service.RequestBidiStream(&svr_ctx, &response_rw, fixture->cq(),
  62. fixture->cq(), tag(0));
  63. ClientContext cli_ctx;
  64. ClientContextMutator cli_ctx_mut(&cli_ctx);
  65. auto request_rw = stub->AsyncBidiStream(&cli_ctx, fixture->cq(), tag(1));
  66. // Establish async stream between client side and server side
  67. void* t;
  68. bool ok;
  69. int need_tags = (1 << 0) | (1 << 1);
  70. while (need_tags) {
  71. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  72. GPR_ASSERT(ok);
  73. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  74. GPR_ASSERT(need_tags & (1 << i));
  75. need_tags &= ~(1 << i);
  76. }
  77. // Send 'max_ping_pongs' number of ping pong messages
  78. int ping_pong_cnt = 0;
  79. while (ping_pong_cnt < max_ping_pongs) {
  80. request_rw->Write(send_request, tag(0)); // Start client send
  81. response_rw.Read(&recv_request, tag(1)); // Start server recv
  82. request_rw->Read(&recv_response, tag(2)); // Start client recv
  83. need_tags = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3);
  84. while (need_tags) {
  85. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  86. GPR_ASSERT(ok);
  87. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  88. // If server recv is complete, start the server send operation
  89. if (i == 1) {
  90. response_rw.Write(send_response, tag(3));
  91. }
  92. GPR_ASSERT(need_tags & (1 << i));
  93. need_tags &= ~(1 << i);
  94. }
  95. ping_pong_cnt++;
  96. }
  97. request_rw->WritesDone(tag(0));
  98. response_rw.Finish(Status::OK, tag(1));
  99. Status recv_status;
  100. request_rw->Finish(&recv_status, tag(2));
  101. need_tags = (1 << 0) | (1 << 1) | (1 << 2);
  102. while (need_tags) {
  103. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  104. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  105. GPR_ASSERT(need_tags & (1 << i));
  106. need_tags &= ~(1 << i);
  107. }
  108. GPR_ASSERT(recv_status.ok());
  109. }
  110. }
  111. fixture->Finish(state);
  112. fixture.reset();
  113. state.SetBytesProcessed(msg_size * state.iterations() * max_ping_pongs * 2);
  114. }
  115. // Repeatedly sends ping pong messages in a single streaming Bidi call in a loop
  116. // First parmeter (i.e state.range(0)): Message size (in bytes) to use
  117. template <class Fixture, class ClientContextMutator, class ServerContextMutator>
  118. static void BM_StreamingPingPongMsgs(benchmark::State& state) {
  119. const int msg_size = state.range(0);
  120. EchoTestService::AsyncService service;
  121. std::unique_ptr<Fixture> fixture(new Fixture(&service));
  122. {
  123. EchoResponse send_response;
  124. EchoResponse recv_response;
  125. EchoRequest send_request;
  126. EchoRequest recv_request;
  127. if (msg_size > 0) {
  128. send_request.set_message(std::string(msg_size, 'a'));
  129. send_response.set_message(std::string(msg_size, 'b'));
  130. }
  131. std::unique_ptr<EchoTestService::Stub> stub(
  132. EchoTestService::NewStub(fixture->channel()));
  133. ServerContext svr_ctx;
  134. ServerContextMutator svr_ctx_mut(&svr_ctx);
  135. ServerAsyncReaderWriter<EchoResponse, EchoRequest> response_rw(&svr_ctx);
  136. service.RequestBidiStream(&svr_ctx, &response_rw, fixture->cq(),
  137. fixture->cq(), tag(0));
  138. ClientContext cli_ctx;
  139. ClientContextMutator cli_ctx_mut(&cli_ctx);
  140. auto request_rw = stub->AsyncBidiStream(&cli_ctx, fixture->cq(), tag(1));
  141. // Establish async stream between client side and server side
  142. void* t;
  143. bool ok;
  144. int need_tags = (1 << 0) | (1 << 1);
  145. while (need_tags) {
  146. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  147. GPR_ASSERT(ok);
  148. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  149. GPR_ASSERT(need_tags & (1 << i));
  150. need_tags &= ~(1 << i);
  151. }
  152. for (auto _ : state) {
  153. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  154. request_rw->Write(send_request, tag(0)); // Start client send
  155. response_rw.Read(&recv_request, tag(1)); // Start server recv
  156. request_rw->Read(&recv_response, tag(2)); // Start client recv
  157. need_tags = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3);
  158. while (need_tags) {
  159. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  160. GPR_ASSERT(ok);
  161. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  162. // If server recv is complete, start the server send operation
  163. if (i == 1) {
  164. response_rw.Write(send_response, tag(3));
  165. }
  166. GPR_ASSERT(need_tags & (1 << i));
  167. need_tags &= ~(1 << i);
  168. }
  169. }
  170. request_rw->WritesDone(tag(0));
  171. response_rw.Finish(Status::OK, tag(1));
  172. Status recv_status;
  173. request_rw->Finish(&recv_status, tag(2));
  174. need_tags = (1 << 0) | (1 << 1) | (1 << 2);
  175. while (need_tags) {
  176. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  177. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  178. GPR_ASSERT(need_tags & (1 << i));
  179. need_tags &= ~(1 << i);
  180. }
  181. GPR_ASSERT(recv_status.ok());
  182. }
  183. fixture->Finish(state);
  184. fixture.reset();
  185. state.SetBytesProcessed(msg_size * state.iterations() * 2);
  186. }
  187. // Repeatedly makes Streaming Bidi calls (exchanging a configurable number of
  188. // messages in each call) in a loop on a single channel. Different from
  189. // BM_StreamingPingPong we are using stream coalescing api, e.g. WriteLast,
  190. // WriteAndFinish, set_initial_metadata_corked. These apis aim at saving
  191. // sendmsg syscalls for streaming by coalescing 1. initial metadata with first
  192. // message; 2. final streaming message with trailing metadata.
  193. //
  194. // First parmeter (i.e state.range(0)): Message size (in bytes) to use
  195. // Second parameter (i.e state.range(1)): Number of ping pong messages.
  196. // Note: One ping-pong means two messages (one from client to server and
  197. // the other from server to client):
  198. // Third parameter (i.e state.range(2)): Switch between using WriteAndFinish
  199. // API and WriteLast API for server.
  200. template <class Fixture, class ClientContextMutator, class ServerContextMutator>
  201. static void BM_StreamingPingPongWithCoalescingApi(benchmark::State& state) {
  202. const int msg_size = state.range(0);
  203. const int max_ping_pongs = state.range(1);
  204. // This options is used to test out server API: WriteLast and WriteAndFinish
  205. // respectively, since we can not use both of them on server side at the same
  206. // time. Value 1 means we are testing out the WriteAndFinish API, and
  207. // otherwise we are testing out the WriteLast API.
  208. const int write_and_finish = state.range(2);
  209. EchoTestService::AsyncService service;
  210. std::unique_ptr<Fixture> fixture(new Fixture(&service));
  211. {
  212. EchoResponse send_response;
  213. EchoResponse recv_response;
  214. EchoRequest send_request;
  215. EchoRequest recv_request;
  216. if (msg_size > 0) {
  217. send_request.set_message(std::string(msg_size, 'a'));
  218. send_response.set_message(std::string(msg_size, 'b'));
  219. }
  220. std::unique_ptr<EchoTestService::Stub> stub(
  221. EchoTestService::NewStub(fixture->channel()));
  222. for (auto _ : state) {
  223. ServerContext svr_ctx;
  224. ServerContextMutator svr_ctx_mut(&svr_ctx);
  225. ServerAsyncReaderWriter<EchoResponse, EchoRequest> response_rw(&svr_ctx);
  226. service.RequestBidiStream(&svr_ctx, &response_rw, fixture->cq(),
  227. fixture->cq(), tag(0));
  228. ClientContext cli_ctx;
  229. ClientContextMutator cli_ctx_mut(&cli_ctx);
  230. cli_ctx.set_initial_metadata_corked(true);
  231. // tag:1 here will never comes up, since we are not performing any op due
  232. // to initial metadata coalescing.
  233. auto request_rw = stub->AsyncBidiStream(&cli_ctx, fixture->cq(), tag(1));
  234. void* t;
  235. bool ok;
  236. int expect_tags = 0;
  237. // Send 'max_ping_pongs' number of ping pong messages
  238. int ping_pong_cnt = 0;
  239. while (ping_pong_cnt < max_ping_pongs) {
  240. if (ping_pong_cnt == max_ping_pongs - 1) {
  241. request_rw->WriteLast(send_request, WriteOptions(), tag(2));
  242. } else {
  243. request_rw->Write(send_request, tag(2)); // Start client send
  244. }
  245. int await_tags = (1 << 2);
  246. if (ping_pong_cnt == 0) {
  247. // wait for the server call structure (call_hook, etc.) to be
  248. // initialized (async stream between client side and server side
  249. // established). It is necessary when client init metadata is
  250. // coalesced
  251. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  252. while (static_cast<int>(reinterpret_cast<intptr_t>(t)) != 0) {
  253. // In some cases tag:2 comes before tag:0 (write tag comes out
  254. // first), this while loop is to make sure get tag:0.
  255. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  256. GPR_ASSERT(await_tags & (1 << i));
  257. await_tags &= ~(1 << i);
  258. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  259. }
  260. }
  261. response_rw.Read(&recv_request, tag(3)); // Start server recv
  262. request_rw->Read(&recv_response, tag(4)); // Start client recv
  263. await_tags |= (1 << 3) | (1 << 4);
  264. expect_tags = await_tags;
  265. await_tags |= (1 << 5);
  266. while (await_tags != 0) {
  267. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  268. GPR_ASSERT(ok);
  269. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  270. // If server recv is complete, start the server send operation
  271. if (i == 3) {
  272. if (ping_pong_cnt == max_ping_pongs - 1) {
  273. if (write_and_finish == 1) {
  274. response_rw.WriteAndFinish(send_response, WriteOptions(),
  275. Status::OK, tag(5));
  276. expect_tags |= (1 << 5);
  277. } else {
  278. response_rw.WriteLast(send_response, WriteOptions(), tag(5));
  279. // WriteLast buffers the write, so it's possible neither server
  280. // write op nor client read op will finish inside the while
  281. // loop.
  282. await_tags &= ~(1 << 4);
  283. await_tags &= ~(1 << 5);
  284. expect_tags |= (1 << 5);
  285. }
  286. } else {
  287. response_rw.Write(send_response, tag(5));
  288. expect_tags |= (1 << 5);
  289. }
  290. }
  291. GPR_ASSERT(expect_tags & (1 << i));
  292. expect_tags &= ~(1 << i);
  293. await_tags &= ~(1 << i);
  294. }
  295. ping_pong_cnt++;
  296. }
  297. if (max_ping_pongs == 0) {
  298. expect_tags |= (1 << 6) | (1 << 7) | (1 << 8);
  299. } else {
  300. if (write_and_finish == 1) {
  301. expect_tags |= (1 << 8);
  302. } else {
  303. // server's buffered write and the client's read of the buffered write
  304. // tags should come up.
  305. expect_tags |= (1 << 7) | (1 << 8);
  306. }
  307. }
  308. // No message write or initial metadata write happened yet.
  309. if (max_ping_pongs == 0) {
  310. request_rw->WritesDone(tag(6));
  311. // wait for server call data structure(call_hook, etc.) to be
  312. // initialized, since initial metadata is corked.
  313. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  314. while (static_cast<int>(reinterpret_cast<intptr_t>(t)) != 0) {
  315. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  316. GPR_ASSERT(expect_tags & (1 << i));
  317. expect_tags &= ~(1 << i);
  318. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  319. }
  320. response_rw.Finish(Status::OK, tag(7));
  321. } else {
  322. if (write_and_finish != 1) {
  323. response_rw.Finish(Status::OK, tag(7));
  324. }
  325. }
  326. Status recv_status;
  327. request_rw->Finish(&recv_status, tag(8));
  328. while (expect_tags) {
  329. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  330. int i = static_cast<int>(reinterpret_cast<intptr_t>(t));
  331. GPR_ASSERT(expect_tags & (1 << i));
  332. expect_tags &= ~(1 << i);
  333. }
  334. GPR_ASSERT(recv_status.ok());
  335. }
  336. }
  337. fixture->Finish(state);
  338. fixture.reset();
  339. state.SetBytesProcessed(msg_size * state.iterations() * max_ping_pongs * 2);
  340. }
  341. } // namespace testing
  342. } // namespace grpc
  343. #endif // TEST_CPP_MICROBENCHMARKS_FULLSTACK_STREAMING_PING_PONG_H