Debian Codegraph @

test/cpp/microbenchmarks/bm_event_engine_run.cc

raw
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 <atomic>
16 #include <cmath>
17 #include <memory>
18 #include <vector>
19
20 #include <benchmark/benchmark.h>
21
22 #include "absl/debugging/leak_check.h"
23 #include "absl/functional/any_invocable.h"
24
25 #include <grpc/event_engine/event_engine.h>
26 #include <grpcpp/impl/grpc_library.h>
27
28 #include "src/core/lib/event_engine/common_closures.h"
29 #include "src/core/lib/event_engine/default_event_engine.h"
30 #include "src/core/lib/gprpp/notification.h"
31 #include "test/core/util/test_config.h"
32 #include "test/cpp/microbenchmarks/helpers.h"
33 #include "test/cpp/util/test_config.h"
34
35 namespace {
36
37 using ::grpc_event_engine::experimental::AnyInvocableClosure;
38 using ::grpc_event_engine::experimental::EventEngine;
39 using ::grpc_event_engine::experimental::GetDefaultEventEngine;
40
41 struct FanoutParameters {
42 int depth;
43 int fanout;
44 int limit;
45 };
46
47 void BM_EventEngine_RunSmallLambda(benchmark::State& state) {
48 auto engine = GetDefaultEventEngine();
49 const int cb_count = state.range(0);
50 std::atomic_int count{0};
51 for (auto _ : state) {
52 state.PauseTiming();
53 grpc_core::Notification signal;
54 auto cb = [&signal, &count, cb_count]() {
55 if (++count == cb_count) signal.Notify();
56 };
57 state.ResumeTiming();
58 for (int i = 0; i < cb_count; i++) {
59 engine->Run(cb);
60 }
61 signal.WaitForNotification();
62 count.store(0);
63 }
64 state.SetItemsProcessed(cb_count * state.iterations());
65 }
66 BENCHMARK(BM_EventEngine_RunSmallLambda)
67 ->Range(100, 4096)
68 ->MeasureProcessCPUTime()
69 ->UseRealTime();
70
71 void BM_EventEngine_RunLargeLambda(benchmark::State& state) {
72 int cb_count = state.range(0);
73 // large lambdas require an extra allocation
74 std::string extra = "12345678";
75 auto engine = GetDefaultEventEngine();
76 std::atomic_int count{0};
77 for (auto _ : state) {
78 state.PauseTiming();
79 grpc_core::Notification signal;
80 auto cb = [&signal, &count, cb_count, extra]() {
81 (void)extra;
82 if (++count == cb_count) signal.Notify();
83 };
84 state.ResumeTiming();
85 for (int i = 0; i < cb_count; i++) {
86 engine->Run(cb);
87 }
88 signal.WaitForNotification();
89 count.store(0);
90 }
91 state.SetItemsProcessed(cb_count * state.iterations());
92 }
93 BENCHMARK(BM_EventEngine_RunLargeLambda)
94 ->Range(100, 4096)
95 ->MeasureProcessCPUTime()
96 ->UseRealTime();
97
98 void BM_EventEngine_RunClosure(benchmark::State& state) {
99 int cb_count = state.range(0);
100 grpc_core::Notification* signal = new grpc_core::Notification();
101 std::atomic_int count{0};
102 // Ignore leaks from this closure. For simplicty, this closure is not deleted
103 // because the closure may still be executing after the event engine is
104 // destroyed. This is because the default posix event engine's thread pool may
105 // get destroyed separately from the event engine.
106 AnyInvocableClosure* closure = absl::IgnoreLeak(
107 new AnyInvocableClosure([signal_holder = &signal, cb_count, &count]() {
108 if (++count == cb_count) {
109 (*signal_holder)->Notify();
110 }
111 }));
112 auto engine = GetDefaultEventEngine();
113 for (auto _ : state) {
114 for (int i = 0; i < cb_count; i++) {
115 engine->Run(closure);
116 }
117 signal->WaitForNotification();
118 state.PauseTiming();
119 delete signal;
120 signal = new grpc_core::Notification();
121 count.store(0);
122 state.ResumeTiming();
123 }
124 delete signal;
125 state.SetItemsProcessed(cb_count * state.iterations());
126 }
127 BENCHMARK(BM_EventEngine_RunClosure)
128 ->Range(100, 4096)
129 ->MeasureProcessCPUTime()
130 ->UseRealTime();
131
132 void FanoutTestArguments(benchmark::internal::Benchmark* b) {
133 // TODO(hork): enable when the engines are fast enough to run these:
134 // ->Args({10000, 1}) // chain of callbacks scheduling callbacks
135 // ->Args({1, 10000}) // flat scheduling of callbacks
136 // ->Args({5, 6}) // depth 5, fans out to 9,330 callbacks
137 // ->Args({2, 100}) // depth 2, fans out 10,101 callbacks
138 // ->Args({4, 10}) // depth 4, fans out to 11,110 callbacks
139 b->Args({1000, 1}) // chain of callbacks scheduling callbacks
140 ->Args({100, 1}) // chain of callbacks scheduling callbacks
141 ->Args({1, 1000}) // flat scheduling of callbacks
142 ->Args({1, 100}) // flat scheduling of callbacks
143 ->Args({2, 70}) // depth 2, fans out 4971
144 ->Args({4, 8}) // depth 4, fans out 4681
145 ->UseRealTime()
146 ->MeasureProcessCPUTime();
147 }
148
149 FanoutParameters GetFanoutParameters(benchmark::State& state) {
150 FanoutParameters params;
151 params.depth = state.range(0);
152 params.fanout = state.range(1);
153 if (params.depth == 1 || params.fanout == 1) {
154 params.limit = std::max(params.depth, params.fanout) + 1;
155 } else {
156 // sum of geometric series
157 params.limit =
158 (1 - std::pow(params.fanout, params.depth + 1)) / (1 - params.fanout);
159 }
160 // sanity checking
161 GPR_ASSERT(params.limit >= params.fanout * params.depth);
162 return params;
163 }
164
165 // EventEngine callback for Lambda FanOut tests
166 //
167 // Note that params are copied each time for 2 reasons: 1) callbacks will
168 // inevitably continue to shut down after the end of the test, so a reference
169 // parameter will become invalid and crash some callbacks, and 2) in my RBE
170 // tests, copies are slightly faster than a shared_ptr<FanoutParams>
171 // alternative.
172 void FanOutCallback(std::shared_ptr<EventEngine> engine,
173 const FanoutParameters params,
174 grpc_core::Notification& signal, std::atomic_int& count,
175 int processing_layer) {
176 int local_cnt = count.fetch_add(1, std::memory_order_acq_rel) + 1;
177 if (local_cnt == params.limit) {
178 signal.Notify();
179 return;
180 }
181 GPR_DEBUG_ASSERT(local_cnt < params.limit);
182 if (params.depth == processing_layer) return;
183 for (int i = 0; i < params.fanout; i++) {
184 engine->Run([engine, params, processing_layer, &count, &signal]() {
185 FanOutCallback(engine, params, signal, count, processing_layer + 1);
186 });
187 }
188 }
189
190 void BM_EventEngine_Lambda_FanOut(benchmark::State& state) {
191 auto params = GetFanoutParameters(state);
192 auto engine = GetDefaultEventEngine();
193 for (auto _ : state) {
194 std::atomic_int count{0};
195 grpc_core::Notification signal;
196 FanOutCallback(engine, params, signal, count, /*processing_layer=*/0);
197 do {
198 signal.WaitForNotification();
199 } while (count.load() != params.limit);
200 }
201 state.SetItemsProcessed(params.limit * state.iterations());
202 }
203 BENCHMARK(BM_EventEngine_Lambda_FanOut)->Apply(FanoutTestArguments);
204
205 void ClosureFanOutCallback(EventEngine::Closure* child_closure,
206 std::shared_ptr<EventEngine> engine,
207 grpc_core::Notification** signal_holder,
208 std::atomic_int& count,
209 const FanoutParameters params) {
210 int local_cnt = count.fetch_add(1, std::memory_order_acq_rel) + 1;
211 if (local_cnt == params.limit) {
212 (*signal_holder)->Notify();
213 return;
214 }
215 if (local_cnt > params.limit) {
216 gpr_log(GPR_ERROR, "Ran too many closures: %d/%d", local_cnt, params.limit);
217 abort();
218 }
219 if (child_closure == nullptr) return;
220 for (int i = 0; i < params.fanout; i++) {
221 engine->Run(child_closure);
222 }
223 }
224
225 void BM_EventEngine_Closure_FanOut(benchmark::State& state) {
226 auto params = GetFanoutParameters(state);
227 auto engine = GetDefaultEventEngine();
228 std::vector<EventEngine::Closure*> closures;
229 closures.reserve(params.depth + 2);
230 closures.push_back(nullptr);
231 grpc_core::Notification* signal = new grpc_core::Notification();
232 std::atomic_int count{0};
233 // prepare a unique closure for each depth
234 for (int i = 0; i <= params.depth; i++) {
235 // call the previous closure (e.g., closures[2] calls closures[1] during
236 // fanout)
237 closures.push_back(new AnyInvocableClosure(
238 [i, engine, &closures, params, signal_holder = &signal, &count]() {
239 ClosureFanOutCallback(closures[i], engine, signal_holder, count,
240 params);
241 }));
242 }
243 for (auto _ : state) {
244 GPR_DEBUG_ASSERT(count.load(std::memory_order_relaxed) == 0);
245 engine->Run(closures[params.depth + 1]);
246 do {
247 signal->WaitForNotification();
248 } while (count.load() != params.limit);
249 // cleanup
250 state.PauseTiming();
251 delete signal;
252 signal = new grpc_core::Notification();
253 count.store(0);
254 state.ResumeTiming();
255 }
256 delete signal;
257 state.SetItemsProcessed(params.limit * state.iterations());
258 for (auto i : closures) delete i;
259 }
260 BENCHMARK(BM_EventEngine_Closure_FanOut)->Apply(FanoutTestArguments);
261
262 } // namespace
263
264 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
265 // and others do not. This allows us to support both modes.
266 namespace benchmark {
267 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
268 } // namespace benchmark
269
270 int main(int argc, char** argv) {
271 grpc::testing::TestEnvironment env(&argc, argv);
272 LibraryInitializer libInit;
273 benchmark::Initialize(&argc, argv);
274 grpc::testing::InitTest(&argc, &argv, false);
275
276 benchmark::RunTheBenchmarksNamespaced();
277 return 0;
278 }