Skip to content

Commit 18546e6

Browse files
committed
reduce code duplication in Benchmark by using Apply() to set arguments
1 parent 7a59bd3 commit 18546e6

File tree

2 files changed

+69
-83
lines changed

2 files changed

+69
-83
lines changed

benchmark/benchmark.cpp

Lines changed: 54 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
*
77
*/
8-
98
#include "benchmark.hpp"
9+
#include <benchmark/benchmark.h>
1010

1111
#define UMF_BENCHMARK_TEMPLATE_DEFINE(BaseClass, Method, ...) \
1212
BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, __VA_ARGS__) \
@@ -18,69 +18,65 @@
1818

1919
#define UMF_BENCHMARK_REGISTER_F(BaseClass, Method) \
2020
BENCHMARK_REGISTER_F(BaseClass, Method) \
21-
->ArgNames( \
22-
BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::argsName()) \
23-
->Name(BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::name()) \
24-
->Iterations( \
25-
BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::iterations())
26-
27-
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, glibc_fix, fixed_alloc_size,
28-
glibc_malloc);
21+
->Apply( \
22+
&BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::defaultArgs)
2923

3024
// Benchmarks scenarios:
3125

3226
// The benchmark arguments specified in Args() are, in order:
3327
// benchmark arguments, allocator arguments, size generator arguments.
3428
// The exact meaning of each argument depends on the benchmark, allocator, and size components used.
3529
// Refer to the 'argsName()' function in each component to find detailed descriptions of these arguments.
30+
31+
static void default_alloc_fix_size(benchmark::internal::Benchmark *benchmark) {
32+
benchmark->Args({10000, 0, 4096});
33+
benchmark->Args({10000, 100000, 4096});
34+
benchmark->Threads(4);
35+
benchmark->Threads(1);
36+
}
37+
38+
static void
39+
default_alloc_uniform_size(benchmark::internal::Benchmark *benchmark) {
40+
benchmark->Args({10000, 0, 8, 64 * 1024, 8});
41+
benchmark->Threads(4);
42+
benchmark->Threads(1);
43+
}
44+
45+
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, glibc_fix, fixed_alloc_size,
46+
glibc_malloc);
47+
3648
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, glibc_fix)
37-
->Args({10000, 0, 4096})
38-
->Args({10000, 100000, 4096})
39-
->Threads(4)
40-
->Threads(1);
49+
->Apply(&default_alloc_fix_size);
4150

4251
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, glibc_uniform,
4352
uniform_alloc_size, glibc_malloc);
4453
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, glibc_uniform)
45-
->Args({10000, 0, 8, 64 * 1024, 8})
46-
->Threads(4)
47-
->Threads(1);
54+
->Apply(&default_alloc_uniform_size);
4855

4956
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, os_provider, fixed_alloc_size,
5057
provider_allocator<os_provider>);
5158
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, os_provider)
52-
->Args({10000, 0, 4096})
53-
->Args({10000, 100000, 4096})
54-
->Threads(4)
55-
->Threads(1);
59+
->Apply(&default_alloc_fix_size);
5660

5761
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, proxy_pool, fixed_alloc_size,
5862
pool_allocator<proxy_pool<os_provider>>);
5963

6064
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, proxy_pool)
61-
->Args({1000, 0, 4096})
62-
->Args({1000, 100000, 4096})
63-
->Threads(4)
64-
->Threads(1);
65+
->Apply(&default_alloc_fix_size);
6566

6667
#ifdef UMF_POOL_DISJOINT_ENABLED
6768
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, disjoint_pool_fix,
6869
fixed_alloc_size,
6970
pool_allocator<disjoint_pool<os_provider>>);
7071
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, disjoint_pool_fix)
71-
->Args({10000, 0, 4096})
72-
->Args({10000, 100000, 4096})
73-
->Threads(4)
74-
->Threads(1);
72+
->Apply(&default_alloc_fix_size);
7573

7674
// TODO: debug why this crashes
7775
/*UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, disjoint_pool_uniform,
7876
uniform_alloc_size,
7977
pool_allocator<disjoint_pool<os_provider>>);
8078
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, disjoint_pool_uniform)
81-
->Args({10000, 0, 8, 64 * 1024, 8})
82-
// ->Threads(4)
83-
->Threads(1);
79+
->Apply(&default_alloc_uniform_size);
8480
*/
8581
#endif
8682

