benchmark
1.10.0
Toggle main menu visibility
Loading...
Searching...
No Matches
registration.h
1
// Copyright 2015 Google Inc. All rights reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef BENCHMARK_REGISTRATION_H_
16
#define BENCHMARK_REGISTRATION_H_
17
18
#include "benchmark/benchmark_api.h"
19
#include "benchmark/macros.h"
20
21
#if defined(__clang__)
22
#define BENCHMARK_DISABLE_COUNTER_WARNING \
23
_Pragma("GCC diagnostic push") \
24
_Pragma("GCC diagnostic ignored \"-Wunknown-warning-option\"") \
25
_Pragma("GCC diagnostic ignored \"-Wc2y-extensions\"")
26
#define BENCHMARK_RESTORE_COUNTER_WARNING _Pragma("GCC diagnostic pop")
27
#else
28
#define BENCHMARK_DISABLE_COUNTER_WARNING
29
#define BENCHMARK_RESTORE_COUNTER_WARNING
30
#endif
31
32
BENCHMARK_DISABLE_COUNTER_WARNING
33
#if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
34
#define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
35
#else
36
#define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
37
#endif
38
BENCHMARK_RESTORE_COUNTER_WARNING
39
40
#define BENCHMARK_PRIVATE_NAME(...) \
41
BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, \
42
__VA_ARGS__)
43
44
#define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
45
#define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
46
#define BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method) \
47
BaseClass##_##Method##_Benchmark
48
49
#define BENCHMARK_PRIVATE_DECLARE(n) \
50
BENCHMARK_DISABLE_COUNTER_WARNING \
51
static ::benchmark::Benchmark const* const BENCHMARK_PRIVATE_NAME(n) \
52
BENCHMARK_RESTORE_COUNTER_WARNING BENCHMARK_UNUSED
53
54
#define BENCHMARK(...) \
55
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
56
(::benchmark::internal::RegisterBenchmarkInternal( \
57
::benchmark::internal::make_unique< \
58
::benchmark::internal::FunctionBenchmark>( \
59
#__VA_ARGS__, \
60
static_cast<::benchmark::internal::Function*>(__VA_ARGS__))))
61
62
#define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
63
#define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->Args({(a1), (a2)})
64
#define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t))
65
#define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
66
#define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
67
BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
68
69
#define BENCHMARK_CAPTURE(func, test_case_name, ...) \
70
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
71
(::benchmark::internal::RegisterBenchmarkInternal( \
72
::benchmark::internal::make_unique< \
73
::benchmark::internal::FunctionBenchmark>( \
74
#func "/" #test_case_name, \
75
[](::benchmark::State& st) { func(st, __VA_ARGS__); })))
76
77
#define BENCHMARK_NAMED(func, test_case_name) \
78
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
79
(::benchmark::internal::RegisterBenchmarkInternal( \
80
::benchmark::internal::make_unique< \
81
::benchmark::internal::FunctionBenchmark>( \
82
#func "/" #test_case_name, \
83
static_cast<::benchmark::internal::Function*>(func))))
84
85
#define BENCHMARK_TEMPLATE1(n, a) \
86
BENCHMARK_PRIVATE_DECLARE(n) = \
87
(::benchmark::internal::RegisterBenchmarkInternal( \
88
::benchmark::internal::make_unique< \
89
::benchmark::internal::FunctionBenchmark>( \
90
#n "<" #a ">", \
91
static_cast<::benchmark::internal::Function*>(n<a>))))
92
93
#define BENCHMARK_TEMPLATE2(n, a, b) \
94
BENCHMARK_PRIVATE_DECLARE(n) = \
95
(::benchmark::internal::RegisterBenchmarkInternal( \
96
::benchmark::internal::make_unique< \
97
::benchmark::internal::FunctionBenchmark>( \
98
#n "<" #a "," #b ">", \
99
static_cast<::benchmark::internal::Function*>(n<a, b>))))
100
101
#define BENCHMARK_TEMPLATE(n, ...) \
102
BENCHMARK_PRIVATE_DECLARE(n) = \
103
(::benchmark::internal::RegisterBenchmarkInternal( \
104
::benchmark::internal::make_unique< \
105
::benchmark::internal::FunctionBenchmark>( \
106
#n "<" #__VA_ARGS__ ">", \
107
static_cast<::benchmark::internal::Function*>(n<__VA_ARGS__>))))
108
109
#define BENCHMARK_TEMPLATE1_CAPTURE(func, a, test_case_name, ...) \
110
BENCHMARK_CAPTURE(func<a>, test_case_name, __VA_ARGS__)
111
112
#define BENCHMARK_TEMPLATE2_CAPTURE(func, a, b, test_case_name, ...) \
113
BENCHMARK_PRIVATE_DECLARE(func) = \
114
(::benchmark::internal::RegisterBenchmarkInternal( \
115
::benchmark::internal::make_unique< \
116
::benchmark::internal::FunctionBenchmark>( \
117
#func "<" #a "," #b ">" \
118
"/" #test_case_name, \
119
[](::benchmark::State& st) { func<a, b>(st, __VA_ARGS__); })))
120
121
#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
122
class BaseClass##_##Method##_Benchmark : public BaseClass { \
123
public: \
124
BaseClass##_##Method##_Benchmark() { \
125
this->SetName(#BaseClass "/" #Method); \
126
} \
127
\
128
protected: \
129
void BenchmarkCase(::benchmark::State&) override; \
130
};
131
132
#define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
133
class BaseClass##_##Method##_Benchmark : public BaseClass<a> { \
134
public: \
135
BaseClass##_##Method##_Benchmark() { \
136
this->SetName(#BaseClass "<" #a ">/" #Method); \
137
} \
138
\
139
protected: \
140
void BenchmarkCase(::benchmark::State&) override; \
141
};
142
143
#define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
144
class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> { \
145
public: \
146
BaseClass##_##Method##_Benchmark() { \
147
this->SetName(#BaseClass "<" #a "," #b ">/" #Method); \
148
} \
149
\
150
protected: \
151
void BenchmarkCase(::benchmark::State&) override; \
152
};
153
154
#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...) \
155
class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \
156
public: \
157
BaseClass##_##Method##_Benchmark() { \
158
this->SetName(#BaseClass "<" #__VA_ARGS__ ">/" #Method); \
159
} \
160
\
161
protected: \
162
void BenchmarkCase(::benchmark::State&) override; \
163
};
164
165
#define BENCHMARK_DEFINE_F(BaseClass, Method) \
166
BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
167
void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
168
169
#define BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a) \
170
BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
171
void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
172
173
#define BENCHMARK_TEMPLATE2_DEFINE_F(BaseClass, Method, a, b) \
174
BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
175
void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
176
177
#define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, ...) \
178
BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
179
void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
180
181
#define BENCHMARK_REGISTER_F(BaseClass, Method) \
182
BENCHMARK_PRIVATE_REGISTER_F(BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method))
183
184
#define BENCHMARK_PRIVATE_REGISTER_F(TestName) \
185
BENCHMARK_PRIVATE_DECLARE(TestName) = \
186
(::benchmark::internal::RegisterBenchmarkInternal( \
187
::benchmark::internal::make_unique<TestName>()))
188
189
#define BENCHMARK_TEMPLATE_PRIVATE_CONCAT_NAME_F(BaseClass, Method) \
190
BaseClass##_##Method##_BenchmarkTemplate
191
192
#define BENCHMARK_TEMPLATE_METHOD_F(BaseClass, Method) \
193
template <class... Args> \
194
class BENCHMARK_TEMPLATE_PRIVATE_CONCAT_NAME_F(BaseClass, Method) \
195
: public BaseClass<Args...> { \
196
protected: \
197
using Base = BaseClass<Args...>; \
198
void BenchmarkCase(::benchmark::State&) override; \
199
}; \
200
template <class... Args> \
201
void BENCHMARK_TEMPLATE_PRIVATE_CONCAT_NAME_F( \
202
BaseClass, Method)<Args...>::BenchmarkCase
203
204
#define BENCHMARK_TEMPLATE_PRIVATE_INSTANTIATE_F(BaseClass, Method, \
205
UniqueName, ...) \
206
class UniqueName : public BENCHMARK_TEMPLATE_PRIVATE_CONCAT_NAME_F( \
207
BaseClass, Method)<__VA_ARGS__> { \
208
public: \
209
UniqueName() { this->SetName(#BaseClass "<" #__VA_ARGS__ ">/" #Method); } \
210
}; \
211
BENCHMARK_PRIVATE_DECLARE(BaseClass##_##Method##_Benchmark) = \
212
(::benchmark::internal::RegisterBenchmarkInternal( \
213
::benchmark::internal::make_unique<UniqueName>()))
214
215
#define BENCHMARK_TEMPLATE_INSTANTIATE_F(BaseClass, Method, ...) \
216
BENCHMARK_DISABLE_COUNTER_WARNING \
217
BENCHMARK_TEMPLATE_PRIVATE_INSTANTIATE_F( \
218
BaseClass, Method, BENCHMARK_PRIVATE_NAME(BaseClass##Method), \
219
__VA_ARGS__) \
220
BENCHMARK_RESTORE_COUNTER_WARNING
221
222
#define BENCHMARK_F(BaseClass, Method) \
223
BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
224
BENCHMARK_REGISTER_F(BaseClass, Method); \
225
void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
226
227
#define BENCHMARK_TEMPLATE1_F(BaseClass, Method, a) \
228
BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
229
BENCHMARK_REGISTER_F(BaseClass, Method); \
230
void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
231
232
#define BENCHMARK_TEMPLATE2_F(BaseClass, Method, a, b) \
233
BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
234
BENCHMARK_REGISTER_F(BaseClass, Method); \
235
void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
236
237
#define BENCHMARK_TEMPLATE_F(BaseClass, Method, ...) \
238
BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
239
void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
240
241
#define BENCHMARK_MAIN() \
242
int main(int argc, char** argv) { \
243
benchmark::MaybeReenterWithoutASLR(argc, argv); \
244
char arg0_default[] = "benchmark"; \
245
char* args_default = reinterpret_cast<char*>(arg0_default); \
246
if (!argv) { \
247
argc = 1; \
248
argv = &args_default; \
249
} \
250
::benchmark::Initialize(&argc, argv); \
251
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \
252
::benchmark::RunSpecifiedBenchmarks(); \
253
::benchmark::Shutdown(); \
254
return 0; \
255
} \
256
int main(int, char**)
257
258
#endif
// BENCHMARK_REGISTRATION_H_
include
benchmark
registration.h
Generated by
1.18.0