• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <benchmark/benchmark.h>
16 #include <grpcpp/impl/grpc_library.h>
17 
18 #include <atomic>
19 #include <memory>
20 
21 #include "src/core/lib/iomgr/exec_ctx.h"
22 #include "src/core/util/notification.h"
23 #include "test/core/test_util/test_config.h"
24 #include "test/cpp/microbenchmarks/helpers.h"
25 #include "test/cpp/util/test_config.h"
26 
27 namespace {
NoOpCb(void *,grpc_error_handle)28 void NoOpCb(void* /* arg */, grpc_error_handle /* error */) {}
29 
BM_ExecCtx_Run(benchmark::State & state)30 void BM_ExecCtx_Run(benchmark::State& state) {
31   int cb_count = state.range(0);
32   grpc_closure cb;
33   GRPC_CLOSURE_INIT(&cb, NoOpCb, nullptr, nullptr);
34   grpc_core::ExecCtx exec_ctx;
35   for (auto _ : state) {
36     for (int i = 0; i < cb_count; i++) {
37       exec_ctx.Run(DEBUG_LOCATION, &cb, absl::OkStatus());
38       exec_ctx.Flush();
39     }
40   }
41   state.SetItemsProcessed(cb_count * state.iterations());
42 }
43 BENCHMARK(BM_ExecCtx_Run)
44     ->Range(100, 10000)
45     ->MeasureProcessCPUTime()
46     ->UseRealTime();
47 
48 struct CountingCbData {
49   std::atomic_int cnt{0};
50   grpc_core::Notification* signal;
51   int limit;
52 };
53 
CountingCb(void * arg,grpc_error_handle)54 void CountingCb(void* arg, grpc_error_handle) {
55   auto* data = static_cast<CountingCbData*>(arg);
56   if (++(data->cnt) == data->limit) data->signal->Notify();
57 }
58 
BM_ExecCtx_RunCounted(benchmark::State & state)59 void BM_ExecCtx_RunCounted(benchmark::State& state) {
60   // A more fair comparison with EventEngine::Run, which must wait for all
61   // executions to finish
62   int cb_count = state.range(0);
63   CountingCbData data;
64   data.limit = cb_count;
65   data.signal = new grpc_core::Notification();
66   grpc_closure cb;
67   GRPC_CLOSURE_INIT(&cb, CountingCb, &data, nullptr);
68   grpc_core::ExecCtx exec_ctx;
69   for (auto _ : state) {
70     for (int i = 0; i < cb_count; i++) {
71       exec_ctx.Run(DEBUG_LOCATION, &cb, absl::OkStatus());
72       exec_ctx.Flush();
73     }
74     data.signal->WaitForNotification();
75     state.PauseTiming();
76     delete data.signal;
77     data.signal = new grpc_core::Notification();
78     data.cnt = 0;
79     state.ResumeTiming();
80   }
81   delete data.signal;
82   state.SetItemsProcessed(cb_count * state.iterations());
83 }
84 BENCHMARK(BM_ExecCtx_RunCounted)
85     ->Range(100, 10000)
86     ->MeasureProcessCPUTime()
87     ->UseRealTime();
88 }  // namespace
89 
90 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
91 // and others do not. This allows us to support both modes.
92 namespace benchmark {
RunTheBenchmarksNamespaced()93 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
94 }  // namespace benchmark
95 
main(int argc,char ** argv)96 int main(int argc, char** argv) {
97   grpc::testing::TestEnvironment env(&argc, argv);
98   LibraryInitializer libInit;
99   benchmark::Initialize(&argc, argv);
100   grpc::testing::InitTest(&argc, &argv, false);
101 
102   benchmark::RunTheBenchmarksNamespaced();
103   return 0;
104 }
105