f5a411251b5597ef7483049090b3a64b093d52de
[platform/upstream/grpc.git] / test / cpp / microbenchmarks / bm_timer.cc
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 #include "test/cpp/microbenchmarks/helpers.h"
28 #include "test/cpp/util/test_config.h"
29
30 #include "src/core/lib/iomgr/timer.h"
31
32 namespace grpc {
33 namespace testing {
34
35 auto& force_library_initialization = Library::get();
36
37 struct TimerClosure {
38   grpc_timer timer;
39   grpc_closure closure;
40 };
41
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);
47   int i = 0;
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);
56     exec_ctx.Flush();
57   }
58   track_counters.Finish(state);
59 }
60 BENCHMARK(BM_InitCancelTimer);
61
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);
66
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;
72
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);
82
83       grpc_timer_init(&timer_closure->timer, deadline, &timer_closure->closure);
84     }
85     if (check) {
86       grpc_millis next;
87       grpc_timer_check(&next);
88     }
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);
92     }
93     exec_ctx.Flush();
94   }
95   track_counters.Finish(state);
96 }
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);
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 {
110 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
111 }  // namespace benchmark
112
113 int main(int argc, char** argv) {
114   ::benchmark::Initialize(&argc, argv);
115   ::grpc::testing::InitTest(&argc, &argv, false);
116   benchmark::RunTheBenchmarksNamespaced();
117   return 0;
118 }