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