xref: /aosp_15_r20/external/grpc-grpc/test/cpp/microbenchmarks/callback_unary_ping_pong.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2019 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_CALLBACK_UNARY_PING_PONG_H
22 #define GRPC_TEST_CPP_MICROBENCHMARKS_CALLBACK_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/callback_test_service.h"
30 #include "test/cpp/microbenchmarks/fullstack_context_mutators.h"
31 #include "test/cpp/microbenchmarks/fullstack_fixtures.h"
32 
33 namespace grpc {
34 namespace testing {
35 
36 //******************************************************************************
37 // BENCHMARKING KERNELS
38 //
39 
SendCallbackUnaryPingPong(benchmark::State * state,ClientContext * cli_ctx,EchoRequest * request,EchoResponse * response,EchoTestService::Stub * stub_,bool * done,std::mutex * mu,std::condition_variable * cv)40 inline void SendCallbackUnaryPingPong(
41     benchmark::State* state, ClientContext* cli_ctx, EchoRequest* request,
42     EchoResponse* response, EchoTestService::Stub* stub_, bool* done,
43     std::mutex* mu, std::condition_variable* cv) {
44   int response_msgs_size = state->range(1);
45   cli_ctx->AddMetadata(kServerMessageSize, std::to_string(response_msgs_size));
46   stub_->async()->Echo(
47       cli_ctx, request, response,
48       [state, cli_ctx, request, response, stub_, done, mu, cv](Status s) {
49         GPR_ASSERT(s.ok());
50         if (state->KeepRunning()) {
51           cli_ctx->~ClientContext();
52           new (cli_ctx) ClientContext();
53           SendCallbackUnaryPingPong(state, cli_ctx, request, response, stub_,
54                                     done, mu, cv);
55         } else {
56           std::lock_guard<std::mutex> l(*mu);
57           *done = true;
58           cv->notify_one();
59         }
60       });
61 };
62 
63 template <class Fixture, class ClientContextMutator, class ServerContextMutator>
BM_CallbackUnaryPingPong(benchmark::State & state)64 static void BM_CallbackUnaryPingPong(benchmark::State& state) {
65   int request_msgs_size = state.range(0);
66   int response_msgs_size = state.range(1);
67   CallbackStreamingTestService service;
68   std::unique_ptr<Fixture> fixture(new Fixture(&service));
69   std::unique_ptr<EchoTestService::Stub> stub_(
70       EchoTestService::NewStub(fixture->channel()));
71   EchoRequest request;
72   EchoResponse response;
73   ClientContext cli_ctx;
74 
75   if (request_msgs_size > 0) {
76     request.set_message(std::string(request_msgs_size, 'a'));
77   } else {
78     request.set_message("");
79   }
80 
81   std::mutex mu;
82   std::condition_variable cv;
83   bool done = false;
84   if (state.KeepRunning()) {
85     SendCallbackUnaryPingPong(&state, &cli_ctx, &request, &response,
86                               stub_.get(), &done, &mu, &cv);
87   }
88   std::unique_lock<std::mutex> l(mu);
89   while (!done) {
90     cv.wait(l);
91   }
92   fixture.reset();
93   state.SetBytesProcessed(request_msgs_size * state.iterations() +
94                           response_msgs_size * state.iterations());
95 }
96 
97 }  // namespace testing
98 }  // namespace grpc
99 
100 #endif  // GRPC_TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_PING_PONG_H
101