3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <benchmark/benchmark.h>
24 #include <grpc/grpc.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include "test/cpp/microbenchmarks/helpers.h"
28 #include "test/cpp/util/test_config.h"
30 #include "src/core/lib/iomgr/timer.h"
35 auto& force_library_initialization = Library::get();
42 static void BM_InitCancelTimer(benchmark::State& state) {
43 constexpr int kTimerCount = 1024;
44 TrackCounters track_counters;
45 grpc_core::ExecCtx exec_ctx;
46 std::vector<TimerClosure> timer_closures(kTimerCount);
48 while (state.KeepRunning()) {
49 TimerClosure* timer_closure = &timer_closures[i++ % kTimerCount];
50 GRPC_CLOSURE_INIT(&timer_closure->closure,
51 [](void* /*args*/, grpc_error* /*err*/) {}, nullptr,
52 grpc_schedule_on_exec_ctx);
53 grpc_timer_init(&timer_closure->timer, GRPC_MILLIS_INF_FUTURE,
54 &timer_closure->closure);
55 grpc_timer_cancel(&timer_closure->timer);
58 track_counters.Finish(state);
60 BENCHMARK(BM_InitCancelTimer);
62 static void BM_TimerBatch(benchmark::State& state) {
63 constexpr int kTimerCount = 1024;
64 const bool check = state.range(0);
65 const bool reverse = state.range(1);
67 const grpc_millis start =
68 reverse ? GRPC_MILLIS_INF_FUTURE : GRPC_MILLIS_INF_FUTURE - kTimerCount;
69 const grpc_millis end =
70 reverse ? GRPC_MILLIS_INF_FUTURE - kTimerCount : GRPC_MILLIS_INF_FUTURE;
71 const grpc_millis increment = reverse ? -1 : 1;
73 TrackCounters track_counters;
74 grpc_core::ExecCtx exec_ctx;
75 std::vector<TimerClosure> timer_closures(kTimerCount);
76 while (state.KeepRunning()) {
77 for (grpc_millis deadline = start; deadline != end; deadline += increment) {
78 TimerClosure* timer_closure = &timer_closures[deadline % kTimerCount];
79 GRPC_CLOSURE_INIT(&timer_closure->closure,
80 [](void* /*args*/, grpc_error* /*err*/) {}, nullptr,
81 grpc_schedule_on_exec_ctx);
83 grpc_timer_init(&timer_closure->timer, deadline, &timer_closure->closure);
87 grpc_timer_check(&next);
89 for (grpc_millis deadline = start; deadline != end; deadline += increment) {
90 TimerClosure* timer_closure = &timer_closures[deadline % kTimerCount];
91 grpc_timer_cancel(&timer_closure->timer);
95 track_counters.Finish(state);
97 BENCHMARK(BM_TimerBatch)
98 ->Args({/*check=*/false, /*reverse=*/false})
99 ->Args({/*check=*/false, /*reverse=*/true})
100 ->Args({/*check=*/true, /*reverse=*/false})
101 ->Args({/*check=*/true, /*reverse=*/true})
102 ->ThreadRange(1, 128);
104 } // namespace testing
107 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
108 // and others do not. This allows us to support both modes.
109 namespace benchmark {
110 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
111 } // namespace benchmark
113 int main(int argc, char** argv) {
114 ::benchmark::Initialize(&argc, argv);
115 ::grpc::testing::InitTest(&argc, &argv, false);
116 benchmark::RunTheBenchmarksNamespaced();