@@ -89,18 +85,13 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, jemalloc_pool_fix,
8985
fixed_alloc_size,
9086
pool_allocator<jemalloc_pool<os_provider>>);
9187
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, jemalloc_pool_fix)
92-
->Args({10000, 0, 4096})
93-
->Args({10000, 100000, 4096})
94-
->Threads(4)
95-
->Threads(1);
88+
->Apply(&default_alloc_fix_size);
9689

9790
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, jemalloc_pool_uniform,
9891
uniform_alloc_size,
9992
pool_allocator<jemalloc_pool<os_provider>>);
10093
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, jemalloc_pool_uniform)
101-
->Args({10000, 0, 8, 64 * 1024, 8})
102-
->Threads(4)
103-
->Threads(1);
94+
->Apply(&default_alloc_uniform_size);
10495

10596
#endif
10697
#ifdef UMF_POOL_SCALABLE_ENABLED
@@ -109,71 +100,67 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, scalable_pool_fix,
109100
pool_allocator<scalable_pool<os_provider>>);
110101

111102
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, scalable_pool_fix)
112-
->Args({10000, 0, 4096})
113-
->Args({10000, 100000, 4096})
114-
->Threads(4)
115-
->Threads(1);
103+
->Apply(&default_alloc_fix_size);
116104

117105
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, scalable_pool_uniform,
118106
uniform_alloc_size,
119107
pool_allocator<scalable_pool<os_provider>>);
120108

121109
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, scalable_pool_uniform)
122-
->Args({10000, 0, 8, 64 * 1024, 8})
123-
->Threads(4)
124-
->Threads(1);
110+
->Apply(&default_alloc_uniform_size);
125111
#endif
126112
// Multiple allocs/free
113+
static void
114+
default_multiple_alloc_fix_size(benchmark::internal::Benchmark *benchmark) {
115+
benchmark->Args({10000, 4096});
116+
benchmark->Threads(4);
117+
benchmark->Threads(1);
118+
}
119+
120+
static void
121+
default_multiple_alloc_uniform_size(benchmark::internal::Benchmark *benchmark) {
122+
benchmark->Args({10000, 0, 8, 64 * 1024, 8});
123+
benchmark->Threads(4);
124+
benchmark->Threads(1);
125+
}
127126

128127
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, glibc_fix,
129128
fixed_alloc_size, glibc_malloc);
130129

131130
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, glibc_fix)
132-
->Args({10000, 4096})
133-
->Threads(4)
134-
->Threads(1);
131+
->Apply(&default_multiple_alloc_fix_size);
135132

136133
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, glibc_uniform,
137134
uniform_alloc_size, glibc_malloc);
138135
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, glibc_uniform)
139-
->Args({10000, 8, 64 * 1024, 8})
140-
->Threads(4)
141-
->Threads(1);
136+
->Apply(&default_multiple_alloc_uniform_size);
142137

143138
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, proxy_pool,
144139
fixed_alloc_size,
145140
pool_allocator<proxy_pool<os_provider>>);
146141

147142
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, proxy_pool)
148-
->Args({10000, 4096})
149-
->Threads(4)
150-
->Threads(1);
143+
->Apply(&default_multiple_alloc_fix_size);
151144

152145
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, os_provider,
153146
fixed_alloc_size,
154147
provider_allocator<os_provider>);
155148
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, os_provider)
156-
->Args({10000, 4096})
157-
->Threads(4)
158-
->Threads(1);
149+
->Apply(&default_multiple_alloc_fix_size);
159150

160151
#ifdef UMF_POOL_DISJOINT_ENABLED
161152
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, disjoint_pool_fix,
162153
fixed_alloc_size,
163154
pool_allocator<disjoint_pool<os_provider>>);
164155
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, disjoint_pool_fix)
165-
->Args({10000, 4096})
166-
->Threads(4)
167-
->Threads(1);
156+
->Apply(&default_multiple_alloc_fix_size);
168157

