Debian Codegraph @

test/cpp/microbenchmarks/bm_arena.cc

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