Skip to content

Commit 40d0ebe

Browse files
Merge pull request #1042 from lplewa/benchmark_apply
reduce code duplication in Benchmark by using Apply() to set arguments
2 parents 7dc640c + 330ffd3 commit 40d0ebe

File tree

2 files changed

+72
-84
lines changed

2 files changed

+72
-84
lines changed

benchmark/benchmark.cpp

Lines changed: 56 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/*
2-
* Copyright (C) 2024 Intel Corporation
2+
* Copyright (C) 2024-2025 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
*
77
*/
88

9+
#include <benchmark/benchmark.h>
10+
911
#include "benchmark.hpp"
1012

1113
#define UMF_BENCHMARK_TEMPLATE_DEFINE(BaseClass, Method, ...) \
@@ -18,69 +20,65 @@
1820

1921
#define UMF_BENCHMARK_REGISTER_F(BaseClass, Method) \
2022
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);
23+
->Apply( \
24+
&BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::defaultArgs)
2925

3026
// Benchmarks scenarios:
3127

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

4253
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, glibc_uniform,
4354
uniform_alloc_size, glibc_malloc);
4455
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, glibc_uniform)
45-
->Args({10000, 0, 8, 64 * 1024, 8})
46-
->Threads(4)
47-
->Threads(1);
56+
->Apply(&default_alloc_uniform_size);
4857

4958
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, os_provider, fixed_alloc_size,
5059
provider_allocator<os_provider>);
5160
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, os_provider)
52-
->Args({10000, 0, 4096})
53-
->Args({10000, 100000, 4096})
54-
->Threads(4)
55-
->Threads(1);
61+
->Apply(&default_alloc_fix_size);
5662

5763
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, proxy_pool, fixed_alloc_size,
5864
pool_allocator<proxy_pool<os_provider>>);
5965

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

6669
#ifdef UMF_POOL_DISJOINT_ENABLED
6770
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, disjoint_pool_fix,
6871
fixed_alloc_size,
6972
pool_allocator<disjoint_pool<os_provider>>);
7073
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);
74+
->Apply(&default_alloc_fix_size);
7575

7676
// TODO: debug why this crashes
7777
/*UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, disjoint_pool_uniform,
7878
uniform_alloc_size,
7979
pool_allocator<disjoint_pool<os_provider>>);
8080
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, disjoint_pool_uniform)
81-
->Args({10000, 0, 8, 64 * 1024, 8})
82-
// ->Threads(4)
83-
->Threads(1);
81+
->Apply(&default_alloc_uniform_size);
8482
*/
8583
#endif
8684

@@ -89,18 +87,13 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, jemalloc_pool_fix,
8987
fixed_alloc_size,
9088
pool_allocator<jemalloc_pool<os_provider>>);
9189
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);
90+
->Apply(&default_alloc_fix_size);
9691

9792
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, jemalloc_pool_uniform,
9893
uniform_alloc_size,
9994
pool_allocator<jemalloc_pool<os_provider>>);
10095
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, jemalloc_pool_uniform)
101-
->Args({10000, 0, 8, 64 * 1024, 8})
102-
->Threads(4)
103-
->Threads(1);
96+
->Apply(&default_alloc_uniform_size);
10497

10598
#endif
10699
#ifdef UMF_POOL_SCALABLE_ENABLED
@@ -109,71 +102,67 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, scalable_pool_fix,
109102
pool_allocator<scalable_pool<os_provider>>);
110103

111104
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);
105+
->Apply(&default_alloc_fix_size);
116106

117107
UMF_BENCHMARK_TEMPLATE_DEFINE(alloc_benchmark, scalable_pool_uniform,
118108
uniform_alloc_size,
119109
pool_allocator<scalable_pool<os_provider>>);
120110

121111
UMF_BENCHMARK_REGISTER_F(alloc_benchmark, scalable_pool_uniform)
122-
->Args({10000, 0, 8, 64 * 1024, 8})
123-
->Threads(4)
124-
->Threads(1);
112+
->Apply(&default_alloc_uniform_size);
125113
#endif
126114
// Multiple allocs/free
115+
static void
116+
default_multiple_alloc_fix_size(benchmark::internal::Benchmark *benchmark) {
117+
benchmark->Args({10000, 4096});
118+
benchmark->Threads(4);
119+
benchmark->Threads(1);
120+
}
121+
122+
static void
123+
default_multiple_alloc_uniform_size(benchmark::internal::Benchmark *benchmark) {
124+
benchmark->Args({10000, 8, 64 * 1024, 8});
125+
benchmark->Threads(4);
126+
benchmark->Threads(1);
127+
}
127128

128129
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, glibc_fix,
129130
fixed_alloc_size, glibc_malloc);
130131

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

136135
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, glibc_uniform,
137136
uniform_alloc_size, glibc_malloc);
138137
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, glibc_uniform)
139-
->Args({10000, 8, 64 * 1024, 8})
140-
->Threads(4)
141-
->Threads(1);
138+
->Apply(&default_multiple_alloc_uniform_size);
142139

143140
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, proxy_pool,
144141
fixed_alloc_size,
145142
pool_allocator<proxy_pool<os_provider>>);
146143

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

152147
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, os_provider,
153148
fixed_alloc_size,
154149
provider_allocator<os_provider>);
155150
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, os_provider)
156-
->Args({10000, 4096})
157-
->Threads(4)
158-
->Threads(1);
151+
->Apply(&default_multiple_alloc_fix_size);
159152

160153
#ifdef UMF_POOL_DISJOINT_ENABLED
161154
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, disjoint_pool_fix,
162155
fixed_alloc_size,
163156
pool_allocator<disjoint_pool<os_provider>>);
164157
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, disjoint_pool_fix)
165-
->Args({10000, 4096})
166-
->Threads(4)
167-
->Threads(1);
158+
->Apply(&default_multiple_alloc_fix_size);
168159

169160
// TODO: debug why this crashes
170161
/*UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark,
171162
disjoint_pool_uniform, uniform_alloc_size,
172163
pool_allocator<disjoint_pool<os_provider>>);
173164
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);
165+
->Apply(&default_multiple_alloc_uniform_size);
177166
*/
178167
#endif
179168

@@ -182,17 +171,13 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, jemalloc_pool_fix,
182171
fixed_alloc_size,
183172
pool_allocator<jemalloc_pool<os_provider>>);
184173
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, jemalloc_pool_fix)
185-
->Args({10000, 4096})
186-
->Threads(4)
187-
->Threads(1);
174+
->Apply(&default_multiple_alloc_fix_size);
188175

189176
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark,
190177
jemalloc_pool_uniform, uniform_alloc_size,
191178
pool_allocator<jemalloc_pool<os_provider>>);
192179
UMF_BENCHMARK_REGISTER_F(multiple_malloc_free_benchmark, jemalloc_pool_uniform)
193-
->Args({1000, 8, 64 * 1024, 8})
194-
->Threads(4)
195-
->Threads(1);
180+
->Apply(&default_multiple_alloc_uniform_size);
196181

197182
#endif
198183

@@ -202,18 +187,14 @@ UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark, scalable_pool_fix,
202187
pool_allocator<scalable_pool<os_provider>>);
203188

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

209192
UMF_BENCHMARK_TEMPLATE_DEFINE(multiple_malloc_free_benchmark,
210193
scalable_pool_uniform, uniform_alloc_size,
211194
pool_allocator<scalable_pool<os_provider>>);
212195

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

218199
#endif
219200
BENCHMARK_MAIN();

benchmark/benchmark.hpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Intel Corporation
2+
* Copyright (C) 2024-2025 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -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)