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
BM_Arena_NoOp(benchmark::State & state)31 static void BM_Arena_NoOp(benchmark::State& state) {
32 grpc_core::MemoryAllocator memory_allocator =
33 grpc_core::MemoryAllocator(grpc_core::ResourceQuota::Default()
34 ->memory_quota()
35 ->CreateMemoryAllocator("test"));
36 for (auto _ : state) {
37 Arena::Create(state.range(0), &memory_allocator)->Destroy();
38 }
39 }
40 BENCHMARK(BM_Arena_NoOp)->Range(1, 1024 * 1024);
41
BM_Arena_ManyAlloc(benchmark::State & state)42 static void BM_Arena_ManyAlloc(benchmark::State& state) {
43 grpc_core::MemoryAllocator memory_allocator =
44 grpc_core::MemoryAllocator(grpc_core::ResourceQuota::Default()
45 ->memory_quota()
46 ->CreateMemoryAllocator("test"));
47 Arena* a = Arena::Create(state.range(0), &memory_allocator);
48 const size_t realloc_after =
49 1024 * 1024 * 1024 / ((state.range(1) + 15) & 0xffffff0u);
50 while (state.KeepRunning()) {
51 a->Alloc(state.range(1));
52 // periodically recreate arena to avoid OOM
53 if (state.iterations() % realloc_after == 0) {
54 a->Destroy();
55 a = Arena::Create(state.range(0), &memory_allocator);
56 }
57 }
58 a->Destroy();
59 }
60 BENCHMARK(BM_Arena_ManyAlloc)->Ranges({{1, 1024 * 1024}, {1, 32 * 1024}});
61
BM_Arena_Batch(benchmark::State & state)62 static void BM_Arena_Batch(benchmark::State& state) {
63 grpc_core::MemoryAllocator memory_allocator =
64 grpc_core::MemoryAllocator(grpc_core::ResourceQuota::Default()
65 ->memory_quota()
66 ->CreateMemoryAllocator("test"));
67 for (auto _ : state) {
68 Arena* a = Arena::Create(state.range(0), &memory_allocator);
69 for (int i = 0; i < state.range(1); i++) {
70 a->Alloc(state.range(2));
71 }
72 a->Destroy();
73 }
74 }
75 BENCHMARK(BM_Arena_Batch)->Ranges({{1, 64 * 1024}, {1, 64}, {1, 1024}});
76
77 struct TestThingToAllocate {
78 int a;
79 int b;
80 int c;
81 int d;
82 };
83
BM_Arena_MakePooled_Small(benchmark::State & state)84 static void BM_Arena_MakePooled_Small(benchmark::State& state) {
85 grpc_core::MemoryAllocator memory_allocator =
86 grpc_core::MemoryAllocator(grpc_core::ResourceQuota::Default()
87 ->memory_quota()
88 ->CreateMemoryAllocator("test"));
89 Arena* a = Arena::Create(1024, &memory_allocator);
90 for (auto _ : state) {
91 a->MakePooled<TestThingToAllocate>();
92 }
93 a->Destroy();
94 }
95 BENCHMARK(BM_Arena_MakePooled_Small);
96
BM_Arena_MakePooled3_Small(benchmark::State & state)97 static void BM_Arena_MakePooled3_Small(benchmark::State& state) {
98 grpc_core::MemoryAllocator memory_allocator =
99 grpc_core::MemoryAllocator(grpc_core::ResourceQuota::Default()
100 ->memory_quota()
101 ->CreateMemoryAllocator("test"));
102 Arena* a = Arena::Create(1024, &memory_allocator);
103 for (auto _ : state) {
104 auto x = a->MakePooled<TestThingToAllocate>();
105 auto y = a->MakePooled<TestThingToAllocate>();
106 auto z = a->MakePooled<TestThingToAllocate>();
107 }
108 a->Destroy();
109 }
110 BENCHMARK(BM_Arena_MakePooled3_Small);
111
BM_Arena_NewDeleteComparison_Small(benchmark::State & state)112 static void BM_Arena_NewDeleteComparison_Small(benchmark::State& state) {
113 for (auto _ : state) {
114 benchmark::DoNotOptimize(std::make_unique<TestThingToAllocate>());
115 }
116 }
117 BENCHMARK(BM_Arena_NewDeleteComparison_Small);
118
119 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
120 // and others do not. This allows us to support both modes.
121 namespace benchmark {
RunTheBenchmarksNamespaced()122 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
123 } // namespace benchmark
124
main(int argc,char ** argv)125 int main(int argc, char** argv) {
126 grpc::testing::TestEnvironment env(&argc, argv);
127 ::benchmark::Initialize(&argc, argv);
128 grpc::testing::InitTest(&argc, &argv, false);
129 benchmark::RunTheBenchmarksNamespaced();
130 return 0;
131 }
132