xref: /aosp_15_r20/external/grpc-grpc/test/cpp/microbenchmarks/bm_fullstack_streaming_ping_pong.cc (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 #include "test/core/util/build.h"
22 #include "test/core/util/test_config.h"
23 #include "test/cpp/microbenchmarks/fullstack_streaming_ping_pong.h"
24 #include "test/cpp/util/test_config.h"
25 
26 namespace grpc {
27 namespace testing {
28 
29 //******************************************************************************
30 // CONFIGURATIONS
31 //
32 
__anone616d2370102null33 static const int kMaxMessageSize = [] {
34   if (BuiltUnderMsan() || BuiltUnderTsan() || BuiltUnderUbsan()) {
35     // Scale down sizes for intensive benchmarks to avoid timeouts.
36     return 8 * 1024 * 1024;
37   }
38   return 128 * 1024 * 1024;
39 }();
40 
41 // Generate Args for StreamingPingPong benchmarks. Currently generates args for
42 // only "small streams" (i.e streams with 0, 1 or 2 messages)
StreamingPingPongArgs(benchmark::internal::Benchmark * b)43 static void StreamingPingPongArgs(benchmark::internal::Benchmark* b) {
44   int msg_size = 0;
45 
46   b->Args({0, 0});  // spl case: 0 ping-pong msgs (msg_size doesn't matter here)
47 
48   for (msg_size = 0; msg_size <= kMaxMessageSize;
49        msg_size == 0 ? msg_size++ : msg_size *= 8) {
50     b->Args({msg_size, 1});
51     b->Args({msg_size, 2});
52   }
53 }
54 
55 BENCHMARK_TEMPLATE(BM_StreamingPingPong, TCP, NoOpMutator, NoOpMutator)
56     ->Apply(StreamingPingPongArgs);
57 BENCHMARK_TEMPLATE(BM_StreamingPingPong, InProcess, NoOpMutator, NoOpMutator)
58     ->Apply(StreamingPingPongArgs);
59 
60 BENCHMARK_TEMPLATE(BM_StreamingPingPongMsgs, TCP, NoOpMutator, NoOpMutator)
61     ->Range(0, kMaxMessageSize);
62 BENCHMARK_TEMPLATE(BM_StreamingPingPongMsgs, InProcess, NoOpMutator,
63                    NoOpMutator)
64     ->Range(0, kMaxMessageSize);
65 
66 BENCHMARK_TEMPLATE(BM_StreamingPingPong, MinTCP, NoOpMutator, NoOpMutator)
67     ->Apply(StreamingPingPongArgs);
68 BENCHMARK_TEMPLATE(BM_StreamingPingPong, MinInProcess, NoOpMutator, NoOpMutator)
69     ->Apply(StreamingPingPongArgs);
70 
71 BENCHMARK_TEMPLATE(BM_StreamingPingPongMsgs, MinTCP, NoOpMutator, NoOpMutator)
72     ->Range(0, kMaxMessageSize);
73 BENCHMARK_TEMPLATE(BM_StreamingPingPongMsgs, MinInProcess, NoOpMutator,
74                    NoOpMutator)
75     ->Range(0, kMaxMessageSize);
76 
77 // Generate Args for StreamingPingPongWithCoalescingApi benchmarks. Currently
78 // generates args for only "small streams" (i.e streams with 0, 1 or 2 messages)
StreamingPingPongWithCoalescingApiArgs(benchmark::internal::Benchmark * b)79 static void StreamingPingPongWithCoalescingApiArgs(
80     benchmark::internal::Benchmark* b) {
81   int msg_size = 0;
82 
83   b->Args(
84       {0, 0, 0});  // spl case: 0 ping-pong msgs (msg_size doesn't matter here)
85   b->Args(
86       {0, 0, 1});  // spl case: 0 ping-pong msgs (msg_size doesn't matter here)
87 
88   for (msg_size = 0; msg_size <= kMaxMessageSize;
89        msg_size == 0 ? msg_size++ : msg_size *= 8) {
90     b->Args({msg_size, 1, 0});
91     b->Args({msg_size, 2, 0});
92     b->Args({msg_size, 1, 1});
93     b->Args({msg_size, 2, 1});
94   }
95 }
96 
97 BENCHMARK_TEMPLATE(BM_StreamingPingPongWithCoalescingApi, InProcess,
98                    NoOpMutator, NoOpMutator)
99     ->Apply(StreamingPingPongWithCoalescingApiArgs);
100 BENCHMARK_TEMPLATE(BM_StreamingPingPongWithCoalescingApi, MinInProcess,
101                    NoOpMutator, NoOpMutator)
102     ->Apply(StreamingPingPongWithCoalescingApiArgs);
103 
104 }  // namespace testing
105 }  // namespace grpc
106 
107 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
108 // and others do not. This allows us to support both modes.
109 namespace benchmark {
RunTheBenchmarksNamespaced()110 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
111 }  // namespace benchmark
112 
main(int argc,char ** argv)113 int main(int argc, char** argv) {
114   grpc::testing::TestEnvironment env(&argc, argv);
115   LibraryInitializer libInit;
116   ::benchmark::Initialize(&argc, argv);
117   grpc::testing::InitTest(&argc, &argv, false);
118   benchmark::RunTheBenchmarksNamespaced();
119   return 0;
120 }
121