1 /*
2 *
3 * Copyright 2017 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 #include <benchmark/benchmark.h>
20 #include <string.h>
21 #include <atomic>
22 #include <vector>
23
24 #include <grpc/grpc.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27
28 #include "src/core/lib/iomgr/timer.h"
29 #include "test/core/util/test_config.h"
30 #include "test/cpp/microbenchmarks/helpers.h"
31 #include "test/cpp/util/test_config.h"
32
33 namespace grpc {
34 namespace testing {
35
36 struct TimerClosure {
37 grpc_timer timer;
38 grpc_closure closure;
39 };
40
BM_InitCancelTimer(benchmark::State & state)41 static void BM_InitCancelTimer(benchmark::State& state) {
42 constexpr int kTimerCount = 1024;
43 TrackCounters track_counters;
44 grpc_core::ExecCtx exec_ctx;
45 std::vector<TimerClosure> timer_closures(kTimerCount);
46 int i = 0;
47 for (auto _ : state) {
48 TimerClosure* timer_closure = &timer_closures[i++ % kTimerCount];
49 GRPC_CLOSURE_INIT(&timer_closure->closure,
50 [](void* /*args*/, grpc_error* /*err*/) {}, nullptr,
51 grpc_schedule_on_exec_ctx);
52 grpc_timer_init(&timer_closure->timer, GRPC_MILLIS_INF_FUTURE,
53 &timer_closure->closure);
54 grpc_timer_cancel(&timer_closure->timer);
55 exec_ctx.Flush();
56 }
57 track_counters.Finish(state);
58 }
59 BENCHMARK(BM_InitCancelTimer);
60
BM_TimerBatch(benchmark::State & state)61 static void BM_TimerBatch(benchmark::State& state) {
62 constexpr int kTimerCount = 1024;
63 const bool check = state.range(0);
64 const bool reverse = state.range(1);
65
66 const grpc_millis start =
67 reverse ? GRPC_MILLIS_INF_FUTURE : GRPC_MILLIS_INF_FUTURE - kTimerCount;
68 const grpc_millis end =
69 reverse ? GRPC_MILLIS_INF_FUTURE - kTimerCount : GRPC_MILLIS_INF_FUTURE;
70 const grpc_millis increment = reverse ? -1 : 1;
71
72 TrackCounters track_counters;
73 grpc_core::ExecCtx exec_ctx;
74 std::vector<TimerClosure> timer_closures(kTimerCount);
75 for (auto _ : state) {
76 for (grpc_millis deadline = start; deadline != end; deadline += increment) {
77 TimerClosure* timer_closure = &timer_closures[deadline % kTimerCount];
78 GRPC_CLOSURE_INIT(&timer_closure->closure,
79 [](void* /*args*/, grpc_error* /*err*/) {}, nullptr,
80 grpc_schedule_on_exec_ctx);
81
82 grpc_timer_init(&timer_closure->timer, deadline, &timer_closure->closure);
83 }
84 if (check) {
85 grpc_millis next = GRPC_MILLIS_INF_FUTURE;
86 grpc_timer_check(&next);
87 }
88 for (grpc_millis deadline = start; deadline != end; deadline += increment) {
89 TimerClosure* timer_closure = &timer_closures[deadline % kTimerCount];
90 grpc_timer_cancel(&timer_closure->timer);
91 }
92 exec_ctx.Flush();
93 }
94 track_counters.Finish(state);
95 }
96 BENCHMARK(BM_TimerBatch)
97 ->Args({/*check=*/false, /*reverse=*/false})
98 ->Args({/*check=*/false, /*reverse=*/true})
99 ->Args({/*check=*/true, /*reverse=*/false})
100 ->Args({/*check=*/true, /*reverse=*/true})
101 ->ThreadRange(1, 128);
102
103 } // namespace testing
104 } // namespace grpc
105
106 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
107 // and others do not. This allows us to support both modes.
108 namespace benchmark {
RunTheBenchmarksNamespaced()109 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
110 } // namespace benchmark
111
main(int argc,char ** argv)112 int main(int argc, char** argv) {
113 grpc::testing::TestEnvironment env(argc, argv);
114 LibraryInitializer libInit;
115 ::benchmark::Initialize(&argc, argv);
116 ::grpc::testing::InitTest(&argc, &argv, false);
117 benchmark::RunTheBenchmarksNamespaced();
118 return 0;
119 }
120