21 #ifndef TEST_CPP_MICROBENCHMARKS_FULLSTACK_STREAMING_PING_PONG_H
22 #define TEST_CPP_MICROBENCHMARKS_FULLSTACK_STREAMING_PING_PONG_H
26 #include <benchmark/benchmark.h>
29 #include "src/proto/grpc/testing/echo.grpc.pb.h"
40 static void*
tag(
intptr_t x) {
return reinterpret_cast<void*
>(
x); }
49 template <
class Fixture,
class ClientContextMutator,
class ServerContextMutator>
51 const int msg_size =
state.range(0);
52 const int max_ping_pongs =
state.range(1);
54 EchoTestService::AsyncService
service;
57 EchoResponse send_response;
58 EchoResponse recv_response;
60 EchoRequest recv_request;
64 send_response.set_message(
std::string(msg_size,
'b'));
67 std::unique_ptr<EchoTestService::Stub>
stub(
68 EchoTestService::NewStub(
fixture->channel()));
72 ServerContextMutator svr_ctx_mut(&svr_ctx);
78 ClientContextMutator cli_ctx_mut(&cli_ctx);
79 auto request_rw =
stub->AsyncBidiStream(&cli_ctx,
fixture->cq(),
tag(1));
84 int need_tags = (1 << 0) | (1 << 1);
88 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
90 need_tags &= ~(1 <<
i);
94 int ping_pong_cnt = 0;
95 while (ping_pong_cnt < max_ping_pongs) {
97 response_rw.
Read(&recv_request,
tag(1));
98 request_rw->Read(&recv_response,
tag(2));
100 need_tags = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3);
104 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
108 response_rw.
Write(send_response,
tag(3));
112 need_tags &= ~(1 <<
i);
118 request_rw->WritesDone(
tag(0));
122 request_rw->Finish(&recv_status,
tag(2));
124 need_tags = (1 << 0) | (1 << 1) | (1 << 2);
127 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
129 need_tags &= ~(1 <<
i);
138 state.SetBytesProcessed(msg_size *
state.iterations() * max_ping_pongs * 2);
143 template <
class Fixture,
class ClientContextMutator,
class ServerContextMutator>
145 const int msg_size =
state.range(0);
147 EchoTestService::AsyncService
service;
150 EchoResponse send_response;
151 EchoResponse recv_response;
153 EchoRequest recv_request;
157 send_response.set_message(
std::string(msg_size,
'b'));
160 std::unique_ptr<EchoTestService::Stub>
stub(
161 EchoTestService::NewStub(
fixture->channel()));
164 ServerContextMutator svr_ctx_mut(&svr_ctx);
170 ClientContextMutator cli_ctx_mut(&cli_ctx);
171 auto request_rw =
stub->AsyncBidiStream(&cli_ctx,
fixture->cq(),
tag(1));
176 int need_tags = (1 << 0) | (1 << 1);
180 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
182 need_tags &= ~(1 <<
i);
188 response_rw.
Read(&recv_request,
tag(1));
189 request_rw->Read(&recv_response,
tag(2));
191 need_tags = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3);
195 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
199 response_rw.
Write(send_response,
tag(3));
203 need_tags &= ~(1 <<
i);
207 request_rw->WritesDone(
tag(0));
210 request_rw->Finish(&recv_status,
tag(2));
212 need_tags = (1 << 0) | (1 << 1) | (1 << 2);
215 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
217 need_tags &= ~(1 <<
i);
225 state.SetBytesProcessed(msg_size *
state.iterations() * 2);
241 template <
class Fixture,
class ClientContextMutator,
class ServerContextMutator>
243 const int msg_size =
state.range(0);
244 const int max_ping_pongs =
state.range(1);
249 const int write_and_finish =
state.range(2);
251 EchoTestService::AsyncService
service;
254 EchoResponse send_response;
255 EchoResponse recv_response;
257 EchoRequest recv_request;
261 send_response.set_message(
std::string(msg_size,
'b'));
264 std::unique_ptr<EchoTestService::Stub>
stub(
265 EchoTestService::NewStub(
fixture->channel()));
269 ServerContextMutator svr_ctx_mut(&svr_ctx);
275 ClientContextMutator cli_ctx_mut(&cli_ctx);
279 auto request_rw =
stub->AsyncBidiStream(&cli_ctx,
fixture->cq(),
tag(1));
286 int ping_pong_cnt = 0;
287 while (ping_pong_cnt < max_ping_pongs) {
288 if (ping_pong_cnt == max_ping_pongs - 1) {
294 int await_tags = (1 << 2);
296 if (ping_pong_cnt == 0) {
302 while (
static_cast<int>(
reinterpret_cast<intptr_t>(t)) != 0) {
305 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
307 await_tags &= ~(1 <<
i);
312 response_rw.
Read(&recv_request,
tag(3));
313 request_rw->Read(&recv_response,
tag(4));
315 await_tags |= (1 << 3) | (1 << 4);
316 expect_tags = await_tags;
317 await_tags |= (1 << 5);
319 while (await_tags != 0) {
322 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
326 if (ping_pong_cnt == max_ping_pongs - 1) {
327 if (write_and_finish == 1) {
330 expect_tags |= (1 << 5);
336 await_tags &= ~(1 << 4);
337 await_tags &= ~(1 << 5);
338 expect_tags |= (1 << 5);
341 response_rw.
Write(send_response,
tag(5));
342 expect_tags |= (1 << 5);
347 expect_tags &= ~(1 <<
i);
348 await_tags &= ~(1 <<
i);
354 if (max_ping_pongs == 0) {
355 expect_tags |= (1 << 6) | (1 << 7) | (1 << 8);
357 if (write_and_finish == 1) {
358 expect_tags |= (1 << 8);
362 expect_tags |= (1 << 7) | (1 << 8);
367 if (max_ping_pongs == 0) {
368 request_rw->WritesDone(
tag(6));
372 while (
static_cast<int>(
reinterpret_cast<intptr_t>(t)) != 0) {
373 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
375 expect_tags &= ~(1 <<
i);
380 if (write_and_finish != 1) {
386 request_rw->Finish(&recv_status,
tag(8));
388 while (expect_tags) {
390 int i =
static_cast<int>(
reinterpret_cast<intptr_t>(t));
392 expect_tags &= ~(1 <<
i);
401 state.SetBytesProcessed(msg_size *
state.iterations() * max_ping_pongs * 2);
406 #endif // TEST_CPP_MICROBENCHMARKS_FULLSTACK_STREAMING_PING_PONG_H