169158
// TODO: debug why this crashes
170159
/*UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark,
171160
disjoint_pool_uniform, uniform_alloc_size,
172161
pool_allocator<disjoint_pool<os_provider>>);
173162
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, disjoint_pool_uniform)
174-
->Args({10000, 0, 8, 64 * 1024, 8})
175-
->Threads(4)
176-
->Threads(1);
163+
->Apply(&default_multiple_alloc_uniform_size);
177164
*/
178165
#endif
179166

@@ -182,17 +169,13 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, jemalloc_pool_fix,
182169
fixed_alloc_size,
183170
pool_allocator<jemalloc_pool<os_provider>>);
184171
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, jemalloc_pool_fix)
185-
->Args({10000, 4096})
186-
->Threads(4)
187-
->Threads(1);
172+
->Apply(&default_multiple_alloc_fix_size);
188173

189174
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark,
190175
jemalloc_pool_uniform, uniform_alloc_size,
191176
pool_allocator<jemalloc_pool<os_provider>>);
192177
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, jemalloc_pool_uniform)
193-
->Args({1000, 8, 64 * 1024, 8})
194-
->Threads(4)
195-
->Threads(1);
178+
->Apply(&default_multiple_alloc_uniform_size);
196179

197180
#endif
198181

@@ -202,18 +185,14 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, scalable_pool_fix,
202185
pool_allocator<scalable_pool<os_provider>>);
203186

204187
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, scalable_pool_fix)
205-
->Args({10000, 4096})
206-
->Threads(4)
207-
->Threads(1);
188+
->Apply(&default_multiple_alloc_fix_size);
208189

209190
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark,
210191
scalable_pool_uniform, uniform_alloc_size,
211192
pool_allocator<scalable_pool<os_provider>>);
212193

213194
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, scalable_pool_uniform)
214-
->Args({10000, 8, 64 * 1024, 8})
215-
->Threads(4)
216-
->Threads(1);
195+
->Apply(&default_multiple_alloc_uniform_size);
217196

218197
#endif
219198
BENCHMARK_MAIN();

benchmark/benchmark.hpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,15 @@ struct benchmark_interface : public benchmark::Fixture {
162162
return res;
163163
}
164164

165-
static std::string name() { return Allocator::name(); }
166-
static int64_t iterations() { return 10000; }
165+
virtual std::string name() { return Allocator::name(); }
166+
virtual int64_t iterations() { return 10000; }
167+
static void defaultArgs(Benchmark *benchmark) {
168+
auto *bench =
169+
static_cast<benchmark_interface<Size, Allocator> *>(benchmark);
170+
benchmark->ArgNames(bench->argsName())
171+
->Name(bench->name())
172+
->Iterations(bench->iterations());
173+
}
167174
Size alloc_size;
168175
Allocator allocator;
169176
};
@@ -260,15 +267,15 @@ class alloc_benchmark : public benchmark_interface<Size, Alloc> {
260267
}
261268
}
262269

263-
static std::vector<std::string> argsName() {
270+
virtual std::vector<std::string> argsName() {
264271
auto n = benchmark_interface<Size, Alloc>::argsName();
265272
std::vector<std::string> res = {"max_allocs", "pre_allocs"};
266273
res.insert(res.end(), n.begin(), n.end());
267274
return res;
268275
}
269276

270-
static std::string name() { return base::name() + "/alloc"; }
271-
static int64_t iterations() { return 200000; }
277+
virtual std::string name() { return base::name() + "/alloc"; }
278+
virtual int64_t iterations() { return 200000; }
272279

273280
protected:
274281
using base = benchmark_interface<Size, Alloc>;
@@ -346,18 +353,18 @@ class multiple_malloc_free_benchmark : public alloc_benchmark<Size, Alloc> {
346353
}
347354
}
348355

349-
static std::string name() {
356+
virtual std::string name() {
350357
return base::base::name() + "/multiple_malloc_free";
351358
}
352359

353-
static std::vector<std::string> argsName() {
360+
virtual std::vector<std::string> argsName() {
354361
auto n = benchmark_interface<Size, Alloc>::argsName();
355362
std::vector<std::string> res = {"max_allocs"};
356363
res.insert(res.end(), n.begin(), n.end());
357364
return res;
358365
}
359366

360-
static int64_t iterations() { return 2000; }
367+
virtual int64_t iterations() { return 2000; }
361368

362369
std::default_random_engine generator;
363370
distribution dist;

0 commit comments

Comments
 (0)