aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp/microbenchmarks/bm_timer.cc
blob: f5a411251b5597ef7483049090b3a64b093d52de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *
 * Copyright 2017 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <benchmark/benchmark.h>
#include <string.h>
#include <atomic>
#include <vector>

#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include "test/cpp/microbenchmarks/helpers.h"
#include "test/cpp/util/test_config.h"

#include "src/core/lib/iomgr/timer.h"

namespace grpc {
namespace testing {

auto& force_library_initialization = Library::get();

struct TimerClosure {
  grpc_timer timer;
  grpc_closure closure;
};

static void BM_InitCancelTimer(benchmark::State& state) {
  constexpr int kTimerCount = 1024;
  TrackCounters track_counters;
  grpc_core::ExecCtx exec_ctx;
  std::vector<TimerClosure> timer_closures(kTimerCount);
  int i = 0;
  while (state.KeepRunning()) {
    TimerClosure* timer_closure = &timer_closures[i++ % kTimerCount];
    GRPC_CLOSURE_INIT(&timer_closure->closure,
                      [](void* /*args*/, grpc_error* /*err*/) {}, nullptr,
                      grpc_schedule_on_exec_ctx);
    grpc_timer_init(&timer_closure->timer, GRPC_MILLIS_INF_FUTURE,
                    &timer_closure->closure);
    grpc_timer_cancel(&timer_closure->timer);
    exec_ctx.Flush();
  }
  track_counters.Finish(state);
}
BENCHMARK(BM_InitCancelTimer);

static void BM_TimerBatch(benchmark::State& state) {
  constexpr int kTimerCount = 1024;
  const bool check = state.range(0);
  const bool reverse = state.range(1);

  const grpc_millis start =
      reverse ? GRPC_MILLIS_INF_FUTURE : GRPC_MILLIS_INF_FUTURE - kTimerCount;
  const grpc_millis end =
      reverse ? GRPC_MILLIS_INF_FUTURE - kTimerCount : GRPC_MILLIS_INF_FUTURE;
  const grpc_millis increment = reverse ? -1 : 1;

  TrackCounters track_counters;
  grpc_core::ExecCtx exec_ctx;
  std::vector<TimerClosure> timer_closures(kTimerCount);
  while (state.KeepRunning()) {
    for (grpc_millis deadline = start; deadline != end; deadline += increment) {
      TimerClosure* timer_closure = &timer_closures[deadline % kTimerCount];
      GRPC_CLOSURE_INIT(&timer_closure->closure,
                        [](void* /*args*/, grpc_error* /*err*/) {}, nullptr,
                        grpc_schedule_on_exec_ctx);

      grpc_timer_init(&timer_closure->timer, deadline, &timer_closure->closure);
    }
    if (check) {
      grpc_millis next;
      grpc_timer_check(&next);
    }
    for (grpc_millis deadline = start; deadline != end; deadline += increment) {
      TimerClosure* timer_closure = &timer_closures[deadline % kTimerCount];
      grpc_timer_cancel(&timer_closure->timer);
    }
    exec_ctx.Flush();
  }
  track_counters.Finish(state);
}
BENCHMARK(BM_TimerBatch)
    ->Args({/*check=*/false, /*reverse=*/false})
    ->Args({/*check=*/false, /*reverse=*/true})
    ->Args({/*check=*/true, /*reverse=*/false})
    ->Args({/*check=*/true, /*reverse=*/true})
    ->ThreadRange(1, 128);

}  // namespace testing
}  // namespace grpc

// Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
// and others do not. This allows us to support both modes.
namespace benchmark {
void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
}  // namespace benchmark

int main(int argc, char** argv) {
  ::benchmark::Initialize(&argc, argv);
  ::grpc::testing::InitTest(&argc, &argv, false);
  benchmark::RunTheBenchmarksNamespaced();
  return 0;
}