Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / test / cpp / microbenchmarks / bm_alarm.cc
1 /*
2  *
3  * Copyright 2015 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 /* This benchmark exists to ensure that immediately-firing alarms are fast */
20
21 #include <benchmark/benchmark.h>
22
23 #include <grpc/grpc.h>
24 #include <grpcpp/alarm.h>
25 #include <grpcpp/completion_queue.h>
26 #include <grpcpp/impl/grpc_library.h>
27
28 #include "test/core/util/test_config.h"
29 #include "test/cpp/microbenchmarks/helpers.h"
30 #include "test/cpp/util/test_config.h"
31
32 namespace grpc {
33 namespace testing {
34
35 static void BM_Alarm_Tag_Immediate(benchmark::State& state) {
36   TrackCounters track_counters;
37   CompletionQueue cq;
38   Alarm alarm;
39   void* output_tag;
40   bool ok;
41   auto deadline = grpc_timeout_seconds_to_deadline(0);
42   for (auto _ : state) {
43     alarm.Set(&cq, deadline, nullptr);
44     cq.Next(&output_tag, &ok);
45   }
46   track_counters.Finish(state);
47 }
48 BENCHMARK(BM_Alarm_Tag_Immediate);
49
50 }  // namespace testing
51 }  // namespace grpc
52
53 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
54 // and others do not. This allows us to support both modes.
55 namespace benchmark {
56 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
57 }  // namespace benchmark
58
59 int main(int argc, char** argv) {
60   grpc::testing::TestEnvironment env(argc, argv);
61   LibraryInitializer libInit;
62   ::benchmark::Initialize(&argc, argv);
63   ::grpc::testing::InitTest(&argc, &argv, false);
64   benchmark::RunTheBenchmarksNamespaced();
65   return 0;
66 }