• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* Benchmark arenas */
20 
21 #include <benchmark/benchmark.h>
22 #include "src/core/lib/gprpp/arena.h"
23 #include "test/core/util/test_config.h"
24 #include "test/cpp/microbenchmarks/helpers.h"
25 #include "test/cpp/util/test_config.h"
26 
27 using grpc_core::Arena;
28 
BM_Arena_NoOp(benchmark::State & state)29 static void BM_Arena_NoOp(benchmark::State& state) {
30   for (auto _ : state) {
31     Arena::Create(state.range(0))->Destroy();
32   }
33 }
34 BENCHMARK(BM_Arena_NoOp)->Range(1, 1024 * 1024);
35 
BM_Arena_ManyAlloc(benchmark::State & state)36 static void BM_Arena_ManyAlloc(benchmark::State& state) {
37   Arena* a = Arena::Create(state.range(0));
38   const size_t realloc_after =
39       1024 * 1024 * 1024 / ((state.range(1) + 15) & 0xffffff0u);
40   while (state.KeepRunning()) {
41     a->Alloc(state.range(1));
42     // periodically recreate arena to avoid OOM
43     if (state.iterations() % realloc_after == 0) {
44       a->Destroy();
45       a = Arena::Create(state.range(0));
46     }
47   }
48   a->Destroy();
49 }
50 BENCHMARK(BM_Arena_ManyAlloc)->Ranges({{1, 1024 * 1024}, {1, 32 * 1024}});
51 
BM_Arena_Batch(benchmark::State & state)52 static void BM_Arena_Batch(benchmark::State& state) {
53   for (auto _ : state) {
54     Arena* a = Arena::Create(state.range(0));
55     for (int i = 0; i < state.range(1); i++) {
56       a->Alloc(state.range(2));
57     }
58     a->Destroy();
59   }
60 }
61 BENCHMARK(BM_Arena_Batch)->Ranges({{1, 64 * 1024}, {1, 64}, {1, 1024}});
62 
63 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
64 // and others do not. This allows us to support both modes.
65 namespace benchmark {
RunTheBenchmarksNamespaced()66 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
67 }  // namespace benchmark
68 
main(int argc,char ** argv)69 int main(int argc, char** argv) {
70   grpc::testing::TestEnvironment env(argc, argv);
71   ::benchmark::Initialize(&argc, argv);
72   ::grpc::testing::InitTest(&argc, &argv, false);
73   benchmark::RunTheBenchmarksNamespaced();
74   return 0;
75 }
76