1*58b9f456SAndroid Build Coastguard Worker // Copyright 2015 Google Inc. All rights reserved.
2*58b9f456SAndroid Build Coastguard Worker //
3*58b9f456SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*58b9f456SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*58b9f456SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*58b9f456SAndroid Build Coastguard Worker //
7*58b9f456SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*58b9f456SAndroid Build Coastguard Worker //
9*58b9f456SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*58b9f456SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*58b9f456SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*58b9f456SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*58b9f456SAndroid Build Coastguard Worker // limitations under the License.
14*58b9f456SAndroid Build Coastguard Worker
15*58b9f456SAndroid Build Coastguard Worker // Support for registering benchmarks for functions.
16*58b9f456SAndroid Build Coastguard Worker
17*58b9f456SAndroid Build Coastguard Worker /* Example usage:
18*58b9f456SAndroid Build Coastguard Worker // Define a function that executes the code to be measured a
19*58b9f456SAndroid Build Coastguard Worker // specified number of times:
20*58b9f456SAndroid Build Coastguard Worker static void BM_StringCreation(benchmark::State& state) {
21*58b9f456SAndroid Build Coastguard Worker for (auto _ : state)
22*58b9f456SAndroid Build Coastguard Worker std::string empty_string;
23*58b9f456SAndroid Build Coastguard Worker }
24*58b9f456SAndroid Build Coastguard Worker
25*58b9f456SAndroid Build Coastguard Worker // Register the function as a benchmark
26*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_StringCreation);
27*58b9f456SAndroid Build Coastguard Worker
28*58b9f456SAndroid Build Coastguard Worker // Define another benchmark
29*58b9f456SAndroid Build Coastguard Worker static void BM_StringCopy(benchmark::State& state) {
30*58b9f456SAndroid Build Coastguard Worker std::string x = "hello";
31*58b9f456SAndroid Build Coastguard Worker for (auto _ : state)
32*58b9f456SAndroid Build Coastguard Worker std::string copy(x);
33*58b9f456SAndroid Build Coastguard Worker }
34*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_StringCopy);
35*58b9f456SAndroid Build Coastguard Worker
36*58b9f456SAndroid Build Coastguard Worker // Augment the main() program to invoke benchmarks if specified
37*58b9f456SAndroid Build Coastguard Worker // via the --benchmarks command line flag. E.g.,
38*58b9f456SAndroid Build Coastguard Worker // my_unittest --benchmark_filter=all
39*58b9f456SAndroid Build Coastguard Worker // my_unittest --benchmark_filter=BM_StringCreation
40*58b9f456SAndroid Build Coastguard Worker // my_unittest --benchmark_filter=String
41*58b9f456SAndroid Build Coastguard Worker // my_unittest --benchmark_filter='Copy|Creation'
42*58b9f456SAndroid Build Coastguard Worker int main(int argc, char** argv) {
43*58b9f456SAndroid Build Coastguard Worker benchmark::Initialize(&argc, argv);
44*58b9f456SAndroid Build Coastguard Worker benchmark::RunSpecifiedBenchmarks();
45*58b9f456SAndroid Build Coastguard Worker return 0;
46*58b9f456SAndroid Build Coastguard Worker }
47*58b9f456SAndroid Build Coastguard Worker
48*58b9f456SAndroid Build Coastguard Worker // Sometimes a family of microbenchmarks can be implemented with
49*58b9f456SAndroid Build Coastguard Worker // just one routine that takes an extra argument to specify which
50*58b9f456SAndroid Build Coastguard Worker // one of the family of benchmarks to run. For example, the following
51*58b9f456SAndroid Build Coastguard Worker // code defines a family of microbenchmarks for measuring the speed
52*58b9f456SAndroid Build Coastguard Worker // of memcpy() calls of different lengths:
53*58b9f456SAndroid Build Coastguard Worker
54*58b9f456SAndroid Build Coastguard Worker static void BM_memcpy(benchmark::State& state) {
55*58b9f456SAndroid Build Coastguard Worker char* src = new char[state.range(0)]; char* dst = new char[state.range(0)];
56*58b9f456SAndroid Build Coastguard Worker memset(src, 'x', state.range(0));
57*58b9f456SAndroid Build Coastguard Worker for (auto _ : state)
58*58b9f456SAndroid Build Coastguard Worker memcpy(dst, src, state.range(0));
59*58b9f456SAndroid Build Coastguard Worker state.SetBytesProcessed(int64_t(state.iterations()) *
60*58b9f456SAndroid Build Coastguard Worker int64_t(state.range(0)));
61*58b9f456SAndroid Build Coastguard Worker delete[] src; delete[] dst;
62*58b9f456SAndroid Build Coastguard Worker }
63*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10);
64*58b9f456SAndroid Build Coastguard Worker
65*58b9f456SAndroid Build Coastguard Worker // The preceding code is quite repetitive, and can be replaced with the
66*58b9f456SAndroid Build Coastguard Worker // following short-hand. The following invocation will pick a few
67*58b9f456SAndroid Build Coastguard Worker // appropriate arguments in the specified range and will generate a
68*58b9f456SAndroid Build Coastguard Worker // microbenchmark for each such argument.
69*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_memcpy)->Range(8, 8<<10);
70*58b9f456SAndroid Build Coastguard Worker
71*58b9f456SAndroid Build Coastguard Worker // You might have a microbenchmark that depends on two inputs. For
72*58b9f456SAndroid Build Coastguard Worker // example, the following code defines a family of microbenchmarks for
73*58b9f456SAndroid Build Coastguard Worker // measuring the speed of set insertion.
74*58b9f456SAndroid Build Coastguard Worker static void BM_SetInsert(benchmark::State& state) {
75*58b9f456SAndroid Build Coastguard Worker set<int> data;
76*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
77*58b9f456SAndroid Build Coastguard Worker state.PauseTiming();
78*58b9f456SAndroid Build Coastguard Worker data = ConstructRandomSet(state.range(0));
79*58b9f456SAndroid Build Coastguard Worker state.ResumeTiming();
80*58b9f456SAndroid Build Coastguard Worker for (int j = 0; j < state.range(1); ++j)
81*58b9f456SAndroid Build Coastguard Worker data.insert(RandomNumber());
82*58b9f456SAndroid Build Coastguard Worker }
83*58b9f456SAndroid Build Coastguard Worker }
84*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_SetInsert)
85*58b9f456SAndroid Build Coastguard Worker ->Args({1<<10, 128})
86*58b9f456SAndroid Build Coastguard Worker ->Args({2<<10, 128})
87*58b9f456SAndroid Build Coastguard Worker ->Args({4<<10, 128})
88*58b9f456SAndroid Build Coastguard Worker ->Args({8<<10, 128})
89*58b9f456SAndroid Build Coastguard Worker ->Args({1<<10, 512})
90*58b9f456SAndroid Build Coastguard Worker ->Args({2<<10, 512})
91*58b9f456SAndroid Build Coastguard Worker ->Args({4<<10, 512})
92*58b9f456SAndroid Build Coastguard Worker ->Args({8<<10, 512});
93*58b9f456SAndroid Build Coastguard Worker
94*58b9f456SAndroid Build Coastguard Worker // The preceding code is quite repetitive, and can be replaced with
95*58b9f456SAndroid Build Coastguard Worker // the following short-hand. The following macro will pick a few
96*58b9f456SAndroid Build Coastguard Worker // appropriate arguments in the product of the two specified ranges
97*58b9f456SAndroid Build Coastguard Worker // and will generate a microbenchmark for each such pair.
98*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {128, 512}});
99*58b9f456SAndroid Build Coastguard Worker
100*58b9f456SAndroid Build Coastguard Worker // For more complex patterns of inputs, passing a custom function
101*58b9f456SAndroid Build Coastguard Worker // to Apply allows programmatic specification of an
102*58b9f456SAndroid Build Coastguard Worker // arbitrary set of arguments to run the microbenchmark on.
103*58b9f456SAndroid Build Coastguard Worker // The following example enumerates a dense range on
104*58b9f456SAndroid Build Coastguard Worker // one parameter, and a sparse range on the second.
105*58b9f456SAndroid Build Coastguard Worker static void CustomArguments(benchmark::internal::Benchmark* b) {
106*58b9f456SAndroid Build Coastguard Worker for (int i = 0; i <= 10; ++i)
107*58b9f456SAndroid Build Coastguard Worker for (int j = 32; j <= 1024*1024; j *= 8)
108*58b9f456SAndroid Build Coastguard Worker b->Args({i, j});
109*58b9f456SAndroid Build Coastguard Worker }
110*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_SetInsert)->Apply(CustomArguments);
111*58b9f456SAndroid Build Coastguard Worker
112*58b9f456SAndroid Build Coastguard Worker // Templated microbenchmarks work the same way:
113*58b9f456SAndroid Build Coastguard Worker // Produce then consume 'size' messages 'iters' times
114*58b9f456SAndroid Build Coastguard Worker // Measures throughput in the absence of multiprogramming.
115*58b9f456SAndroid Build Coastguard Worker template <class Q> int BM_Sequential(benchmark::State& state) {
116*58b9f456SAndroid Build Coastguard Worker Q q;
117*58b9f456SAndroid Build Coastguard Worker typename Q::value_type v;
118*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
119*58b9f456SAndroid Build Coastguard Worker for (int i = state.range(0); i--; )
120*58b9f456SAndroid Build Coastguard Worker q.push(v);
121*58b9f456SAndroid Build Coastguard Worker for (int e = state.range(0); e--; )
122*58b9f456SAndroid Build Coastguard Worker q.Wait(&v);
123*58b9f456SAndroid Build Coastguard Worker }
124*58b9f456SAndroid Build Coastguard Worker // actually messages, not bytes:
125*58b9f456SAndroid Build Coastguard Worker state.SetBytesProcessed(
126*58b9f456SAndroid Build Coastguard Worker static_cast<int64_t>(state.iterations())*state.range(0));
127*58b9f456SAndroid Build Coastguard Worker }
128*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
129*58b9f456SAndroid Build Coastguard Worker
130*58b9f456SAndroid Build Coastguard Worker Use `Benchmark::MinTime(double t)` to set the minimum time used to run the
131*58b9f456SAndroid Build Coastguard Worker benchmark. This option overrides the `benchmark_min_time` flag.
132*58b9f456SAndroid Build Coastguard Worker
133*58b9f456SAndroid Build Coastguard Worker void BM_test(benchmark::State& state) {
134*58b9f456SAndroid Build Coastguard Worker ... body ...
135*58b9f456SAndroid Build Coastguard Worker }
136*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_test)->MinTime(2.0); // Run for at least 2 seconds.
137*58b9f456SAndroid Build Coastguard Worker
138*58b9f456SAndroid Build Coastguard Worker In a multithreaded test, it is guaranteed that none of the threads will start
139*58b9f456SAndroid Build Coastguard Worker until all have reached the loop start, and all will have finished before any
140*58b9f456SAndroid Build Coastguard Worker thread exits the loop body. As such, any global setup or teardown you want to
141*58b9f456SAndroid Build Coastguard Worker do can be wrapped in a check against the thread index:
142*58b9f456SAndroid Build Coastguard Worker
143*58b9f456SAndroid Build Coastguard Worker static void BM_MultiThreaded(benchmark::State& state) {
144*58b9f456SAndroid Build Coastguard Worker if (state.thread_index == 0) {
145*58b9f456SAndroid Build Coastguard Worker // Setup code here.
146*58b9f456SAndroid Build Coastguard Worker }
147*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
148*58b9f456SAndroid Build Coastguard Worker // Run the test as normal.
149*58b9f456SAndroid Build Coastguard Worker }
150*58b9f456SAndroid Build Coastguard Worker if (state.thread_index == 0) {
151*58b9f456SAndroid Build Coastguard Worker // Teardown code here.
152*58b9f456SAndroid Build Coastguard Worker }
153*58b9f456SAndroid Build Coastguard Worker }
154*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_MultiThreaded)->Threads(4);
155*58b9f456SAndroid Build Coastguard Worker
156*58b9f456SAndroid Build Coastguard Worker
157*58b9f456SAndroid Build Coastguard Worker If a benchmark runs a few milliseconds it may be hard to visually compare the
158*58b9f456SAndroid Build Coastguard Worker measured times, since the output data is given in nanoseconds per default. In
159*58b9f456SAndroid Build Coastguard Worker order to manually set the time unit, you can specify it manually:
160*58b9f456SAndroid Build Coastguard Worker
161*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_test)->Unit(benchmark::kMillisecond);
162*58b9f456SAndroid Build Coastguard Worker */
163*58b9f456SAndroid Build Coastguard Worker
164*58b9f456SAndroid Build Coastguard Worker #ifndef BENCHMARK_BENCHMARK_H_
165*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_BENCHMARK_H_
166*58b9f456SAndroid Build Coastguard Worker
167*58b9f456SAndroid Build Coastguard Worker // The _MSVC_LANG check should detect Visual Studio 2015 Update 3 and newer.
168*58b9f456SAndroid Build Coastguard Worker #if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
169*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_HAS_CXX11
170*58b9f456SAndroid Build Coastguard Worker #endif
171*58b9f456SAndroid Build Coastguard Worker
172*58b9f456SAndroid Build Coastguard Worker #include <stdint.h>
173*58b9f456SAndroid Build Coastguard Worker
174*58b9f456SAndroid Build Coastguard Worker #include <algorithm>
175*58b9f456SAndroid Build Coastguard Worker #include <cassert>
176*58b9f456SAndroid Build Coastguard Worker #include <cstddef>
177*58b9f456SAndroid Build Coastguard Worker #include <iosfwd>
178*58b9f456SAndroid Build Coastguard Worker #include <map>
179*58b9f456SAndroid Build Coastguard Worker #include <set>
180*58b9f456SAndroid Build Coastguard Worker #include <string>
181*58b9f456SAndroid Build Coastguard Worker #include <vector>
182*58b9f456SAndroid Build Coastguard Worker
183*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_HAS_CXX11)
184*58b9f456SAndroid Build Coastguard Worker #include <initializer_list>
185*58b9f456SAndroid Build Coastguard Worker #include <type_traits>
186*58b9f456SAndroid Build Coastguard Worker #include <utility>
187*58b9f456SAndroid Build Coastguard Worker #endif
188*58b9f456SAndroid Build Coastguard Worker
189*58b9f456SAndroid Build Coastguard Worker #if defined(_MSC_VER)
190*58b9f456SAndroid Build Coastguard Worker #include <intrin.h> // for _ReadWriteBarrier
191*58b9f456SAndroid Build Coastguard Worker #endif
192*58b9f456SAndroid Build Coastguard Worker
193*58b9f456SAndroid Build Coastguard Worker #ifndef BENCHMARK_HAS_CXX11
194*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
195*58b9f456SAndroid Build Coastguard Worker TypeName(const TypeName&); \
196*58b9f456SAndroid Build Coastguard Worker TypeName& operator=(const TypeName&)
197*58b9f456SAndroid Build Coastguard Worker #else
198*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
199*58b9f456SAndroid Build Coastguard Worker TypeName(const TypeName&) = delete; \
200*58b9f456SAndroid Build Coastguard Worker TypeName& operator=(const TypeName&) = delete
201*58b9f456SAndroid Build Coastguard Worker #endif
202*58b9f456SAndroid Build Coastguard Worker
203*58b9f456SAndroid Build Coastguard Worker #if defined(__GNUC__)
204*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_UNUSED __attribute__((unused))
205*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_ALWAYS_INLINE __attribute__((always_inline))
206*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOEXCEPT noexcept
207*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOEXCEPT_OP(x) noexcept(x)
208*58b9f456SAndroid Build Coastguard Worker #elif defined(_MSC_VER) && !defined(__clang__)
209*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_UNUSED
210*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_ALWAYS_INLINE __forceinline
211*58b9f456SAndroid Build Coastguard Worker #if _MSC_VER >= 1900
212*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOEXCEPT noexcept
213*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOEXCEPT_OP(x) noexcept(x)
214*58b9f456SAndroid Build Coastguard Worker #else
215*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOEXCEPT
216*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOEXCEPT_OP(x)
217*58b9f456SAndroid Build Coastguard Worker #endif
218*58b9f456SAndroid Build Coastguard Worker #define __func__ __FUNCTION__
219*58b9f456SAndroid Build Coastguard Worker #else
220*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_UNUSED
221*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_ALWAYS_INLINE
222*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOEXCEPT
223*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOEXCEPT_OP(x)
224*58b9f456SAndroid Build Coastguard Worker #endif
225*58b9f456SAndroid Build Coastguard Worker
226*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_INTERNAL_TOSTRING2(x) #x
227*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_INTERNAL_TOSTRING(x) BENCHMARK_INTERNAL_TOSTRING2(x)
228*58b9f456SAndroid Build Coastguard Worker
229*58b9f456SAndroid Build Coastguard Worker #if defined(__GNUC__) || defined(__clang__)
230*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
231*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
232*58b9f456SAndroid Build Coastguard Worker #else
233*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_BUILTIN_EXPECT(x, y) x
234*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_DEPRECATED_MSG(msg)
235*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_WARNING_MSG(msg) \
236*58b9f456SAndroid Build Coastguard Worker __pragma(message(__FILE__ "(" BENCHMARK_INTERNAL_TOSTRING( \
237*58b9f456SAndroid Build Coastguard Worker __LINE__) ") : warning note: " msg))
238*58b9f456SAndroid Build Coastguard Worker #endif
239*58b9f456SAndroid Build Coastguard Worker
240*58b9f456SAndroid Build Coastguard Worker #if defined(__GNUC__) && !defined(__clang__)
241*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
242*58b9f456SAndroid Build Coastguard Worker #endif
243*58b9f456SAndroid Build Coastguard Worker
244*58b9f456SAndroid Build Coastguard Worker #ifndef __has_builtin
245*58b9f456SAndroid Build Coastguard Worker #define __has_builtin(x) 0
246*58b9f456SAndroid Build Coastguard Worker #endif
247*58b9f456SAndroid Build Coastguard Worker
248*58b9f456SAndroid Build Coastguard Worker #if defined(__GNUC__) || __has_builtin(__builtin_unreachable)
249*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_UNREACHABLE() __builtin_unreachable()
250*58b9f456SAndroid Build Coastguard Worker #elif defined(_MSC_VER)
251*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_UNREACHABLE() __assume(false)
252*58b9f456SAndroid Build Coastguard Worker #else
253*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_UNREACHABLE() ((void)0)
254*58b9f456SAndroid Build Coastguard Worker #endif
255*58b9f456SAndroid Build Coastguard Worker
256*58b9f456SAndroid Build Coastguard Worker namespace benchmark {
257*58b9f456SAndroid Build Coastguard Worker class BenchmarkReporter;
258*58b9f456SAndroid Build Coastguard Worker class MemoryManager;
259*58b9f456SAndroid Build Coastguard Worker
260*58b9f456SAndroid Build Coastguard Worker void Initialize(int* argc, char** argv);
261*58b9f456SAndroid Build Coastguard Worker
262*58b9f456SAndroid Build Coastguard Worker // Report to stdout all arguments in 'argv' as unrecognized except the first.
263*58b9f456SAndroid Build Coastguard Worker // Returns true there is at least on unrecognized argument (i.e. 'argc' > 1).
264*58b9f456SAndroid Build Coastguard Worker bool ReportUnrecognizedArguments(int argc, char** argv);
265*58b9f456SAndroid Build Coastguard Worker
266*58b9f456SAndroid Build Coastguard Worker // Generate a list of benchmarks matching the specified --benchmark_filter flag
267*58b9f456SAndroid Build Coastguard Worker // and if --benchmark_list_tests is specified return after printing the name
268*58b9f456SAndroid Build Coastguard Worker // of each matching benchmark. Otherwise run each matching benchmark and
269*58b9f456SAndroid Build Coastguard Worker // report the results.
270*58b9f456SAndroid Build Coastguard Worker //
271*58b9f456SAndroid Build Coastguard Worker // The second and third overload use the specified 'display_reporter' and
272*58b9f456SAndroid Build Coastguard Worker // 'file_reporter' respectively. 'file_reporter' will write to the file
273*58b9f456SAndroid Build Coastguard Worker // specified
274*58b9f456SAndroid Build Coastguard Worker // by '--benchmark_output'. If '--benchmark_output' is not given the
275*58b9f456SAndroid Build Coastguard Worker // 'file_reporter' is ignored.
276*58b9f456SAndroid Build Coastguard Worker //
277*58b9f456SAndroid Build Coastguard Worker // RETURNS: The number of matching benchmarks.
278*58b9f456SAndroid Build Coastguard Worker size_t RunSpecifiedBenchmarks();
279*58b9f456SAndroid Build Coastguard Worker size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter);
280*58b9f456SAndroid Build Coastguard Worker size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
281*58b9f456SAndroid Build Coastguard Worker BenchmarkReporter* file_reporter);
282*58b9f456SAndroid Build Coastguard Worker
283*58b9f456SAndroid Build Coastguard Worker // Register a MemoryManager instance that will be used to collect and report
284*58b9f456SAndroid Build Coastguard Worker // allocation measurements for benchmark runs.
285*58b9f456SAndroid Build Coastguard Worker void RegisterMemoryManager(MemoryManager* memory_manager);
286*58b9f456SAndroid Build Coastguard Worker
287*58b9f456SAndroid Build Coastguard Worker namespace internal {
288*58b9f456SAndroid Build Coastguard Worker class Benchmark;
289*58b9f456SAndroid Build Coastguard Worker class BenchmarkImp;
290*58b9f456SAndroid Build Coastguard Worker class BenchmarkFamilies;
291*58b9f456SAndroid Build Coastguard Worker
292*58b9f456SAndroid Build Coastguard Worker void UseCharPointer(char const volatile*);
293*58b9f456SAndroid Build Coastguard Worker
294*58b9f456SAndroid Build Coastguard Worker // Take ownership of the pointer and register the benchmark. Return the
295*58b9f456SAndroid Build Coastguard Worker // registered benchmark.
296*58b9f456SAndroid Build Coastguard Worker Benchmark* RegisterBenchmarkInternal(Benchmark*);
297*58b9f456SAndroid Build Coastguard Worker
298*58b9f456SAndroid Build Coastguard Worker // Ensure that the standard streams are properly initialized in every TU.
299*58b9f456SAndroid Build Coastguard Worker int InitializeStreams();
300*58b9f456SAndroid Build Coastguard Worker BENCHMARK_UNUSED static int stream_init_anchor = InitializeStreams();
301*58b9f456SAndroid Build Coastguard Worker
302*58b9f456SAndroid Build Coastguard Worker } // namespace internal
303*58b9f456SAndroid Build Coastguard Worker
304*58b9f456SAndroid Build Coastguard Worker #if (!defined(__GNUC__) && !defined(__clang__)) || defined(__pnacl__) || \
305*58b9f456SAndroid Build Coastguard Worker defined(__EMSCRIPTEN__)
306*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_HAS_NO_INLINE_ASSEMBLY
307*58b9f456SAndroid Build Coastguard Worker #endif
308*58b9f456SAndroid Build Coastguard Worker
309*58b9f456SAndroid Build Coastguard Worker // The DoNotOptimize(...) function can be used to prevent a value or
310*58b9f456SAndroid Build Coastguard Worker // expression from being optimized away by the compiler. This function is
311*58b9f456SAndroid Build Coastguard Worker // intended to add little to no overhead.
312*58b9f456SAndroid Build Coastguard Worker // See: https://youtu.be/nXaxk27zwlk?t=2441
313*58b9f456SAndroid Build Coastguard Worker #ifndef BENCHMARK_HAS_NO_INLINE_ASSEMBLY
314*58b9f456SAndroid Build Coastguard Worker template <class Tp>
DoNotOptimize(Tp const & value)315*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
316*58b9f456SAndroid Build Coastguard Worker asm volatile("" : : "r,m"(value) : "memory");
317*58b9f456SAndroid Build Coastguard Worker }
318*58b9f456SAndroid Build Coastguard Worker
319*58b9f456SAndroid Build Coastguard Worker template <class Tp>
DoNotOptimize(Tp & value)320*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) {
321*58b9f456SAndroid Build Coastguard Worker #if defined(__clang__)
322*58b9f456SAndroid Build Coastguard Worker asm volatile("" : "+r,m"(value) : : "memory");
323*58b9f456SAndroid Build Coastguard Worker #else
324*58b9f456SAndroid Build Coastguard Worker asm volatile("" : "+m,r"(value) : : "memory");
325*58b9f456SAndroid Build Coastguard Worker #endif
326*58b9f456SAndroid Build Coastguard Worker }
327*58b9f456SAndroid Build Coastguard Worker
328*58b9f456SAndroid Build Coastguard Worker // Force the compiler to flush pending writes to global memory. Acts as an
329*58b9f456SAndroid Build Coastguard Worker // effective read/write barrier
ClobberMemory()330*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE void ClobberMemory() {
331*58b9f456SAndroid Build Coastguard Worker asm volatile("" : : : "memory");
332*58b9f456SAndroid Build Coastguard Worker }
333*58b9f456SAndroid Build Coastguard Worker #elif defined(_MSC_VER)
334*58b9f456SAndroid Build Coastguard Worker template <class Tp>
DoNotOptimize(Tp const & value)335*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
336*58b9f456SAndroid Build Coastguard Worker internal::UseCharPointer(&reinterpret_cast<char const volatile&>(value));
337*58b9f456SAndroid Build Coastguard Worker _ReadWriteBarrier();
338*58b9f456SAndroid Build Coastguard Worker }
339*58b9f456SAndroid Build Coastguard Worker
ClobberMemory()340*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE void ClobberMemory() { _ReadWriteBarrier(); }
341*58b9f456SAndroid Build Coastguard Worker #else
342*58b9f456SAndroid Build Coastguard Worker template <class Tp>
DoNotOptimize(Tp const & value)343*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
344*58b9f456SAndroid Build Coastguard Worker internal::UseCharPointer(&reinterpret_cast<char const volatile&>(value));
345*58b9f456SAndroid Build Coastguard Worker }
346*58b9f456SAndroid Build Coastguard Worker // FIXME Add ClobberMemory() for non-gnu and non-msvc compilers
347*58b9f456SAndroid Build Coastguard Worker #endif
348*58b9f456SAndroid Build Coastguard Worker
349*58b9f456SAndroid Build Coastguard Worker // This class is used for user-defined counters.
350*58b9f456SAndroid Build Coastguard Worker class Counter {
351*58b9f456SAndroid Build Coastguard Worker public:
352*58b9f456SAndroid Build Coastguard Worker enum Flags {
353*58b9f456SAndroid Build Coastguard Worker kDefaults = 0,
354*58b9f456SAndroid Build Coastguard Worker // Mark the counter as a rate. It will be presented divided
355*58b9f456SAndroid Build Coastguard Worker // by the duration of the benchmark.
356*58b9f456SAndroid Build Coastguard Worker kIsRate = 1U << 0U,
357*58b9f456SAndroid Build Coastguard Worker // Mark the counter as a thread-average quantity. It will be
358*58b9f456SAndroid Build Coastguard Worker // presented divided by the number of threads.
359*58b9f456SAndroid Build Coastguard Worker kAvgThreads = 1U << 1U,
360*58b9f456SAndroid Build Coastguard Worker // Mark the counter as a thread-average rate. See above.
361*58b9f456SAndroid Build Coastguard Worker kAvgThreadsRate = kIsRate | kAvgThreads,
362*58b9f456SAndroid Build Coastguard Worker // Mark the counter as a constant value, valid/same for *every* iteration.
363*58b9f456SAndroid Build Coastguard Worker // When reporting, it will be *multiplied* by the iteration count.
364*58b9f456SAndroid Build Coastguard Worker kIsIterationInvariant = 1U << 2U,
365*58b9f456SAndroid Build Coastguard Worker // Mark the counter as a constant rate.
366*58b9f456SAndroid Build Coastguard Worker // When reporting, it will be *multiplied* by the iteration count
367*58b9f456SAndroid Build Coastguard Worker // and then divided by the duration of the benchmark.
368*58b9f456SAndroid Build Coastguard Worker kIsIterationInvariantRate = kIsRate | kIsIterationInvariant,
369*58b9f456SAndroid Build Coastguard Worker // Mark the counter as a iteration-average quantity.
370*58b9f456SAndroid Build Coastguard Worker // It will be presented divided by the number of iterations.
371*58b9f456SAndroid Build Coastguard Worker kAvgIterations = 1U << 3U,
372*58b9f456SAndroid Build Coastguard Worker // Mark the counter as a iteration-average rate. See above.
373*58b9f456SAndroid Build Coastguard Worker kAvgIterationsRate = kIsRate | kAvgIterations
374*58b9f456SAndroid Build Coastguard Worker };
375*58b9f456SAndroid Build Coastguard Worker
376*58b9f456SAndroid Build Coastguard Worker enum OneK {
377*58b9f456SAndroid Build Coastguard Worker // 1'000 items per 1k
378*58b9f456SAndroid Build Coastguard Worker kIs1000 = 1000,
379*58b9f456SAndroid Build Coastguard Worker // 1'024 items per 1k
380*58b9f456SAndroid Build Coastguard Worker kIs1024 = 1024
381*58b9f456SAndroid Build Coastguard Worker };
382*58b9f456SAndroid Build Coastguard Worker
383*58b9f456SAndroid Build Coastguard Worker double value;
384*58b9f456SAndroid Build Coastguard Worker Flags flags;
385*58b9f456SAndroid Build Coastguard Worker OneK oneK;
386*58b9f456SAndroid Build Coastguard Worker
387*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
388*58b9f456SAndroid Build Coastguard Worker Counter(double v = 0., Flags f = kDefaults, OneK k = kIs1000)
value(v)389*58b9f456SAndroid Build Coastguard Worker : value(v), flags(f), oneK(k) {}
390*58b9f456SAndroid Build Coastguard Worker
391*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE operator double const&() const { return value; }
392*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE operator double&() { return value; }
393*58b9f456SAndroid Build Coastguard Worker };
394*58b9f456SAndroid Build Coastguard Worker
395*58b9f456SAndroid Build Coastguard Worker // A helper for user code to create unforeseen combinations of Flags, without
396*58b9f456SAndroid Build Coastguard Worker // having to do this cast manually each time, or providing this operator.
397*58b9f456SAndroid Build Coastguard Worker Counter::Flags inline operator|(const Counter::Flags& LHS,
398*58b9f456SAndroid Build Coastguard Worker const Counter::Flags& RHS) {
399*58b9f456SAndroid Build Coastguard Worker return static_cast<Counter::Flags>(static_cast<int>(LHS) |
400*58b9f456SAndroid Build Coastguard Worker static_cast<int>(RHS));
401*58b9f456SAndroid Build Coastguard Worker }
402*58b9f456SAndroid Build Coastguard Worker
403*58b9f456SAndroid Build Coastguard Worker // This is the container for the user-defined counters.
404*58b9f456SAndroid Build Coastguard Worker typedef std::map<std::string, Counter> UserCounters;
405*58b9f456SAndroid Build Coastguard Worker
406*58b9f456SAndroid Build Coastguard Worker // TimeUnit is passed to a benchmark in order to specify the order of magnitude
407*58b9f456SAndroid Build Coastguard Worker // for the measured time.
408*58b9f456SAndroid Build Coastguard Worker enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond };
409*58b9f456SAndroid Build Coastguard Worker
410*58b9f456SAndroid Build Coastguard Worker // BigO is passed to a benchmark in order to specify the asymptotic
411*58b9f456SAndroid Build Coastguard Worker // computational
412*58b9f456SAndroid Build Coastguard Worker // complexity for the benchmark. In case oAuto is selected, complexity will be
413*58b9f456SAndroid Build Coastguard Worker // calculated automatically to the best fit.
414*58b9f456SAndroid Build Coastguard Worker enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda };
415*58b9f456SAndroid Build Coastguard Worker
416*58b9f456SAndroid Build Coastguard Worker // BigOFunc is passed to a benchmark in order to specify the asymptotic
417*58b9f456SAndroid Build Coastguard Worker // computational complexity for the benchmark.
418*58b9f456SAndroid Build Coastguard Worker typedef double(BigOFunc)(int64_t);
419*58b9f456SAndroid Build Coastguard Worker
420*58b9f456SAndroid Build Coastguard Worker // StatisticsFunc is passed to a benchmark in order to compute some descriptive
421*58b9f456SAndroid Build Coastguard Worker // statistics over all the measurements of some type
422*58b9f456SAndroid Build Coastguard Worker typedef double(StatisticsFunc)(const std::vector<double>&);
423*58b9f456SAndroid Build Coastguard Worker
424*58b9f456SAndroid Build Coastguard Worker struct Statistics {
425*58b9f456SAndroid Build Coastguard Worker std::string name_;
426*58b9f456SAndroid Build Coastguard Worker StatisticsFunc* compute_;
427*58b9f456SAndroid Build Coastguard Worker
StatisticsStatistics428*58b9f456SAndroid Build Coastguard Worker Statistics(const std::string& name, StatisticsFunc* compute)
429*58b9f456SAndroid Build Coastguard Worker : name_(name), compute_(compute) {}
430*58b9f456SAndroid Build Coastguard Worker };
431*58b9f456SAndroid Build Coastguard Worker
432*58b9f456SAndroid Build Coastguard Worker namespace internal {
433*58b9f456SAndroid Build Coastguard Worker struct BenchmarkInstance;
434*58b9f456SAndroid Build Coastguard Worker class ThreadTimer;
435*58b9f456SAndroid Build Coastguard Worker class ThreadManager;
436*58b9f456SAndroid Build Coastguard Worker
437*58b9f456SAndroid Build Coastguard Worker enum AggregationReportMode
438*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_HAS_CXX11)
439*58b9f456SAndroid Build Coastguard Worker : unsigned
440*58b9f456SAndroid Build Coastguard Worker #else
441*58b9f456SAndroid Build Coastguard Worker #endif
442*58b9f456SAndroid Build Coastguard Worker {
443*58b9f456SAndroid Build Coastguard Worker // The mode has not been manually specified
444*58b9f456SAndroid Build Coastguard Worker ARM_Unspecified = 0,
445*58b9f456SAndroid Build Coastguard Worker // The mode is user-specified.
446*58b9f456SAndroid Build Coastguard Worker // This may or may not be set when the following bit-flags are set.
447*58b9f456SAndroid Build Coastguard Worker ARM_Default = 1U << 0U,
448*58b9f456SAndroid Build Coastguard Worker // File reporter should only output aggregates.
449*58b9f456SAndroid Build Coastguard Worker ARM_FileReportAggregatesOnly = 1U << 1U,
450*58b9f456SAndroid Build Coastguard Worker // Display reporter should only output aggregates
451*58b9f456SAndroid Build Coastguard Worker ARM_DisplayReportAggregatesOnly = 1U << 2U,
452*58b9f456SAndroid Build Coastguard Worker // Both reporters should only display aggregates.
453*58b9f456SAndroid Build Coastguard Worker ARM_ReportAggregatesOnly =
454*58b9f456SAndroid Build Coastguard Worker ARM_FileReportAggregatesOnly | ARM_DisplayReportAggregatesOnly
455*58b9f456SAndroid Build Coastguard Worker };
456*58b9f456SAndroid Build Coastguard Worker
457*58b9f456SAndroid Build Coastguard Worker } // namespace internal
458*58b9f456SAndroid Build Coastguard Worker
459*58b9f456SAndroid Build Coastguard Worker // State is passed to a running Benchmark and contains state for the
460*58b9f456SAndroid Build Coastguard Worker // benchmark to use.
461*58b9f456SAndroid Build Coastguard Worker class State {
462*58b9f456SAndroid Build Coastguard Worker public:
463*58b9f456SAndroid Build Coastguard Worker struct StateIterator;
464*58b9f456SAndroid Build Coastguard Worker friend struct StateIterator;
465*58b9f456SAndroid Build Coastguard Worker
466*58b9f456SAndroid Build Coastguard Worker // Returns iterators used to run each iteration of a benchmark using a
467*58b9f456SAndroid Build Coastguard Worker // C++11 ranged-based for loop. These functions should not be called directly.
468*58b9f456SAndroid Build Coastguard Worker //
469*58b9f456SAndroid Build Coastguard Worker // REQUIRES: The benchmark has not started running yet. Neither begin nor end
470*58b9f456SAndroid Build Coastguard Worker // have been called previously.
471*58b9f456SAndroid Build Coastguard Worker //
472*58b9f456SAndroid Build Coastguard Worker // NOTE: KeepRunning may not be used after calling either of these functions.
473*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE StateIterator begin();
474*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE StateIterator end();
475*58b9f456SAndroid Build Coastguard Worker
476*58b9f456SAndroid Build Coastguard Worker // Returns true if the benchmark should continue through another iteration.
477*58b9f456SAndroid Build Coastguard Worker // NOTE: A benchmark may not return from the test until KeepRunning() has
478*58b9f456SAndroid Build Coastguard Worker // returned false.
479*58b9f456SAndroid Build Coastguard Worker bool KeepRunning();
480*58b9f456SAndroid Build Coastguard Worker
481*58b9f456SAndroid Build Coastguard Worker // Returns true iff the benchmark should run n more iterations.
482*58b9f456SAndroid Build Coastguard Worker // REQUIRES: 'n' > 0.
483*58b9f456SAndroid Build Coastguard Worker // NOTE: A benchmark must not return from the test until KeepRunningBatch()
484*58b9f456SAndroid Build Coastguard Worker // has returned false.
485*58b9f456SAndroid Build Coastguard Worker // NOTE: KeepRunningBatch() may overshoot by up to 'n' iterations.
486*58b9f456SAndroid Build Coastguard Worker //
487*58b9f456SAndroid Build Coastguard Worker // Intended usage:
488*58b9f456SAndroid Build Coastguard Worker // while (state.KeepRunningBatch(1000)) {
489*58b9f456SAndroid Build Coastguard Worker // // process 1000 elements
490*58b9f456SAndroid Build Coastguard Worker // }
491*58b9f456SAndroid Build Coastguard Worker bool KeepRunningBatch(size_t n);
492*58b9f456SAndroid Build Coastguard Worker
493*58b9f456SAndroid Build Coastguard Worker // REQUIRES: timer is running and 'SkipWithError(...)' has not been called
494*58b9f456SAndroid Build Coastguard Worker // by the current thread.
495*58b9f456SAndroid Build Coastguard Worker // Stop the benchmark timer. If not called, the timer will be
496*58b9f456SAndroid Build Coastguard Worker // automatically stopped after the last iteration of the benchmark loop.
497*58b9f456SAndroid Build Coastguard Worker //
498*58b9f456SAndroid Build Coastguard Worker // For threaded benchmarks the PauseTiming() function only pauses the timing
499*58b9f456SAndroid Build Coastguard Worker // for the current thread.
500*58b9f456SAndroid Build Coastguard Worker //
501*58b9f456SAndroid Build Coastguard Worker // NOTE: The "real time" measurement is per-thread. If different threads
502*58b9f456SAndroid Build Coastguard Worker // report different measurements the largest one is reported.
503*58b9f456SAndroid Build Coastguard Worker //
504*58b9f456SAndroid Build Coastguard Worker // NOTE: PauseTiming()/ResumeTiming() are relatively
505*58b9f456SAndroid Build Coastguard Worker // heavyweight, and so their use should generally be avoided
506*58b9f456SAndroid Build Coastguard Worker // within each benchmark iteration, if possible.
507*58b9f456SAndroid Build Coastguard Worker void PauseTiming();
508*58b9f456SAndroid Build Coastguard Worker
509*58b9f456SAndroid Build Coastguard Worker // REQUIRES: timer is not running and 'SkipWithError(...)' has not been called
510*58b9f456SAndroid Build Coastguard Worker // by the current thread.
511*58b9f456SAndroid Build Coastguard Worker // Start the benchmark timer. The timer is NOT running on entrance to the
512*58b9f456SAndroid Build Coastguard Worker // benchmark function. It begins running after control flow enters the
513*58b9f456SAndroid Build Coastguard Worker // benchmark loop.
514*58b9f456SAndroid Build Coastguard Worker //
515*58b9f456SAndroid Build Coastguard Worker // NOTE: PauseTiming()/ResumeTiming() are relatively
516*58b9f456SAndroid Build Coastguard Worker // heavyweight, and so their use should generally be avoided
517*58b9f456SAndroid Build Coastguard Worker // within each benchmark iteration, if possible.
518*58b9f456SAndroid Build Coastguard Worker void ResumeTiming();
519*58b9f456SAndroid Build Coastguard Worker
520*58b9f456SAndroid Build Coastguard Worker // REQUIRES: 'SkipWithError(...)' has not been called previously by the
521*58b9f456SAndroid Build Coastguard Worker // current thread.
522*58b9f456SAndroid Build Coastguard Worker // Report the benchmark as resulting in an error with the specified 'msg'.
523*58b9f456SAndroid Build Coastguard Worker // After this call the user may explicitly 'return' from the benchmark.
524*58b9f456SAndroid Build Coastguard Worker //
525*58b9f456SAndroid Build Coastguard Worker // If the ranged-for style of benchmark loop is used, the user must explicitly
526*58b9f456SAndroid Build Coastguard Worker // break from the loop, otherwise all future iterations will be run.
527*58b9f456SAndroid Build Coastguard Worker // If the 'KeepRunning()' loop is used the current thread will automatically
528*58b9f456SAndroid Build Coastguard Worker // exit the loop at the end of the current iteration.
529*58b9f456SAndroid Build Coastguard Worker //
530*58b9f456SAndroid Build Coastguard Worker // For threaded benchmarks only the current thread stops executing and future
531*58b9f456SAndroid Build Coastguard Worker // calls to `KeepRunning()` will block until all threads have completed
532*58b9f456SAndroid Build Coastguard Worker // the `KeepRunning()` loop. If multiple threads report an error only the
533*58b9f456SAndroid Build Coastguard Worker // first error message is used.
534*58b9f456SAndroid Build Coastguard Worker //
535*58b9f456SAndroid Build Coastguard Worker // NOTE: Calling 'SkipWithError(...)' does not cause the benchmark to exit
536*58b9f456SAndroid Build Coastguard Worker // the current scope immediately. If the function is called from within
537*58b9f456SAndroid Build Coastguard Worker // the 'KeepRunning()' loop the current iteration will finish. It is the users
538*58b9f456SAndroid Build Coastguard Worker // responsibility to exit the scope as needed.
539*58b9f456SAndroid Build Coastguard Worker void SkipWithError(const char* msg);
540*58b9f456SAndroid Build Coastguard Worker
541*58b9f456SAndroid Build Coastguard Worker // REQUIRES: called exactly once per iteration of the benchmarking loop.
542*58b9f456SAndroid Build Coastguard Worker // Set the manually measured time for this benchmark iteration, which
543*58b9f456SAndroid Build Coastguard Worker // is used instead of automatically measured time if UseManualTime() was
544*58b9f456SAndroid Build Coastguard Worker // specified.
545*58b9f456SAndroid Build Coastguard Worker //
546*58b9f456SAndroid Build Coastguard Worker // For threaded benchmarks the final value will be set to the largest
547*58b9f456SAndroid Build Coastguard Worker // reported values.
548*58b9f456SAndroid Build Coastguard Worker void SetIterationTime(double seconds);
549*58b9f456SAndroid Build Coastguard Worker
550*58b9f456SAndroid Build Coastguard Worker // Set the number of bytes processed by the current benchmark
551*58b9f456SAndroid Build Coastguard Worker // execution. This routine is typically called once at the end of a
552*58b9f456SAndroid Build Coastguard Worker // throughput oriented benchmark.
553*58b9f456SAndroid Build Coastguard Worker //
554*58b9f456SAndroid Build Coastguard Worker // REQUIRES: a benchmark has exited its benchmarking loop.
555*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
SetBytesProcessed(int64_t bytes)556*58b9f456SAndroid Build Coastguard Worker void SetBytesProcessed(int64_t bytes) {
557*58b9f456SAndroid Build Coastguard Worker counters["bytes_per_second"] =
558*58b9f456SAndroid Build Coastguard Worker Counter(static_cast<double>(bytes), Counter::kIsRate, Counter::kIs1024);
559*58b9f456SAndroid Build Coastguard Worker }
560*58b9f456SAndroid Build Coastguard Worker
561*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
bytes_processed()562*58b9f456SAndroid Build Coastguard Worker int64_t bytes_processed() const {
563*58b9f456SAndroid Build Coastguard Worker if (counters.find("bytes_per_second") != counters.end())
564*58b9f456SAndroid Build Coastguard Worker return static_cast<int64_t>(counters.at("bytes_per_second"));
565*58b9f456SAndroid Build Coastguard Worker return 0;
566*58b9f456SAndroid Build Coastguard Worker }
567*58b9f456SAndroid Build Coastguard Worker
568*58b9f456SAndroid Build Coastguard Worker // If this routine is called with complexity_n > 0 and complexity report is
569*58b9f456SAndroid Build Coastguard Worker // requested for the
570*58b9f456SAndroid Build Coastguard Worker // family benchmark, then current benchmark will be part of the computation
571*58b9f456SAndroid Build Coastguard Worker // and complexity_n will
572*58b9f456SAndroid Build Coastguard Worker // represent the length of N.
573*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
SetComplexityN(int64_t complexity_n)574*58b9f456SAndroid Build Coastguard Worker void SetComplexityN(int64_t complexity_n) { complexity_n_ = complexity_n; }
575*58b9f456SAndroid Build Coastguard Worker
576*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
complexity_length_n()577*58b9f456SAndroid Build Coastguard Worker int64_t complexity_length_n() { return complexity_n_; }
578*58b9f456SAndroid Build Coastguard Worker
579*58b9f456SAndroid Build Coastguard Worker // If this routine is called with items > 0, then an items/s
580*58b9f456SAndroid Build Coastguard Worker // label is printed on the benchmark report line for the currently
581*58b9f456SAndroid Build Coastguard Worker // executing benchmark. It is typically called at the end of a processing
582*58b9f456SAndroid Build Coastguard Worker // benchmark where a processing items/second output is desired.
583*58b9f456SAndroid Build Coastguard Worker //
584*58b9f456SAndroid Build Coastguard Worker // REQUIRES: a benchmark has exited its benchmarking loop.
585*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
SetItemsProcessed(int64_t items)586*58b9f456SAndroid Build Coastguard Worker void SetItemsProcessed(int64_t items) {
587*58b9f456SAndroid Build Coastguard Worker counters["items_per_second"] =
588*58b9f456SAndroid Build Coastguard Worker Counter(static_cast<double>(items), benchmark::Counter::kIsRate);
589*58b9f456SAndroid Build Coastguard Worker }
590*58b9f456SAndroid Build Coastguard Worker
591*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
items_processed()592*58b9f456SAndroid Build Coastguard Worker int64_t items_processed() const {
593*58b9f456SAndroid Build Coastguard Worker if (counters.find("items_per_second") != counters.end())
594*58b9f456SAndroid Build Coastguard Worker return static_cast<int64_t>(counters.at("items_per_second"));
595*58b9f456SAndroid Build Coastguard Worker return 0;
596*58b9f456SAndroid Build Coastguard Worker }
597*58b9f456SAndroid Build Coastguard Worker
598*58b9f456SAndroid Build Coastguard Worker // If this routine is called, the specified label is printed at the
599*58b9f456SAndroid Build Coastguard Worker // end of the benchmark report line for the currently executing
600*58b9f456SAndroid Build Coastguard Worker // benchmark. Example:
601*58b9f456SAndroid Build Coastguard Worker // static void BM_Compress(benchmark::State& state) {
602*58b9f456SAndroid Build Coastguard Worker // ...
603*58b9f456SAndroid Build Coastguard Worker // double compress = input_size / output_size;
604*58b9f456SAndroid Build Coastguard Worker // state.SetLabel(StrFormat("compress:%.1f%%", 100.0*compression));
605*58b9f456SAndroid Build Coastguard Worker // }
606*58b9f456SAndroid Build Coastguard Worker // Produces output that looks like:
607*58b9f456SAndroid Build Coastguard Worker // BM_Compress 50 50 14115038 compress:27.3%
608*58b9f456SAndroid Build Coastguard Worker //
609*58b9f456SAndroid Build Coastguard Worker // REQUIRES: a benchmark has exited its benchmarking loop.
610*58b9f456SAndroid Build Coastguard Worker void SetLabel(const char* label);
611*58b9f456SAndroid Build Coastguard Worker
SetLabel(const std::string & str)612*58b9f456SAndroid Build Coastguard Worker void BENCHMARK_ALWAYS_INLINE SetLabel(const std::string& str) {
613*58b9f456SAndroid Build Coastguard Worker this->SetLabel(str.c_str());
614*58b9f456SAndroid Build Coastguard Worker }
615*58b9f456SAndroid Build Coastguard Worker
616*58b9f456SAndroid Build Coastguard Worker // Range arguments for this run. CHECKs if the argument has been set.
617*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
618*58b9f456SAndroid Build Coastguard Worker int64_t range(std::size_t pos = 0) const {
619*58b9f456SAndroid Build Coastguard Worker assert(range_.size() > pos);
620*58b9f456SAndroid Build Coastguard Worker return range_[pos];
621*58b9f456SAndroid Build Coastguard Worker }
622*58b9f456SAndroid Build Coastguard Worker
623*58b9f456SAndroid Build Coastguard Worker BENCHMARK_DEPRECATED_MSG("use 'range(0)' instead")
range_x()624*58b9f456SAndroid Build Coastguard Worker int64_t range_x() const { return range(0); }
625*58b9f456SAndroid Build Coastguard Worker
626*58b9f456SAndroid Build Coastguard Worker BENCHMARK_DEPRECATED_MSG("use 'range(1)' instead")
range_y()627*58b9f456SAndroid Build Coastguard Worker int64_t range_y() const { return range(1); }
628*58b9f456SAndroid Build Coastguard Worker
629*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
iterations()630*58b9f456SAndroid Build Coastguard Worker size_t iterations() const {
631*58b9f456SAndroid Build Coastguard Worker if (BENCHMARK_BUILTIN_EXPECT(!started_, false)) {
632*58b9f456SAndroid Build Coastguard Worker return 0;
633*58b9f456SAndroid Build Coastguard Worker }
634*58b9f456SAndroid Build Coastguard Worker return max_iterations - total_iterations_ + batch_leftover_;
635*58b9f456SAndroid Build Coastguard Worker }
636*58b9f456SAndroid Build Coastguard Worker
637*58b9f456SAndroid Build Coastguard Worker private
638*58b9f456SAndroid Build Coastguard Worker : // items we expect on the first cache line (ie 64 bytes of the struct)
639*58b9f456SAndroid Build Coastguard Worker // When total_iterations_ is 0, KeepRunning() and friends will return false.
640*58b9f456SAndroid Build Coastguard Worker // May be larger than max_iterations.
641*58b9f456SAndroid Build Coastguard Worker size_t total_iterations_;
642*58b9f456SAndroid Build Coastguard Worker
643*58b9f456SAndroid Build Coastguard Worker // When using KeepRunningBatch(), batch_leftover_ holds the number of
644*58b9f456SAndroid Build Coastguard Worker // iterations beyond max_iters that were run. Used to track
645*58b9f456SAndroid Build Coastguard Worker // completed_iterations_ accurately.
646*58b9f456SAndroid Build Coastguard Worker size_t batch_leftover_;
647*58b9f456SAndroid Build Coastguard Worker
648*58b9f456SAndroid Build Coastguard Worker public:
649*58b9f456SAndroid Build Coastguard Worker const size_t max_iterations;
650*58b9f456SAndroid Build Coastguard Worker
651*58b9f456SAndroid Build Coastguard Worker private:
652*58b9f456SAndroid Build Coastguard Worker bool started_;
653*58b9f456SAndroid Build Coastguard Worker bool finished_;
654*58b9f456SAndroid Build Coastguard Worker bool error_occurred_;
655*58b9f456SAndroid Build Coastguard Worker
656*58b9f456SAndroid Build Coastguard Worker private: // items we don't need on the first cache line
657*58b9f456SAndroid Build Coastguard Worker std::vector<int64_t> range_;
658*58b9f456SAndroid Build Coastguard Worker
659*58b9f456SAndroid Build Coastguard Worker int64_t complexity_n_;
660*58b9f456SAndroid Build Coastguard Worker
661*58b9f456SAndroid Build Coastguard Worker public:
662*58b9f456SAndroid Build Coastguard Worker // Container for user-defined counters.
663*58b9f456SAndroid Build Coastguard Worker UserCounters counters;
664*58b9f456SAndroid Build Coastguard Worker // Index of the executing thread. Values from [0, threads).
665*58b9f456SAndroid Build Coastguard Worker const int thread_index;
666*58b9f456SAndroid Build Coastguard Worker // Number of threads concurrently executing the benchmark.
667*58b9f456SAndroid Build Coastguard Worker const int threads;
668*58b9f456SAndroid Build Coastguard Worker
669*58b9f456SAndroid Build Coastguard Worker private:
670*58b9f456SAndroid Build Coastguard Worker State(size_t max_iters, const std::vector<int64_t>& ranges, int thread_i,
671*58b9f456SAndroid Build Coastguard Worker int n_threads, internal::ThreadTimer* timer,
672*58b9f456SAndroid Build Coastguard Worker internal::ThreadManager* manager);
673*58b9f456SAndroid Build Coastguard Worker
674*58b9f456SAndroid Build Coastguard Worker void StartKeepRunning();
675*58b9f456SAndroid Build Coastguard Worker // Implementation of KeepRunning() and KeepRunningBatch().
676*58b9f456SAndroid Build Coastguard Worker // is_batch must be true unless n is 1.
677*58b9f456SAndroid Build Coastguard Worker bool KeepRunningInternal(size_t n, bool is_batch);
678*58b9f456SAndroid Build Coastguard Worker void FinishKeepRunning();
679*58b9f456SAndroid Build Coastguard Worker internal::ThreadTimer* timer_;
680*58b9f456SAndroid Build Coastguard Worker internal::ThreadManager* manager_;
681*58b9f456SAndroid Build Coastguard Worker
682*58b9f456SAndroid Build Coastguard Worker friend struct internal::BenchmarkInstance;
683*58b9f456SAndroid Build Coastguard Worker };
684*58b9f456SAndroid Build Coastguard Worker
KeepRunning()685*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE bool State::KeepRunning() {
686*58b9f456SAndroid Build Coastguard Worker return KeepRunningInternal(1, /*is_batch=*/false);
687*58b9f456SAndroid Build Coastguard Worker }
688*58b9f456SAndroid Build Coastguard Worker
KeepRunningBatch(size_t n)689*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE bool State::KeepRunningBatch(size_t n) {
690*58b9f456SAndroid Build Coastguard Worker return KeepRunningInternal(n, /*is_batch=*/true);
691*58b9f456SAndroid Build Coastguard Worker }
692*58b9f456SAndroid Build Coastguard Worker
KeepRunningInternal(size_t n,bool is_batch)693*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE bool State::KeepRunningInternal(size_t n,
694*58b9f456SAndroid Build Coastguard Worker bool is_batch) {
695*58b9f456SAndroid Build Coastguard Worker // total_iterations_ is set to 0 by the constructor, and always set to a
696*58b9f456SAndroid Build Coastguard Worker // nonzero value by StartKepRunning().
697*58b9f456SAndroid Build Coastguard Worker assert(n > 0);
698*58b9f456SAndroid Build Coastguard Worker // n must be 1 unless is_batch is true.
699*58b9f456SAndroid Build Coastguard Worker assert(is_batch || n == 1);
700*58b9f456SAndroid Build Coastguard Worker if (BENCHMARK_BUILTIN_EXPECT(total_iterations_ >= n, true)) {
701*58b9f456SAndroid Build Coastguard Worker total_iterations_ -= n;
702*58b9f456SAndroid Build Coastguard Worker return true;
703*58b9f456SAndroid Build Coastguard Worker }
704*58b9f456SAndroid Build Coastguard Worker if (!started_) {
705*58b9f456SAndroid Build Coastguard Worker StartKeepRunning();
706*58b9f456SAndroid Build Coastguard Worker if (!error_occurred_ && total_iterations_ >= n) {
707*58b9f456SAndroid Build Coastguard Worker total_iterations_ -= n;
708*58b9f456SAndroid Build Coastguard Worker return true;
709*58b9f456SAndroid Build Coastguard Worker }
710*58b9f456SAndroid Build Coastguard Worker }
711*58b9f456SAndroid Build Coastguard Worker // For non-batch runs, total_iterations_ must be 0 by now.
712*58b9f456SAndroid Build Coastguard Worker if (is_batch && total_iterations_ != 0) {
713*58b9f456SAndroid Build Coastguard Worker batch_leftover_ = n - total_iterations_;
714*58b9f456SAndroid Build Coastguard Worker total_iterations_ = 0;
715*58b9f456SAndroid Build Coastguard Worker return true;
716*58b9f456SAndroid Build Coastguard Worker }
717*58b9f456SAndroid Build Coastguard Worker FinishKeepRunning();
718*58b9f456SAndroid Build Coastguard Worker return false;
719*58b9f456SAndroid Build Coastguard Worker }
720*58b9f456SAndroid Build Coastguard Worker
721*58b9f456SAndroid Build Coastguard Worker struct State::StateIterator {
722*58b9f456SAndroid Build Coastguard Worker struct BENCHMARK_UNUSED Value {};
723*58b9f456SAndroid Build Coastguard Worker typedef std::forward_iterator_tag iterator_category;
724*58b9f456SAndroid Build Coastguard Worker typedef Value value_type;
725*58b9f456SAndroid Build Coastguard Worker typedef Value reference;
726*58b9f456SAndroid Build Coastguard Worker typedef Value pointer;
727*58b9f456SAndroid Build Coastguard Worker typedef std::ptrdiff_t difference_type;
728*58b9f456SAndroid Build Coastguard Worker
729*58b9f456SAndroid Build Coastguard Worker private:
730*58b9f456SAndroid Build Coastguard Worker friend class State;
731*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
StateIteratorStateIterator732*58b9f456SAndroid Build Coastguard Worker StateIterator() : cached_(0), parent_() {}
733*58b9f456SAndroid Build Coastguard Worker
734*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
StateIteratorStateIterator735*58b9f456SAndroid Build Coastguard Worker explicit StateIterator(State* st)
736*58b9f456SAndroid Build Coastguard Worker : cached_(st->error_occurred_ ? 0 : st->max_iterations), parent_(st) {}
737*58b9f456SAndroid Build Coastguard Worker
738*58b9f456SAndroid Build Coastguard Worker public:
739*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
740*58b9f456SAndroid Build Coastguard Worker Value operator*() const { return Value(); }
741*58b9f456SAndroid Build Coastguard Worker
742*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
743*58b9f456SAndroid Build Coastguard Worker StateIterator& operator++() {
744*58b9f456SAndroid Build Coastguard Worker assert(cached_ > 0);
745*58b9f456SAndroid Build Coastguard Worker --cached_;
746*58b9f456SAndroid Build Coastguard Worker return *this;
747*58b9f456SAndroid Build Coastguard Worker }
748*58b9f456SAndroid Build Coastguard Worker
749*58b9f456SAndroid Build Coastguard Worker BENCHMARK_ALWAYS_INLINE
750*58b9f456SAndroid Build Coastguard Worker bool operator!=(StateIterator const&) const {
751*58b9f456SAndroid Build Coastguard Worker if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true;
752*58b9f456SAndroid Build Coastguard Worker parent_->FinishKeepRunning();
753*58b9f456SAndroid Build Coastguard Worker return false;
754*58b9f456SAndroid Build Coastguard Worker }
755*58b9f456SAndroid Build Coastguard Worker
756*58b9f456SAndroid Build Coastguard Worker private:
757*58b9f456SAndroid Build Coastguard Worker size_t cached_;
758*58b9f456SAndroid Build Coastguard Worker State* const parent_;
759*58b9f456SAndroid Build Coastguard Worker };
760*58b9f456SAndroid Build Coastguard Worker
begin()761*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() {
762*58b9f456SAndroid Build Coastguard Worker return StateIterator(this);
763*58b9f456SAndroid Build Coastguard Worker }
end()764*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() {
765*58b9f456SAndroid Build Coastguard Worker StartKeepRunning();
766*58b9f456SAndroid Build Coastguard Worker return StateIterator();
767*58b9f456SAndroid Build Coastguard Worker }
768*58b9f456SAndroid Build Coastguard Worker
769*58b9f456SAndroid Build Coastguard Worker namespace internal {
770*58b9f456SAndroid Build Coastguard Worker
771*58b9f456SAndroid Build Coastguard Worker typedef void(Function)(State&);
772*58b9f456SAndroid Build Coastguard Worker
773*58b9f456SAndroid Build Coastguard Worker // ------------------------------------------------------
774*58b9f456SAndroid Build Coastguard Worker // Benchmark registration object. The BENCHMARK() macro expands
775*58b9f456SAndroid Build Coastguard Worker // into an internal::Benchmark* object. Various methods can
776*58b9f456SAndroid Build Coastguard Worker // be called on this object to change the properties of the benchmark.
777*58b9f456SAndroid Build Coastguard Worker // Each method returns "this" so that multiple method calls can
778*58b9f456SAndroid Build Coastguard Worker // chained into one expression.
779*58b9f456SAndroid Build Coastguard Worker class Benchmark {
780*58b9f456SAndroid Build Coastguard Worker public:
781*58b9f456SAndroid Build Coastguard Worker virtual ~Benchmark();
782*58b9f456SAndroid Build Coastguard Worker
783*58b9f456SAndroid Build Coastguard Worker // Note: the following methods all return "this" so that multiple
784*58b9f456SAndroid Build Coastguard Worker // method calls can be chained together in one expression.
785*58b9f456SAndroid Build Coastguard Worker
786*58b9f456SAndroid Build Coastguard Worker // Run this benchmark once with "x" as the extra argument passed
787*58b9f456SAndroid Build Coastguard Worker // to the function.
788*58b9f456SAndroid Build Coastguard Worker // REQUIRES: The function passed to the constructor must accept an arg1.
789*58b9f456SAndroid Build Coastguard Worker Benchmark* Arg(int64_t x);
790*58b9f456SAndroid Build Coastguard Worker
791*58b9f456SAndroid Build Coastguard Worker // Run this benchmark with the given time unit for the generated output report
792*58b9f456SAndroid Build Coastguard Worker Benchmark* Unit(TimeUnit unit);
793*58b9f456SAndroid Build Coastguard Worker
794*58b9f456SAndroid Build Coastguard Worker // Run this benchmark once for a number of values picked from the
795*58b9f456SAndroid Build Coastguard Worker // range [start..limit]. (start and limit are always picked.)
796*58b9f456SAndroid Build Coastguard Worker // REQUIRES: The function passed to the constructor must accept an arg1.
797*58b9f456SAndroid Build Coastguard Worker Benchmark* Range(int64_t start, int64_t limit);
798*58b9f456SAndroid Build Coastguard Worker
799*58b9f456SAndroid Build Coastguard Worker // Run this benchmark once for all values in the range [start..limit] with
800*58b9f456SAndroid Build Coastguard Worker // specific step
801*58b9f456SAndroid Build Coastguard Worker // REQUIRES: The function passed to the constructor must accept an arg1.
802*58b9f456SAndroid Build Coastguard Worker Benchmark* DenseRange(int64_t start, int64_t limit, int step = 1);
803*58b9f456SAndroid Build Coastguard Worker
804*58b9f456SAndroid Build Coastguard Worker // Run this benchmark once with "args" as the extra arguments passed
805*58b9f456SAndroid Build Coastguard Worker // to the function.
806*58b9f456SAndroid Build Coastguard Worker // REQUIRES: The function passed to the constructor must accept arg1, arg2 ...
807*58b9f456SAndroid Build Coastguard Worker Benchmark* Args(const std::vector<int64_t>& args);
808*58b9f456SAndroid Build Coastguard Worker
809*58b9f456SAndroid Build Coastguard Worker // Equivalent to Args({x, y})
810*58b9f456SAndroid Build Coastguard Worker // NOTE: This is a legacy C++03 interface provided for compatibility only.
811*58b9f456SAndroid Build Coastguard Worker // New code should use 'Args'.
ArgPair(int64_t x,int64_t y)812*58b9f456SAndroid Build Coastguard Worker Benchmark* ArgPair(int64_t x, int64_t y) {
813*58b9f456SAndroid Build Coastguard Worker std::vector<int64_t> args;
814*58b9f456SAndroid Build Coastguard Worker args.push_back(x);
815*58b9f456SAndroid Build Coastguard Worker args.push_back(y);
816*58b9f456SAndroid Build Coastguard Worker return Args(args);
817*58b9f456SAndroid Build Coastguard Worker }
818*58b9f456SAndroid Build Coastguard Worker
819*58b9f456SAndroid Build Coastguard Worker // Run this benchmark once for a number of values picked from the
820*58b9f456SAndroid Build Coastguard Worker // ranges [start..limit]. (starts and limits are always picked.)
821*58b9f456SAndroid Build Coastguard Worker // REQUIRES: The function passed to the constructor must accept arg1, arg2 ...
822*58b9f456SAndroid Build Coastguard Worker Benchmark* Ranges(const std::vector<std::pair<int64_t, int64_t> >& ranges);
823*58b9f456SAndroid Build Coastguard Worker
824*58b9f456SAndroid Build Coastguard Worker // Equivalent to ArgNames({name})
825*58b9f456SAndroid Build Coastguard Worker Benchmark* ArgName(const std::string& name);
826*58b9f456SAndroid Build Coastguard Worker
827*58b9f456SAndroid Build Coastguard Worker // Set the argument names to display in the benchmark name. If not called,
828*58b9f456SAndroid Build Coastguard Worker // only argument values will be shown.
829*58b9f456SAndroid Build Coastguard Worker Benchmark* ArgNames(const std::vector<std::string>& names);
830*58b9f456SAndroid Build Coastguard Worker
831*58b9f456SAndroid Build Coastguard Worker // Equivalent to Ranges({{lo1, hi1}, {lo2, hi2}}).
832*58b9f456SAndroid Build Coastguard Worker // NOTE: This is a legacy C++03 interface provided for compatibility only.
833*58b9f456SAndroid Build Coastguard Worker // New code should use 'Ranges'.
RangePair(int64_t lo1,int64_t hi1,int64_t lo2,int64_t hi2)834*58b9f456SAndroid Build Coastguard Worker Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) {
835*58b9f456SAndroid Build Coastguard Worker std::vector<std::pair<int64_t, int64_t> > ranges;
836*58b9f456SAndroid Build Coastguard Worker ranges.push_back(std::make_pair(lo1, hi1));
837*58b9f456SAndroid Build Coastguard Worker ranges.push_back(std::make_pair(lo2, hi2));
838*58b9f456SAndroid Build Coastguard Worker return Ranges(ranges);
839*58b9f456SAndroid Build Coastguard Worker }
840*58b9f456SAndroid Build Coastguard Worker
841*58b9f456SAndroid Build Coastguard Worker // Pass this benchmark object to *func, which can customize
842*58b9f456SAndroid Build Coastguard Worker // the benchmark by calling various methods like Arg, Args,
843*58b9f456SAndroid Build Coastguard Worker // Threads, etc.
844*58b9f456SAndroid Build Coastguard Worker Benchmark* Apply(void (*func)(Benchmark* benchmark));
845*58b9f456SAndroid Build Coastguard Worker
846*58b9f456SAndroid Build Coastguard Worker // Set the range multiplier for non-dense range. If not called, the range
847*58b9f456SAndroid Build Coastguard Worker // multiplier kRangeMultiplier will be used.
848*58b9f456SAndroid Build Coastguard Worker Benchmark* RangeMultiplier(int multiplier);
849*58b9f456SAndroid Build Coastguard Worker
850*58b9f456SAndroid Build Coastguard Worker // Set the minimum amount of time to use when running this benchmark. This
851*58b9f456SAndroid Build Coastguard Worker // option overrides the `benchmark_min_time` flag.
852*58b9f456SAndroid Build Coastguard Worker // REQUIRES: `t > 0` and `Iterations` has not been called on this benchmark.
853*58b9f456SAndroid Build Coastguard Worker Benchmark* MinTime(double t);
854*58b9f456SAndroid Build Coastguard Worker
855*58b9f456SAndroid Build Coastguard Worker // Specify the amount of iterations that should be run by this benchmark.
856*58b9f456SAndroid Build Coastguard Worker // REQUIRES: 'n > 0' and `MinTime` has not been called on this benchmark.
857*58b9f456SAndroid Build Coastguard Worker //
858*58b9f456SAndroid Build Coastguard Worker // NOTE: This function should only be used when *exact* iteration control is
859*58b9f456SAndroid Build Coastguard Worker // needed and never to control or limit how long a benchmark runs, where
860*58b9f456SAndroid Build Coastguard Worker // `--benchmark_min_time=N` or `MinTime(...)` should be used instead.
861*58b9f456SAndroid Build Coastguard Worker Benchmark* Iterations(size_t n);
862*58b9f456SAndroid Build Coastguard Worker
863*58b9f456SAndroid Build Coastguard Worker // Specify the amount of times to repeat this benchmark. This option overrides
864*58b9f456SAndroid Build Coastguard Worker // the `benchmark_repetitions` flag.
865*58b9f456SAndroid Build Coastguard Worker // REQUIRES: `n > 0`
866*58b9f456SAndroid Build Coastguard Worker Benchmark* Repetitions(int n);
867*58b9f456SAndroid Build Coastguard Worker
868*58b9f456SAndroid Build Coastguard Worker // Specify if each repetition of the benchmark should be reported separately
869*58b9f456SAndroid Build Coastguard Worker // or if only the final statistics should be reported. If the benchmark
870*58b9f456SAndroid Build Coastguard Worker // is not repeated then the single result is always reported.
871*58b9f456SAndroid Build Coastguard Worker // Applies to *ALL* reporters (display and file).
872*58b9f456SAndroid Build Coastguard Worker Benchmark* ReportAggregatesOnly(bool value = true);
873*58b9f456SAndroid Build Coastguard Worker
874*58b9f456SAndroid Build Coastguard Worker // Same as ReportAggregatesOnly(), but applies to display reporter only.
875*58b9f456SAndroid Build Coastguard Worker Benchmark* DisplayAggregatesOnly(bool value = true);
876*58b9f456SAndroid Build Coastguard Worker
877*58b9f456SAndroid Build Coastguard Worker // If a particular benchmark is I/O bound, runs multiple threads internally or
878*58b9f456SAndroid Build Coastguard Worker // if for some reason CPU timings are not representative, call this method. If
879*58b9f456SAndroid Build Coastguard Worker // called, the elapsed time will be used to control how many iterations are
880*58b9f456SAndroid Build Coastguard Worker // run, and in the printing of items/second or MB/seconds values. If not
881*58b9f456SAndroid Build Coastguard Worker // called, the cpu time used by the benchmark will be used.
882*58b9f456SAndroid Build Coastguard Worker Benchmark* UseRealTime();
883*58b9f456SAndroid Build Coastguard Worker
884*58b9f456SAndroid Build Coastguard Worker // If a benchmark must measure time manually (e.g. if GPU execution time is
885*58b9f456SAndroid Build Coastguard Worker // being
886*58b9f456SAndroid Build Coastguard Worker // measured), call this method. If called, each benchmark iteration should
887*58b9f456SAndroid Build Coastguard Worker // call
888*58b9f456SAndroid Build Coastguard Worker // SetIterationTime(seconds) to report the measured time, which will be used
889*58b9f456SAndroid Build Coastguard Worker // to control how many iterations are run, and in the printing of items/second
890*58b9f456SAndroid Build Coastguard Worker // or MB/second values.
891*58b9f456SAndroid Build Coastguard Worker Benchmark* UseManualTime();
892*58b9f456SAndroid Build Coastguard Worker
893*58b9f456SAndroid Build Coastguard Worker // Set the asymptotic computational complexity for the benchmark. If called
894*58b9f456SAndroid Build Coastguard Worker // the asymptotic computational complexity will be shown on the output.
895*58b9f456SAndroid Build Coastguard Worker Benchmark* Complexity(BigO complexity = benchmark::oAuto);
896*58b9f456SAndroid Build Coastguard Worker
897*58b9f456SAndroid Build Coastguard Worker // Set the asymptotic computational complexity for the benchmark. If called
898*58b9f456SAndroid Build Coastguard Worker // the asymptotic computational complexity will be shown on the output.
899*58b9f456SAndroid Build Coastguard Worker Benchmark* Complexity(BigOFunc* complexity);
900*58b9f456SAndroid Build Coastguard Worker
901*58b9f456SAndroid Build Coastguard Worker // Add this statistics to be computed over all the values of benchmark run
902*58b9f456SAndroid Build Coastguard Worker Benchmark* ComputeStatistics(std::string name, StatisticsFunc* statistics);
903*58b9f456SAndroid Build Coastguard Worker
904*58b9f456SAndroid Build Coastguard Worker // Support for running multiple copies of the same benchmark concurrently
905*58b9f456SAndroid Build Coastguard Worker // in multiple threads. This may be useful when measuring the scaling
906*58b9f456SAndroid Build Coastguard Worker // of some piece of code.
907*58b9f456SAndroid Build Coastguard Worker
908*58b9f456SAndroid Build Coastguard Worker // Run one instance of this benchmark concurrently in t threads.
909*58b9f456SAndroid Build Coastguard Worker Benchmark* Threads(int t);
910*58b9f456SAndroid Build Coastguard Worker
911*58b9f456SAndroid Build Coastguard Worker // Pick a set of values T from [min_threads,max_threads].
912*58b9f456SAndroid Build Coastguard Worker // min_threads and max_threads are always included in T. Run this
913*58b9f456SAndroid Build Coastguard Worker // benchmark once for each value in T. The benchmark run for a
914*58b9f456SAndroid Build Coastguard Worker // particular value t consists of t threads running the benchmark
915*58b9f456SAndroid Build Coastguard Worker // function concurrently. For example, consider:
916*58b9f456SAndroid Build Coastguard Worker // BENCHMARK(Foo)->ThreadRange(1,16);
917*58b9f456SAndroid Build Coastguard Worker // This will run the following benchmarks:
918*58b9f456SAndroid Build Coastguard Worker // Foo in 1 thread
919*58b9f456SAndroid Build Coastguard Worker // Foo in 2 threads
920*58b9f456SAndroid Build Coastguard Worker // Foo in 4 threads
921*58b9f456SAndroid Build Coastguard Worker // Foo in 8 threads
922*58b9f456SAndroid Build Coastguard Worker // Foo in 16 threads
923*58b9f456SAndroid Build Coastguard Worker Benchmark* ThreadRange(int min_threads, int max_threads);
924*58b9f456SAndroid Build Coastguard Worker
925*58b9f456SAndroid Build Coastguard Worker // For each value n in the range, run this benchmark once using n threads.
926*58b9f456SAndroid Build Coastguard Worker // min_threads and max_threads are always included in the range.
927*58b9f456SAndroid Build Coastguard Worker // stride specifies the increment. E.g. DenseThreadRange(1, 8, 3) starts
928*58b9f456SAndroid Build Coastguard Worker // a benchmark with 1, 4, 7 and 8 threads.
929*58b9f456SAndroid Build Coastguard Worker Benchmark* DenseThreadRange(int min_threads, int max_threads, int stride = 1);
930*58b9f456SAndroid Build Coastguard Worker
931*58b9f456SAndroid Build Coastguard Worker // Equivalent to ThreadRange(NumCPUs(), NumCPUs())
932*58b9f456SAndroid Build Coastguard Worker Benchmark* ThreadPerCpu();
933*58b9f456SAndroid Build Coastguard Worker
934*58b9f456SAndroid Build Coastguard Worker virtual void Run(State& state) = 0;
935*58b9f456SAndroid Build Coastguard Worker
936*58b9f456SAndroid Build Coastguard Worker protected:
937*58b9f456SAndroid Build Coastguard Worker explicit Benchmark(const char* name);
938*58b9f456SAndroid Build Coastguard Worker Benchmark(Benchmark const&);
939*58b9f456SAndroid Build Coastguard Worker void SetName(const char* name);
940*58b9f456SAndroid Build Coastguard Worker
941*58b9f456SAndroid Build Coastguard Worker int ArgsCnt() const;
942*58b9f456SAndroid Build Coastguard Worker
943*58b9f456SAndroid Build Coastguard Worker private:
944*58b9f456SAndroid Build Coastguard Worker friend class BenchmarkFamilies;
945*58b9f456SAndroid Build Coastguard Worker
946*58b9f456SAndroid Build Coastguard Worker std::string name_;
947*58b9f456SAndroid Build Coastguard Worker AggregationReportMode aggregation_report_mode_;
948*58b9f456SAndroid Build Coastguard Worker std::vector<std::string> arg_names_; // Args for all benchmark runs
949*58b9f456SAndroid Build Coastguard Worker std::vector<std::vector<int64_t> > args_; // Args for all benchmark runs
950*58b9f456SAndroid Build Coastguard Worker TimeUnit time_unit_;
951*58b9f456SAndroid Build Coastguard Worker int range_multiplier_;
952*58b9f456SAndroid Build Coastguard Worker double min_time_;
953*58b9f456SAndroid Build Coastguard Worker size_t iterations_;
954*58b9f456SAndroid Build Coastguard Worker int repetitions_;
955*58b9f456SAndroid Build Coastguard Worker bool use_real_time_;
956*58b9f456SAndroid Build Coastguard Worker bool use_manual_time_;
957*58b9f456SAndroid Build Coastguard Worker BigO complexity_;
958*58b9f456SAndroid Build Coastguard Worker BigOFunc* complexity_lambda_;
959*58b9f456SAndroid Build Coastguard Worker std::vector<Statistics> statistics_;
960*58b9f456SAndroid Build Coastguard Worker std::vector<int> thread_counts_;
961*58b9f456SAndroid Build Coastguard Worker
962*58b9f456SAndroid Build Coastguard Worker Benchmark& operator=(Benchmark const&);
963*58b9f456SAndroid Build Coastguard Worker };
964*58b9f456SAndroid Build Coastguard Worker
965*58b9f456SAndroid Build Coastguard Worker } // namespace internal
966*58b9f456SAndroid Build Coastguard Worker
967*58b9f456SAndroid Build Coastguard Worker // Create and register a benchmark with the specified 'name' that invokes
968*58b9f456SAndroid Build Coastguard Worker // the specified functor 'fn'.
969*58b9f456SAndroid Build Coastguard Worker //
970*58b9f456SAndroid Build Coastguard Worker // RETURNS: A pointer to the registered benchmark.
971*58b9f456SAndroid Build Coastguard Worker internal::Benchmark* RegisterBenchmark(const char* name,
972*58b9f456SAndroid Build Coastguard Worker internal::Function* fn);
973*58b9f456SAndroid Build Coastguard Worker
974*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_HAS_CXX11)
975*58b9f456SAndroid Build Coastguard Worker template <class Lambda>
976*58b9f456SAndroid Build Coastguard Worker internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn);
977*58b9f456SAndroid Build Coastguard Worker #endif
978*58b9f456SAndroid Build Coastguard Worker
979*58b9f456SAndroid Build Coastguard Worker // Remove all registered benchmarks. All pointers to previously registered
980*58b9f456SAndroid Build Coastguard Worker // benchmarks are invalidated.
981*58b9f456SAndroid Build Coastguard Worker void ClearRegisteredBenchmarks();
982*58b9f456SAndroid Build Coastguard Worker
983*58b9f456SAndroid Build Coastguard Worker namespace internal {
984*58b9f456SAndroid Build Coastguard Worker // The class used to hold all Benchmarks created from static function.
985*58b9f456SAndroid Build Coastguard Worker // (ie those created using the BENCHMARK(...) macros.
986*58b9f456SAndroid Build Coastguard Worker class FunctionBenchmark : public Benchmark {
987*58b9f456SAndroid Build Coastguard Worker public:
FunctionBenchmark(const char * name,Function * func)988*58b9f456SAndroid Build Coastguard Worker FunctionBenchmark(const char* name, Function* func)
989*58b9f456SAndroid Build Coastguard Worker : Benchmark(name), func_(func) {}
990*58b9f456SAndroid Build Coastguard Worker
991*58b9f456SAndroid Build Coastguard Worker virtual void Run(State& st);
992*58b9f456SAndroid Build Coastguard Worker
993*58b9f456SAndroid Build Coastguard Worker private:
994*58b9f456SAndroid Build Coastguard Worker Function* func_;
995*58b9f456SAndroid Build Coastguard Worker };
996*58b9f456SAndroid Build Coastguard Worker
997*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_HAS_CXX11
998*58b9f456SAndroid Build Coastguard Worker template <class Lambda>
999*58b9f456SAndroid Build Coastguard Worker class LambdaBenchmark : public Benchmark {
1000*58b9f456SAndroid Build Coastguard Worker public:
Run(State & st)1001*58b9f456SAndroid Build Coastguard Worker virtual void Run(State& st) { lambda_(st); }
1002*58b9f456SAndroid Build Coastguard Worker
1003*58b9f456SAndroid Build Coastguard Worker private:
1004*58b9f456SAndroid Build Coastguard Worker template <class OLambda>
LambdaBenchmark(const char * name,OLambda && lam)1005*58b9f456SAndroid Build Coastguard Worker LambdaBenchmark(const char* name, OLambda&& lam)
1006*58b9f456SAndroid Build Coastguard Worker : Benchmark(name), lambda_(std::forward<OLambda>(lam)) {}
1007*58b9f456SAndroid Build Coastguard Worker
1008*58b9f456SAndroid Build Coastguard Worker LambdaBenchmark(LambdaBenchmark const&) = delete;
1009*58b9f456SAndroid Build Coastguard Worker
1010*58b9f456SAndroid Build Coastguard Worker private:
1011*58b9f456SAndroid Build Coastguard Worker template <class Lam>
1012*58b9f456SAndroid Build Coastguard Worker friend Benchmark* ::benchmark::RegisterBenchmark(const char*, Lam&&);
1013*58b9f456SAndroid Build Coastguard Worker
1014*58b9f456SAndroid Build Coastguard Worker Lambda lambda_;
1015*58b9f456SAndroid Build Coastguard Worker };
1016*58b9f456SAndroid Build Coastguard Worker #endif
1017*58b9f456SAndroid Build Coastguard Worker
1018*58b9f456SAndroid Build Coastguard Worker } // namespace internal
1019*58b9f456SAndroid Build Coastguard Worker
RegisterBenchmark(const char * name,internal::Function * fn)1020*58b9f456SAndroid Build Coastguard Worker inline internal::Benchmark* RegisterBenchmark(const char* name,
1021*58b9f456SAndroid Build Coastguard Worker internal::Function* fn) {
1022*58b9f456SAndroid Build Coastguard Worker return internal::RegisterBenchmarkInternal(
1023*58b9f456SAndroid Build Coastguard Worker ::new internal::FunctionBenchmark(name, fn));
1024*58b9f456SAndroid Build Coastguard Worker }
1025*58b9f456SAndroid Build Coastguard Worker
1026*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_HAS_CXX11
1027*58b9f456SAndroid Build Coastguard Worker template <class Lambda>
RegisterBenchmark(const char * name,Lambda && fn)1028*58b9f456SAndroid Build Coastguard Worker internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn) {
1029*58b9f456SAndroid Build Coastguard Worker using BenchType =
1030*58b9f456SAndroid Build Coastguard Worker internal::LambdaBenchmark<typename std::decay<Lambda>::type>;
1031*58b9f456SAndroid Build Coastguard Worker return internal::RegisterBenchmarkInternal(
1032*58b9f456SAndroid Build Coastguard Worker ::new BenchType(name, std::forward<Lambda>(fn)));
1033*58b9f456SAndroid Build Coastguard Worker }
1034*58b9f456SAndroid Build Coastguard Worker #endif
1035*58b9f456SAndroid Build Coastguard Worker
1036*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_HAS_CXX11) && \
1037*58b9f456SAndroid Build Coastguard Worker (!defined(BENCHMARK_GCC_VERSION) || BENCHMARK_GCC_VERSION >= 409)
1038*58b9f456SAndroid Build Coastguard Worker template <class Lambda, class... Args>
RegisterBenchmark(const char * name,Lambda && fn,Args &&...args)1039*58b9f456SAndroid Build Coastguard Worker internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn,
1040*58b9f456SAndroid Build Coastguard Worker Args&&... args) {
1041*58b9f456SAndroid Build Coastguard Worker return benchmark::RegisterBenchmark(
1042*58b9f456SAndroid Build Coastguard Worker name, [=](benchmark::State& st) { fn(st, args...); });
1043*58b9f456SAndroid Build Coastguard Worker }
1044*58b9f456SAndroid Build Coastguard Worker #else
1045*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
1046*58b9f456SAndroid Build Coastguard Worker #endif
1047*58b9f456SAndroid Build Coastguard Worker
1048*58b9f456SAndroid Build Coastguard Worker // The base class for all fixture tests.
1049*58b9f456SAndroid Build Coastguard Worker class Fixture : public internal::Benchmark {
1050*58b9f456SAndroid Build Coastguard Worker public:
Fixture()1051*58b9f456SAndroid Build Coastguard Worker Fixture() : internal::Benchmark("") {}
1052*58b9f456SAndroid Build Coastguard Worker
Run(State & st)1053*58b9f456SAndroid Build Coastguard Worker virtual void Run(State& st) {
1054*58b9f456SAndroid Build Coastguard Worker this->SetUp(st);
1055*58b9f456SAndroid Build Coastguard Worker this->BenchmarkCase(st);
1056*58b9f456SAndroid Build Coastguard Worker this->TearDown(st);
1057*58b9f456SAndroid Build Coastguard Worker }
1058*58b9f456SAndroid Build Coastguard Worker
1059*58b9f456SAndroid Build Coastguard Worker // These will be deprecated ...
SetUp(const State &)1060*58b9f456SAndroid Build Coastguard Worker virtual void SetUp(const State&) {}
TearDown(const State &)1061*58b9f456SAndroid Build Coastguard Worker virtual void TearDown(const State&) {}
1062*58b9f456SAndroid Build Coastguard Worker // ... In favor of these.
SetUp(State & st)1063*58b9f456SAndroid Build Coastguard Worker virtual void SetUp(State& st) { SetUp(const_cast<const State&>(st)); }
TearDown(State & st)1064*58b9f456SAndroid Build Coastguard Worker virtual void TearDown(State& st) { TearDown(const_cast<const State&>(st)); }
1065*58b9f456SAndroid Build Coastguard Worker
1066*58b9f456SAndroid Build Coastguard Worker protected:
1067*58b9f456SAndroid Build Coastguard Worker virtual void BenchmarkCase(State&) = 0;
1068*58b9f456SAndroid Build Coastguard Worker };
1069*58b9f456SAndroid Build Coastguard Worker
1070*58b9f456SAndroid Build Coastguard Worker } // namespace benchmark
1071*58b9f456SAndroid Build Coastguard Worker
1072*58b9f456SAndroid Build Coastguard Worker // ------------------------------------------------------
1073*58b9f456SAndroid Build Coastguard Worker // Macro to register benchmarks
1074*58b9f456SAndroid Build Coastguard Worker
1075*58b9f456SAndroid Build Coastguard Worker // Check that __COUNTER__ is defined and that __COUNTER__ increases by 1
1076*58b9f456SAndroid Build Coastguard Worker // every time it is expanded. X + 1 == X + 0 is used in case X is defined to be
1077*58b9f456SAndroid Build Coastguard Worker // empty. If X is empty the expression becomes (+1 == +0).
1078*58b9f456SAndroid Build Coastguard Worker #if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
1079*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
1080*58b9f456SAndroid Build Coastguard Worker #else
1081*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
1082*58b9f456SAndroid Build Coastguard Worker #endif
1083*58b9f456SAndroid Build Coastguard Worker
1084*58b9f456SAndroid Build Coastguard Worker // Helpers for generating unique variable names
1085*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_PRIVATE_NAME(n) \
1086*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_CONCAT(_benchmark_, BENCHMARK_PRIVATE_UNIQUE_ID, n)
1087*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
1088*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
1089*58b9f456SAndroid Build Coastguard Worker
1090*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_PRIVATE_DECLARE(n) \
1091*58b9f456SAndroid Build Coastguard Worker static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \
1092*58b9f456SAndroid Build Coastguard Worker BENCHMARK_UNUSED
1093*58b9f456SAndroid Build Coastguard Worker
1094*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK(n) \
1095*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_DECLARE(n) = \
1096*58b9f456SAndroid Build Coastguard Worker (::benchmark::internal::RegisterBenchmarkInternal( \
1097*58b9f456SAndroid Build Coastguard Worker new ::benchmark::internal::FunctionBenchmark(#n, n)))
1098*58b9f456SAndroid Build Coastguard Worker
1099*58b9f456SAndroid Build Coastguard Worker // Old-style macros
1100*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
1101*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->Args({(a1), (a2)})
1102*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t))
1103*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
1104*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
1105*58b9f456SAndroid Build Coastguard Worker BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
1106*58b9f456SAndroid Build Coastguard Worker
1107*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_HAS_CXX11
1108*58b9f456SAndroid Build Coastguard Worker
1109*58b9f456SAndroid Build Coastguard Worker // Register a benchmark which invokes the function specified by `func`
1110*58b9f456SAndroid Build Coastguard Worker // with the additional arguments specified by `...`.
1111*58b9f456SAndroid Build Coastguard Worker //
1112*58b9f456SAndroid Build Coastguard Worker // For example:
1113*58b9f456SAndroid Build Coastguard Worker //
1114*58b9f456SAndroid Build Coastguard Worker // template <class ...ExtraArgs>`
1115*58b9f456SAndroid Build Coastguard Worker // void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) {
1116*58b9f456SAndroid Build Coastguard Worker // [...]
1117*58b9f456SAndroid Build Coastguard Worker //}
1118*58b9f456SAndroid Build Coastguard Worker // /* Registers a benchmark named "BM_takes_args/int_string_test` */
1119*58b9f456SAndroid Build Coastguard Worker // BENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc"));
1120*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_CAPTURE(func, test_case_name, ...) \
1121*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_DECLARE(func) = \
1122*58b9f456SAndroid Build Coastguard Worker (::benchmark::internal::RegisterBenchmarkInternal( \
1123*58b9f456SAndroid Build Coastguard Worker new ::benchmark::internal::FunctionBenchmark( \
1124*58b9f456SAndroid Build Coastguard Worker #func "/" #test_case_name, \
1125*58b9f456SAndroid Build Coastguard Worker [](::benchmark::State& st) { func(st, __VA_ARGS__); })))
1126*58b9f456SAndroid Build Coastguard Worker
1127*58b9f456SAndroid Build Coastguard Worker #endif // BENCHMARK_HAS_CXX11
1128*58b9f456SAndroid Build Coastguard Worker
1129*58b9f456SAndroid Build Coastguard Worker // This will register a benchmark for a templatized function. For example:
1130*58b9f456SAndroid Build Coastguard Worker //
1131*58b9f456SAndroid Build Coastguard Worker // template<int arg>
1132*58b9f456SAndroid Build Coastguard Worker // void BM_Foo(int iters);
1133*58b9f456SAndroid Build Coastguard Worker //
1134*58b9f456SAndroid Build Coastguard Worker // BENCHMARK_TEMPLATE(BM_Foo, 1);
1135*58b9f456SAndroid Build Coastguard Worker //
1136*58b9f456SAndroid Build Coastguard Worker // will register BM_Foo<1> as a benchmark.
1137*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE1(n, a) \
1138*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_DECLARE(n) = \
1139*58b9f456SAndroid Build Coastguard Worker (::benchmark::internal::RegisterBenchmarkInternal( \
1140*58b9f456SAndroid Build Coastguard Worker new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n<a>)))
1141*58b9f456SAndroid Build Coastguard Worker
1142*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE2(n, a, b) \
1143*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_DECLARE(n) = \
1144*58b9f456SAndroid Build Coastguard Worker (::benchmark::internal::RegisterBenchmarkInternal( \
1145*58b9f456SAndroid Build Coastguard Worker new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \
1146*58b9f456SAndroid Build Coastguard Worker n<a, b>)))
1147*58b9f456SAndroid Build Coastguard Worker
1148*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_HAS_CXX11
1149*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE(n, ...) \
1150*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_DECLARE(n) = \
1151*58b9f456SAndroid Build Coastguard Worker (::benchmark::internal::RegisterBenchmarkInternal( \
1152*58b9f456SAndroid Build Coastguard Worker new ::benchmark::internal::FunctionBenchmark( \
1153*58b9f456SAndroid Build Coastguard Worker #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
1154*58b9f456SAndroid Build Coastguard Worker #else
1155*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a)
1156*58b9f456SAndroid Build Coastguard Worker #endif
1157*58b9f456SAndroid Build Coastguard Worker
1158*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1159*58b9f456SAndroid Build Coastguard Worker class BaseClass##_##Method##_Benchmark : public BaseClass { \
1160*58b9f456SAndroid Build Coastguard Worker public: \
1161*58b9f456SAndroid Build Coastguard Worker BaseClass##_##Method##_Benchmark() : BaseClass() { \
1162*58b9f456SAndroid Build Coastguard Worker this->SetName(#BaseClass "/" #Method); \
1163*58b9f456SAndroid Build Coastguard Worker } \
1164*58b9f456SAndroid Build Coastguard Worker \
1165*58b9f456SAndroid Build Coastguard Worker protected: \
1166*58b9f456SAndroid Build Coastguard Worker virtual void BenchmarkCase(::benchmark::State&); \
1167*58b9f456SAndroid Build Coastguard Worker };
1168*58b9f456SAndroid Build Coastguard Worker
1169*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1170*58b9f456SAndroid Build Coastguard Worker class BaseClass##_##Method##_Benchmark : public BaseClass<a> { \
1171*58b9f456SAndroid Build Coastguard Worker public: \
1172*58b9f456SAndroid Build Coastguard Worker BaseClass##_##Method##_Benchmark() : BaseClass<a>() { \
1173*58b9f456SAndroid Build Coastguard Worker this->SetName(#BaseClass "<" #a ">/" #Method); \
1174*58b9f456SAndroid Build Coastguard Worker } \
1175*58b9f456SAndroid Build Coastguard Worker \
1176*58b9f456SAndroid Build Coastguard Worker protected: \
1177*58b9f456SAndroid Build Coastguard Worker virtual void BenchmarkCase(::benchmark::State&); \
1178*58b9f456SAndroid Build Coastguard Worker };
1179*58b9f456SAndroid Build Coastguard Worker
1180*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1181*58b9f456SAndroid Build Coastguard Worker class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> { \
1182*58b9f456SAndroid Build Coastguard Worker public: \
1183*58b9f456SAndroid Build Coastguard Worker BaseClass##_##Method##_Benchmark() : BaseClass<a, b>() { \
1184*58b9f456SAndroid Build Coastguard Worker this->SetName(#BaseClass "<" #a "," #b ">/" #Method); \
1185*58b9f456SAndroid Build Coastguard Worker } \
1186*58b9f456SAndroid Build Coastguard Worker \
1187*58b9f456SAndroid Build Coastguard Worker protected: \
1188*58b9f456SAndroid Build Coastguard Worker virtual void BenchmarkCase(::benchmark::State&); \
1189*58b9f456SAndroid Build Coastguard Worker };
1190*58b9f456SAndroid Build Coastguard Worker
1191*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_HAS_CXX11
1192*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...) \
1193*58b9f456SAndroid Build Coastguard Worker class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \
1194*58b9f456SAndroid Build Coastguard Worker public: \
1195*58b9f456SAndroid Build Coastguard Worker BaseClass##_##Method##_Benchmark() : BaseClass<__VA_ARGS__>() { \
1196*58b9f456SAndroid Build Coastguard Worker this->SetName(#BaseClass "<" #__VA_ARGS__ ">/" #Method); \
1197*58b9f456SAndroid Build Coastguard Worker } \
1198*58b9f456SAndroid Build Coastguard Worker \
1199*58b9f456SAndroid Build Coastguard Worker protected: \
1200*58b9f456SAndroid Build Coastguard Worker virtual void BenchmarkCase(::benchmark::State&); \
1201*58b9f456SAndroid Build Coastguard Worker };
1202*58b9f456SAndroid Build Coastguard Worker #else
1203*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(n, a) \
1204*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(n, a)
1205*58b9f456SAndroid Build Coastguard Worker #endif
1206*58b9f456SAndroid Build Coastguard Worker
1207*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_DEFINE_F(BaseClass, Method) \
1208*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1209*58b9f456SAndroid Build Coastguard Worker void BaseClass##_##Method##_Benchmark::BenchmarkCase
1210*58b9f456SAndroid Build Coastguard Worker
1211*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a) \
1212*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1213*58b9f456SAndroid Build Coastguard Worker void BaseClass##_##Method##_Benchmark::BenchmarkCase
1214*58b9f456SAndroid Build Coastguard Worker
1215*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE2_DEFINE_F(BaseClass, Method, a, b) \
1216*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1217*58b9f456SAndroid Build Coastguard Worker void BaseClass##_##Method##_Benchmark::BenchmarkCase
1218*58b9f456SAndroid Build Coastguard Worker
1219*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_HAS_CXX11
1220*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, ...) \
1221*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1222*58b9f456SAndroid Build Coastguard Worker void BaseClass##_##Method##_Benchmark::BenchmarkCase
1223*58b9f456SAndroid Build Coastguard Worker #else
1224*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, a) \
1225*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a)
1226*58b9f456SAndroid Build Coastguard Worker #endif
1227*58b9f456SAndroid Build Coastguard Worker
1228*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_REGISTER_F(BaseClass, Method) \
1229*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_REGISTER_F(BaseClass##_##Method##_Benchmark)
1230*58b9f456SAndroid Build Coastguard Worker
1231*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_PRIVATE_REGISTER_F(TestName) \
1232*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_DECLARE(TestName) = \
1233*58b9f456SAndroid Build Coastguard Worker (::benchmark::internal::RegisterBenchmarkInternal(new TestName()))
1234*58b9f456SAndroid Build Coastguard Worker
1235*58b9f456SAndroid Build Coastguard Worker // This macro will define and register a benchmark within a fixture class.
1236*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_F(BaseClass, Method) \
1237*58b9f456SAndroid Build Coastguard Worker BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1238*58b9f456SAndroid Build Coastguard Worker BENCHMARK_REGISTER_F(BaseClass, Method); \
1239*58b9f456SAndroid Build Coastguard Worker void BaseClass##_##Method##_Benchmark::BenchmarkCase
1240*58b9f456SAndroid Build Coastguard Worker
1241*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE1_F(BaseClass, Method, a) \
1242*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1243*58b9f456SAndroid Build Coastguard Worker BENCHMARK_REGISTER_F(BaseClass, Method); \
1244*58b9f456SAndroid Build Coastguard Worker void BaseClass##_##Method##_Benchmark::BenchmarkCase
1245*58b9f456SAndroid Build Coastguard Worker
1246*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE2_F(BaseClass, Method, a, b) \
1247*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1248*58b9f456SAndroid Build Coastguard Worker BENCHMARK_REGISTER_F(BaseClass, Method); \
1249*58b9f456SAndroid Build Coastguard Worker void BaseClass##_##Method##_Benchmark::BenchmarkCase
1250*58b9f456SAndroid Build Coastguard Worker
1251*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_HAS_CXX11
1252*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE_F(BaseClass, Method, ...) \
1253*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1254*58b9f456SAndroid Build Coastguard Worker BENCHMARK_REGISTER_F(BaseClass, Method); \
1255*58b9f456SAndroid Build Coastguard Worker void BaseClass##_##Method##_Benchmark::BenchmarkCase
1256*58b9f456SAndroid Build Coastguard Worker #else
1257*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_TEMPLATE_F(BaseClass, Method, a) \
1258*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE1_F(BaseClass, Method, a)
1259*58b9f456SAndroid Build Coastguard Worker #endif
1260*58b9f456SAndroid Build Coastguard Worker
1261*58b9f456SAndroid Build Coastguard Worker // Helper macro to create a main routine in a test that runs the benchmarks
1262*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_MAIN() \
1263*58b9f456SAndroid Build Coastguard Worker int main(int argc, char** argv) { \
1264*58b9f456SAndroid Build Coastguard Worker ::benchmark::Initialize(&argc, argv); \
1265*58b9f456SAndroid Build Coastguard Worker if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \
1266*58b9f456SAndroid Build Coastguard Worker ::benchmark::RunSpecifiedBenchmarks(); \
1267*58b9f456SAndroid Build Coastguard Worker } \
1268*58b9f456SAndroid Build Coastguard Worker int main(int, char**)
1269*58b9f456SAndroid Build Coastguard Worker
1270*58b9f456SAndroid Build Coastguard Worker // ------------------------------------------------------
1271*58b9f456SAndroid Build Coastguard Worker // Benchmark Reporters
1272*58b9f456SAndroid Build Coastguard Worker
1273*58b9f456SAndroid Build Coastguard Worker namespace benchmark {
1274*58b9f456SAndroid Build Coastguard Worker
1275*58b9f456SAndroid Build Coastguard Worker struct CPUInfo {
1276*58b9f456SAndroid Build Coastguard Worker struct CacheInfo {
1277*58b9f456SAndroid Build Coastguard Worker std::string type;
1278*58b9f456SAndroid Build Coastguard Worker int level;
1279*58b9f456SAndroid Build Coastguard Worker int size;
1280*58b9f456SAndroid Build Coastguard Worker int num_sharing;
1281*58b9f456SAndroid Build Coastguard Worker };
1282*58b9f456SAndroid Build Coastguard Worker
1283*58b9f456SAndroid Build Coastguard Worker int num_cpus;
1284*58b9f456SAndroid Build Coastguard Worker double cycles_per_second;
1285*58b9f456SAndroid Build Coastguard Worker std::vector<CacheInfo> caches;
1286*58b9f456SAndroid Build Coastguard Worker bool scaling_enabled;
1287*58b9f456SAndroid Build Coastguard Worker std::vector<double> load_avg;
1288*58b9f456SAndroid Build Coastguard Worker
1289*58b9f456SAndroid Build Coastguard Worker static const CPUInfo& Get();
1290*58b9f456SAndroid Build Coastguard Worker
1291*58b9f456SAndroid Build Coastguard Worker private:
1292*58b9f456SAndroid Build Coastguard Worker CPUInfo();
1293*58b9f456SAndroid Build Coastguard Worker BENCHMARK_DISALLOW_COPY_AND_ASSIGN(CPUInfo);
1294*58b9f456SAndroid Build Coastguard Worker };
1295*58b9f456SAndroid Build Coastguard Worker
1296*58b9f456SAndroid Build Coastguard Worker //Adding Struct for System Information
1297*58b9f456SAndroid Build Coastguard Worker struct SystemInfo {
1298*58b9f456SAndroid Build Coastguard Worker std::string name;
1299*58b9f456SAndroid Build Coastguard Worker static const SystemInfo& Get();
1300*58b9f456SAndroid Build Coastguard Worker private:
1301*58b9f456SAndroid Build Coastguard Worker SystemInfo();
1302*58b9f456SAndroid Build Coastguard Worker BENCHMARK_DISALLOW_COPY_AND_ASSIGN(SystemInfo);
1303*58b9f456SAndroid Build Coastguard Worker };
1304*58b9f456SAndroid Build Coastguard Worker
1305*58b9f456SAndroid Build Coastguard Worker // Interface for custom benchmark result printers.
1306*58b9f456SAndroid Build Coastguard Worker // By default, benchmark reports are printed to stdout. However an application
1307*58b9f456SAndroid Build Coastguard Worker // can control the destination of the reports by calling
1308*58b9f456SAndroid Build Coastguard Worker // RunSpecifiedBenchmarks and passing it a custom reporter object.
1309*58b9f456SAndroid Build Coastguard Worker // The reporter object must implement the following interface.
1310*58b9f456SAndroid Build Coastguard Worker class BenchmarkReporter {
1311*58b9f456SAndroid Build Coastguard Worker public:
1312*58b9f456SAndroid Build Coastguard Worker struct Context {
1313*58b9f456SAndroid Build Coastguard Worker CPUInfo const& cpu_info;
1314*58b9f456SAndroid Build Coastguard Worker SystemInfo const& sys_info;
1315*58b9f456SAndroid Build Coastguard Worker // The number of chars in the longest benchmark name.
1316*58b9f456SAndroid Build Coastguard Worker size_t name_field_width;
1317*58b9f456SAndroid Build Coastguard Worker static const char* executable_name;
1318*58b9f456SAndroid Build Coastguard Worker Context();
1319*58b9f456SAndroid Build Coastguard Worker };
1320*58b9f456SAndroid Build Coastguard Worker
1321*58b9f456SAndroid Build Coastguard Worker struct Run {
1322*58b9f456SAndroid Build Coastguard Worker enum RunType { RT_Iteration, RT_Aggregate };
1323*58b9f456SAndroid Build Coastguard Worker
RunRun1324*58b9f456SAndroid Build Coastguard Worker Run()
1325*58b9f456SAndroid Build Coastguard Worker : run_type(RT_Iteration),
1326*58b9f456SAndroid Build Coastguard Worker error_occurred(false),
1327*58b9f456SAndroid Build Coastguard Worker iterations(1),
1328*58b9f456SAndroid Build Coastguard Worker time_unit(kNanosecond),
1329*58b9f456SAndroid Build Coastguard Worker real_accumulated_time(0),
1330*58b9f456SAndroid Build Coastguard Worker cpu_accumulated_time(0),
1331*58b9f456SAndroid Build Coastguard Worker max_heapbytes_used(0),
1332*58b9f456SAndroid Build Coastguard Worker complexity(oNone),
1333*58b9f456SAndroid Build Coastguard Worker complexity_lambda(),
1334*58b9f456SAndroid Build Coastguard Worker complexity_n(0),
1335*58b9f456SAndroid Build Coastguard Worker report_big_o(false),
1336*58b9f456SAndroid Build Coastguard Worker report_rms(false),
1337*58b9f456SAndroid Build Coastguard Worker counters(),
1338*58b9f456SAndroid Build Coastguard Worker has_memory_result(false),
1339*58b9f456SAndroid Build Coastguard Worker allocs_per_iter(0.0),
1340*58b9f456SAndroid Build Coastguard Worker max_bytes_used(0) {}
1341*58b9f456SAndroid Build Coastguard Worker
1342*58b9f456SAndroid Build Coastguard Worker std::string benchmark_name() const;
1343*58b9f456SAndroid Build Coastguard Worker std::string run_name;
1344*58b9f456SAndroid Build Coastguard Worker RunType run_type; // is this a measurement, or an aggregate?
1345*58b9f456SAndroid Build Coastguard Worker std::string aggregate_name;
1346*58b9f456SAndroid Build Coastguard Worker std::string report_label; // Empty if not set by benchmark.
1347*58b9f456SAndroid Build Coastguard Worker bool error_occurred;
1348*58b9f456SAndroid Build Coastguard Worker std::string error_message;
1349*58b9f456SAndroid Build Coastguard Worker
1350*58b9f456SAndroid Build Coastguard Worker int64_t iterations;
1351*58b9f456SAndroid Build Coastguard Worker TimeUnit time_unit;
1352*58b9f456SAndroid Build Coastguard Worker double real_accumulated_time;
1353*58b9f456SAndroid Build Coastguard Worker double cpu_accumulated_time;
1354*58b9f456SAndroid Build Coastguard Worker
1355*58b9f456SAndroid Build Coastguard Worker // Return a value representing the real time per iteration in the unit
1356*58b9f456SAndroid Build Coastguard Worker // specified by 'time_unit'.
1357*58b9f456SAndroid Build Coastguard Worker // NOTE: If 'iterations' is zero the returned value represents the
1358*58b9f456SAndroid Build Coastguard Worker // accumulated time.
1359*58b9f456SAndroid Build Coastguard Worker double GetAdjustedRealTime() const;
1360*58b9f456SAndroid Build Coastguard Worker
1361*58b9f456SAndroid Build Coastguard Worker // Return a value representing the cpu time per iteration in the unit
1362*58b9f456SAndroid Build Coastguard Worker // specified by 'time_unit'.
1363*58b9f456SAndroid Build Coastguard Worker // NOTE: If 'iterations' is zero the returned value represents the
1364*58b9f456SAndroid Build Coastguard Worker // accumulated time.
1365*58b9f456SAndroid Build Coastguard Worker double GetAdjustedCPUTime() const;
1366*58b9f456SAndroid Build Coastguard Worker
1367*58b9f456SAndroid Build Coastguard Worker // This is set to 0.0 if memory tracing is not enabled.
1368*58b9f456SAndroid Build Coastguard Worker double max_heapbytes_used;
1369*58b9f456SAndroid Build Coastguard Worker
1370*58b9f456SAndroid Build Coastguard Worker // Keep track of arguments to compute asymptotic complexity
1371*58b9f456SAndroid Build Coastguard Worker BigO complexity;
1372*58b9f456SAndroid Build Coastguard Worker BigOFunc* complexity_lambda;
1373*58b9f456SAndroid Build Coastguard Worker int64_t complexity_n;
1374*58b9f456SAndroid Build Coastguard Worker
1375*58b9f456SAndroid Build Coastguard Worker // what statistics to compute from the measurements
1376*58b9f456SAndroid Build Coastguard Worker const std::vector<Statistics>* statistics;
1377*58b9f456SAndroid Build Coastguard Worker
1378*58b9f456SAndroid Build Coastguard Worker // Inform print function whether the current run is a complexity report
1379*58b9f456SAndroid Build Coastguard Worker bool report_big_o;
1380*58b9f456SAndroid Build Coastguard Worker bool report_rms;
1381*58b9f456SAndroid Build Coastguard Worker
1382*58b9f456SAndroid Build Coastguard Worker UserCounters counters;
1383*58b9f456SAndroid Build Coastguard Worker
1384*58b9f456SAndroid Build Coastguard Worker // Memory metrics.
1385*58b9f456SAndroid Build Coastguard Worker bool has_memory_result;
1386*58b9f456SAndroid Build Coastguard Worker double allocs_per_iter;
1387*58b9f456SAndroid Build Coastguard Worker int64_t max_bytes_used;
1388*58b9f456SAndroid Build Coastguard Worker };
1389*58b9f456SAndroid Build Coastguard Worker
1390*58b9f456SAndroid Build Coastguard Worker // Construct a BenchmarkReporter with the output stream set to 'std::cout'
1391*58b9f456SAndroid Build Coastguard Worker // and the error stream set to 'std::cerr'
1392*58b9f456SAndroid Build Coastguard Worker BenchmarkReporter();
1393*58b9f456SAndroid Build Coastguard Worker
1394*58b9f456SAndroid Build Coastguard Worker // Called once for every suite of benchmarks run.
1395*58b9f456SAndroid Build Coastguard Worker // The parameter "context" contains information that the
1396*58b9f456SAndroid Build Coastguard Worker // reporter may wish to use when generating its report, for example the
1397*58b9f456SAndroid Build Coastguard Worker // platform under which the benchmarks are running. The benchmark run is
1398*58b9f456SAndroid Build Coastguard Worker // never started if this function returns false, allowing the reporter
1399*58b9f456SAndroid Build Coastguard Worker // to skip runs based on the context information.
1400*58b9f456SAndroid Build Coastguard Worker virtual bool ReportContext(const Context& context) = 0;
1401*58b9f456SAndroid Build Coastguard Worker
1402*58b9f456SAndroid Build Coastguard Worker // Called once for each group of benchmark runs, gives information about
1403*58b9f456SAndroid Build Coastguard Worker // cpu-time and heap memory usage during the benchmark run. If the group
1404*58b9f456SAndroid Build Coastguard Worker // of runs contained more than two entries then 'report' contains additional
1405*58b9f456SAndroid Build Coastguard Worker // elements representing the mean and standard deviation of those runs.
1406*58b9f456SAndroid Build Coastguard Worker // Additionally if this group of runs was the last in a family of benchmarks
1407*58b9f456SAndroid Build Coastguard Worker // 'reports' contains additional entries representing the asymptotic
1408*58b9f456SAndroid Build Coastguard Worker // complexity and RMS of that benchmark family.
1409*58b9f456SAndroid Build Coastguard Worker virtual void ReportRuns(const std::vector<Run>& report) = 0;
1410*58b9f456SAndroid Build Coastguard Worker
1411*58b9f456SAndroid Build Coastguard Worker // Called once and only once after ever group of benchmarks is run and
1412*58b9f456SAndroid Build Coastguard Worker // reported.
Finalize()1413*58b9f456SAndroid Build Coastguard Worker virtual void Finalize() {}
1414*58b9f456SAndroid Build Coastguard Worker
1415*58b9f456SAndroid Build Coastguard Worker // REQUIRES: The object referenced by 'out' is valid for the lifetime
1416*58b9f456SAndroid Build Coastguard Worker // of the reporter.
SetOutputStream(std::ostream * out)1417*58b9f456SAndroid Build Coastguard Worker void SetOutputStream(std::ostream* out) {
1418*58b9f456SAndroid Build Coastguard Worker assert(out);
1419*58b9f456SAndroid Build Coastguard Worker output_stream_ = out;
1420*58b9f456SAndroid Build Coastguard Worker }
1421*58b9f456SAndroid Build Coastguard Worker
1422*58b9f456SAndroid Build Coastguard Worker // REQUIRES: The object referenced by 'err' is valid for the lifetime
1423*58b9f456SAndroid Build Coastguard Worker // of the reporter.
SetErrorStream(std::ostream * err)1424*58b9f456SAndroid Build Coastguard Worker void SetErrorStream(std::ostream* err) {
1425*58b9f456SAndroid Build Coastguard Worker assert(err);
1426*58b9f456SAndroid Build Coastguard Worker error_stream_ = err;
1427*58b9f456SAndroid Build Coastguard Worker }
1428*58b9f456SAndroid Build Coastguard Worker
GetOutputStream()1429*58b9f456SAndroid Build Coastguard Worker std::ostream& GetOutputStream() const { return *output_stream_; }
1430*58b9f456SAndroid Build Coastguard Worker
GetErrorStream()1431*58b9f456SAndroid Build Coastguard Worker std::ostream& GetErrorStream() const { return *error_stream_; }
1432*58b9f456SAndroid Build Coastguard Worker
1433*58b9f456SAndroid Build Coastguard Worker virtual ~BenchmarkReporter();
1434*58b9f456SAndroid Build Coastguard Worker
1435*58b9f456SAndroid Build Coastguard Worker // Write a human readable string to 'out' representing the specified
1436*58b9f456SAndroid Build Coastguard Worker // 'context'.
1437*58b9f456SAndroid Build Coastguard Worker // REQUIRES: 'out' is non-null.
1438*58b9f456SAndroid Build Coastguard Worker static void PrintBasicContext(std::ostream* out, Context const& context);
1439*58b9f456SAndroid Build Coastguard Worker
1440*58b9f456SAndroid Build Coastguard Worker private:
1441*58b9f456SAndroid Build Coastguard Worker std::ostream* output_stream_;
1442*58b9f456SAndroid Build Coastguard Worker std::ostream* error_stream_;
1443*58b9f456SAndroid Build Coastguard Worker };
1444*58b9f456SAndroid Build Coastguard Worker
1445*58b9f456SAndroid Build Coastguard Worker // Simple reporter that outputs benchmark data to the console. This is the
1446*58b9f456SAndroid Build Coastguard Worker // default reporter used by RunSpecifiedBenchmarks().
1447*58b9f456SAndroid Build Coastguard Worker class ConsoleReporter : public BenchmarkReporter {
1448*58b9f456SAndroid Build Coastguard Worker public:
1449*58b9f456SAndroid Build Coastguard Worker enum OutputOptions {
1450*58b9f456SAndroid Build Coastguard Worker OO_None = 0,
1451*58b9f456SAndroid Build Coastguard Worker OO_Color = 1,
1452*58b9f456SAndroid Build Coastguard Worker OO_Tabular = 2,
1453*58b9f456SAndroid Build Coastguard Worker OO_ColorTabular = OO_Color | OO_Tabular,
1454*58b9f456SAndroid Build Coastguard Worker OO_Defaults = OO_ColorTabular
1455*58b9f456SAndroid Build Coastguard Worker };
1456*58b9f456SAndroid Build Coastguard Worker explicit ConsoleReporter(OutputOptions opts_ = OO_Defaults)
output_options_(opts_)1457*58b9f456SAndroid Build Coastguard Worker : output_options_(opts_),
1458*58b9f456SAndroid Build Coastguard Worker name_field_width_(0),
1459*58b9f456SAndroid Build Coastguard Worker prev_counters_(),
1460*58b9f456SAndroid Build Coastguard Worker printed_header_(false) {}
1461*58b9f456SAndroid Build Coastguard Worker
1462*58b9f456SAndroid Build Coastguard Worker virtual bool ReportContext(const Context& context);
1463*58b9f456SAndroid Build Coastguard Worker virtual void ReportRuns(const std::vector<Run>& reports);
1464*58b9f456SAndroid Build Coastguard Worker
1465*58b9f456SAndroid Build Coastguard Worker protected:
1466*58b9f456SAndroid Build Coastguard Worker virtual void PrintRunData(const Run& report);
1467*58b9f456SAndroid Build Coastguard Worker virtual void PrintHeader(const Run& report);
1468*58b9f456SAndroid Build Coastguard Worker
1469*58b9f456SAndroid Build Coastguard Worker OutputOptions output_options_;
1470*58b9f456SAndroid Build Coastguard Worker size_t name_field_width_;
1471*58b9f456SAndroid Build Coastguard Worker UserCounters prev_counters_;
1472*58b9f456SAndroid Build Coastguard Worker bool printed_header_;
1473*58b9f456SAndroid Build Coastguard Worker };
1474*58b9f456SAndroid Build Coastguard Worker
1475*58b9f456SAndroid Build Coastguard Worker class JSONReporter : public BenchmarkReporter {
1476*58b9f456SAndroid Build Coastguard Worker public:
JSONReporter()1477*58b9f456SAndroid Build Coastguard Worker JSONReporter() : first_report_(true) {}
1478*58b9f456SAndroid Build Coastguard Worker virtual bool ReportContext(const Context& context);
1479*58b9f456SAndroid Build Coastguard Worker virtual void ReportRuns(const std::vector<Run>& reports);
1480*58b9f456SAndroid Build Coastguard Worker virtual void Finalize();
1481*58b9f456SAndroid Build Coastguard Worker
1482*58b9f456SAndroid Build Coastguard Worker private:
1483*58b9f456SAndroid Build Coastguard Worker void PrintRunData(const Run& report);
1484*58b9f456SAndroid Build Coastguard Worker
1485*58b9f456SAndroid Build Coastguard Worker bool first_report_;
1486*58b9f456SAndroid Build Coastguard Worker };
1487*58b9f456SAndroid Build Coastguard Worker
1488*58b9f456SAndroid Build Coastguard Worker class BENCHMARK_DEPRECATED_MSG("The CSV Reporter will be removed in a future release")
1489*58b9f456SAndroid Build Coastguard Worker CSVReporter : public BenchmarkReporter {
1490*58b9f456SAndroid Build Coastguard Worker public:
CSVReporter()1491*58b9f456SAndroid Build Coastguard Worker CSVReporter() : printed_header_(false) {}
1492*58b9f456SAndroid Build Coastguard Worker virtual bool ReportContext(const Context& context);
1493*58b9f456SAndroid Build Coastguard Worker virtual void ReportRuns(const std::vector<Run>& reports);
1494*58b9f456SAndroid Build Coastguard Worker
1495*58b9f456SAndroid Build Coastguard Worker private:
1496*58b9f456SAndroid Build Coastguard Worker void PrintRunData(const Run& report);
1497*58b9f456SAndroid Build Coastguard Worker
1498*58b9f456SAndroid Build Coastguard Worker bool printed_header_;
1499*58b9f456SAndroid Build Coastguard Worker std::set<std::string> user_counter_names_;
1500*58b9f456SAndroid Build Coastguard Worker };
1501*58b9f456SAndroid Build Coastguard Worker
1502*58b9f456SAndroid Build Coastguard Worker // If a MemoryManager is registered, it can be used to collect and report
1503*58b9f456SAndroid Build Coastguard Worker // allocation metrics for a run of the benchmark.
1504*58b9f456SAndroid Build Coastguard Worker class MemoryManager {
1505*58b9f456SAndroid Build Coastguard Worker public:
1506*58b9f456SAndroid Build Coastguard Worker struct Result {
ResultResult1507*58b9f456SAndroid Build Coastguard Worker Result() : num_allocs(0), max_bytes_used(0) {}
1508*58b9f456SAndroid Build Coastguard Worker
1509*58b9f456SAndroid Build Coastguard Worker // The number of allocations made in total between Start and Stop.
1510*58b9f456SAndroid Build Coastguard Worker int64_t num_allocs;
1511*58b9f456SAndroid Build Coastguard Worker
1512*58b9f456SAndroid Build Coastguard Worker // The peak memory use between Start and Stop.
1513*58b9f456SAndroid Build Coastguard Worker int64_t max_bytes_used;
1514*58b9f456SAndroid Build Coastguard Worker };
1515*58b9f456SAndroid Build Coastguard Worker
~MemoryManager()1516*58b9f456SAndroid Build Coastguard Worker virtual ~MemoryManager() {}
1517*58b9f456SAndroid Build Coastguard Worker
1518*58b9f456SAndroid Build Coastguard Worker // Implement this to start recording allocation information.
1519*58b9f456SAndroid Build Coastguard Worker virtual void Start() = 0;
1520*58b9f456SAndroid Build Coastguard Worker
1521*58b9f456SAndroid Build Coastguard Worker // Implement this to stop recording and fill out the given Result structure.
1522*58b9f456SAndroid Build Coastguard Worker virtual void Stop(Result* result) = 0;
1523*58b9f456SAndroid Build Coastguard Worker };
1524*58b9f456SAndroid Build Coastguard Worker
GetTimeUnitString(TimeUnit unit)1525*58b9f456SAndroid Build Coastguard Worker inline const char* GetTimeUnitString(TimeUnit unit) {
1526*58b9f456SAndroid Build Coastguard Worker switch (unit) {
1527*58b9f456SAndroid Build Coastguard Worker case kMillisecond:
1528*58b9f456SAndroid Build Coastguard Worker return "ms";
1529*58b9f456SAndroid Build Coastguard Worker case kMicrosecond:
1530*58b9f456SAndroid Build Coastguard Worker return "us";
1531*58b9f456SAndroid Build Coastguard Worker case kNanosecond:
1532*58b9f456SAndroid Build Coastguard Worker return "ns";
1533*58b9f456SAndroid Build Coastguard Worker }
1534*58b9f456SAndroid Build Coastguard Worker BENCHMARK_UNREACHABLE();
1535*58b9f456SAndroid Build Coastguard Worker }
1536*58b9f456SAndroid Build Coastguard Worker
GetTimeUnitMultiplier(TimeUnit unit)1537*58b9f456SAndroid Build Coastguard Worker inline double GetTimeUnitMultiplier(TimeUnit unit) {
1538*58b9f456SAndroid Build Coastguard Worker switch (unit) {
1539*58b9f456SAndroid Build Coastguard Worker case kMillisecond:
1540*58b9f456SAndroid Build Coastguard Worker return 1e3;
1541*58b9f456SAndroid Build Coastguard Worker case kMicrosecond:
1542*58b9f456SAndroid Build Coastguard Worker return 1e6;
1543*58b9f456SAndroid Build Coastguard Worker case kNanosecond:
1544*58b9f456SAndroid Build Coastguard Worker return 1e9;
1545*58b9f456SAndroid Build Coastguard Worker }
1546*58b9f456SAndroid Build Coastguard Worker BENCHMARK_UNREACHABLE();
1547*58b9f456SAndroid Build Coastguard Worker }
1548*58b9f456SAndroid Build Coastguard Worker
1549*58b9f456SAndroid Build Coastguard Worker } // namespace benchmark
1550*58b9f456SAndroid Build Coastguard Worker
1551*58b9f456SAndroid Build Coastguard Worker #endif // BENCHMARK_BENCHMARK_H_
1552