1*58b9f456SAndroid Build Coastguard Worker# benchmark 2*58b9f456SAndroid Build Coastguard Worker[](https://travis-ci.org/google/benchmark) 3*58b9f456SAndroid Build Coastguard Worker[](https://ci.appveyor.com/project/google/benchmark/branch/master) 4*58b9f456SAndroid Build Coastguard Worker[](https://coveralls.io/r/google/benchmark) 5*58b9f456SAndroid Build Coastguard Worker[](https://slackin-iqtfqnpzxd.now.sh/) 6*58b9f456SAndroid Build Coastguard Worker 7*58b9f456SAndroid Build Coastguard WorkerA library to support the benchmarking of functions, similar to unit-tests. 8*58b9f456SAndroid Build Coastguard Worker 9*58b9f456SAndroid Build Coastguard Worker[Discussion group](https://groups.google.com/d/forum/benchmark-discuss) 10*58b9f456SAndroid Build Coastguard Worker 11*58b9f456SAndroid Build Coastguard WorkerIRC channel: [freenode](https://freenode.net) #googlebenchmark 12*58b9f456SAndroid Build Coastguard Worker 13*58b9f456SAndroid Build Coastguard Worker[Additional Tooling Documentation](docs/tools.md) 14*58b9f456SAndroid Build Coastguard Worker 15*58b9f456SAndroid Build Coastguard Worker[Assembly Testing Documentation](docs/AssemblyTests.md) 16*58b9f456SAndroid Build Coastguard Worker 17*58b9f456SAndroid Build Coastguard Worker 18*58b9f456SAndroid Build Coastguard Worker## Building 19*58b9f456SAndroid Build Coastguard Worker 20*58b9f456SAndroid Build Coastguard WorkerThe basic steps for configuring and building the library look like this: 21*58b9f456SAndroid Build Coastguard Worker 22*58b9f456SAndroid Build Coastguard Worker```bash 23*58b9f456SAndroid Build Coastguard Worker$ git clone https://github.com/google/benchmark.git 24*58b9f456SAndroid Build Coastguard Worker# Benchmark requires Google Test as a dependency. Add the source tree as a subdirectory. 25*58b9f456SAndroid Build Coastguard Worker$ git clone https://github.com/google/googletest.git benchmark/googletest 26*58b9f456SAndroid Build Coastguard Worker$ mkdir build && cd build 27*58b9f456SAndroid Build Coastguard Worker$ cmake -G <generator> [options] ../benchmark 28*58b9f456SAndroid Build Coastguard Worker# Assuming a makefile generator was used 29*58b9f456SAndroid Build Coastguard Worker$ make 30*58b9f456SAndroid Build Coastguard Worker``` 31*58b9f456SAndroid Build Coastguard Worker 32*58b9f456SAndroid Build Coastguard WorkerNote that Google Benchmark requires Google Test to build and run the tests. This 33*58b9f456SAndroid Build Coastguard Workerdependency can be provided two ways: 34*58b9f456SAndroid Build Coastguard Worker 35*58b9f456SAndroid Build Coastguard Worker* Checkout the Google Test sources into `benchmark/googletest` as above. 36*58b9f456SAndroid Build Coastguard Worker* Otherwise, if `-DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON` is specified during 37*58b9f456SAndroid Build Coastguard Worker configuration, the library will automatically download and build any required 38*58b9f456SAndroid Build Coastguard Worker dependencies. 39*58b9f456SAndroid Build Coastguard Worker 40*58b9f456SAndroid Build Coastguard WorkerIf you do not wish to build and run the tests, add `-DBENCHMARK_ENABLE_GTEST_TESTS=OFF` 41*58b9f456SAndroid Build Coastguard Workerto `CMAKE_ARGS`. 42*58b9f456SAndroid Build Coastguard Worker 43*58b9f456SAndroid Build Coastguard Worker 44*58b9f456SAndroid Build Coastguard Worker## Installation Guide 45*58b9f456SAndroid Build Coastguard Worker 46*58b9f456SAndroid Build Coastguard WorkerFor Ubuntu and Debian Based System 47*58b9f456SAndroid Build Coastguard Worker 48*58b9f456SAndroid Build Coastguard WorkerFirst make sure you have git and cmake installed (If not please install them) 49*58b9f456SAndroid Build Coastguard Worker 50*58b9f456SAndroid Build Coastguard Worker``` 51*58b9f456SAndroid Build Coastguard Workersudo apt-get install git cmake 52*58b9f456SAndroid Build Coastguard Worker``` 53*58b9f456SAndroid Build Coastguard Worker 54*58b9f456SAndroid Build Coastguard WorkerNow, let's clone the repository and build it 55*58b9f456SAndroid Build Coastguard Worker 56*58b9f456SAndroid Build Coastguard Worker``` 57*58b9f456SAndroid Build Coastguard Workergit clone https://github.com/google/benchmark.git 58*58b9f456SAndroid Build Coastguard Workercd benchmark 59*58b9f456SAndroid Build Coastguard Worker# If you want to build tests and don't use BENCHMARK_DOWNLOAD_DEPENDENCIES, then 60*58b9f456SAndroid Build Coastguard Worker# git clone https://github.com/google/googletest.git 61*58b9f456SAndroid Build Coastguard Workermkdir build 62*58b9f456SAndroid Build Coastguard Workercd build 63*58b9f456SAndroid Build Coastguard Workercmake .. -DCMAKE_BUILD_TYPE=RELEASE 64*58b9f456SAndroid Build Coastguard Workermake 65*58b9f456SAndroid Build Coastguard Worker``` 66*58b9f456SAndroid Build Coastguard Worker 67*58b9f456SAndroid Build Coastguard WorkerIf you need to install the library globally 68*58b9f456SAndroid Build Coastguard Worker 69*58b9f456SAndroid Build Coastguard Worker``` 70*58b9f456SAndroid Build Coastguard Workersudo make install 71*58b9f456SAndroid Build Coastguard Worker``` 72*58b9f456SAndroid Build Coastguard Worker 73*58b9f456SAndroid Build Coastguard Worker## Stable and Experimental Library Versions 74*58b9f456SAndroid Build Coastguard Worker 75*58b9f456SAndroid Build Coastguard WorkerThe main branch contains the latest stable version of the benchmarking library; 76*58b9f456SAndroid Build Coastguard Workerthe API of which can be considered largely stable, with source breaking changes 77*58b9f456SAndroid Build Coastguard Workerbeing made only upon the release of a new major version. 78*58b9f456SAndroid Build Coastguard Worker 79*58b9f456SAndroid Build Coastguard WorkerNewer, experimental, features are implemented and tested on the 80*58b9f456SAndroid Build Coastguard Worker[`v2` branch](https://github.com/google/benchmark/tree/v2). Users who wish 81*58b9f456SAndroid Build Coastguard Workerto use, test, and provide feedback on the new features are encouraged to try 82*58b9f456SAndroid Build Coastguard Workerthis branch. However, this branch provides no stability guarantees and reserves 83*58b9f456SAndroid Build Coastguard Workerthe right to change and break the API at any time. 84*58b9f456SAndroid Build Coastguard Worker 85*58b9f456SAndroid Build Coastguard Worker## Further knowledge 86*58b9f456SAndroid Build Coastguard Worker 87*58b9f456SAndroid Build Coastguard WorkerIt may help to read the [Google Test documentation](https://github.com/google/googletest/blob/master/googletest/docs/primer.md) 88*58b9f456SAndroid Build Coastguard Workeras some of the structural aspects of the APIs are similar. 89*58b9f456SAndroid Build Coastguard Worker 90*58b9f456SAndroid Build Coastguard Worker## Example usage 91*58b9f456SAndroid Build Coastguard Worker### Basic usage 92*58b9f456SAndroid Build Coastguard WorkerDefine a function that executes the code to be measured, register it as a 93*58b9f456SAndroid Build Coastguard Workerbenchmark function using the `BENCHMARK` macro, and ensure an appropriate `main` 94*58b9f456SAndroid Build Coastguard Workerfunction is available: 95*58b9f456SAndroid Build Coastguard Worker 96*58b9f456SAndroid Build Coastguard Worker```c++ 97*58b9f456SAndroid Build Coastguard Worker#include <benchmark/benchmark.h> 98*58b9f456SAndroid Build Coastguard Worker 99*58b9f456SAndroid Build Coastguard Workerstatic void BM_StringCreation(benchmark::State& state) { 100*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) 101*58b9f456SAndroid Build Coastguard Worker std::string empty_string; 102*58b9f456SAndroid Build Coastguard Worker} 103*58b9f456SAndroid Build Coastguard Worker// Register the function as a benchmark 104*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_StringCreation); 105*58b9f456SAndroid Build Coastguard Worker 106*58b9f456SAndroid Build Coastguard Worker// Define another benchmark 107*58b9f456SAndroid Build Coastguard Workerstatic void BM_StringCopy(benchmark::State& state) { 108*58b9f456SAndroid Build Coastguard Worker std::string x = "hello"; 109*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) 110*58b9f456SAndroid Build Coastguard Worker std::string copy(x); 111*58b9f456SAndroid Build Coastguard Worker} 112*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_StringCopy); 113*58b9f456SAndroid Build Coastguard Worker 114*58b9f456SAndroid Build Coastguard WorkerBENCHMARK_MAIN(); 115*58b9f456SAndroid Build Coastguard Worker``` 116*58b9f456SAndroid Build Coastguard Worker 117*58b9f456SAndroid Build Coastguard WorkerDon't forget to inform your linker to add benchmark library e.g. through 118*58b9f456SAndroid Build Coastguard Worker`-lbenchmark` compilation flag. Alternatively, you may leave out the 119*58b9f456SAndroid Build Coastguard Worker`BENCHMARK_MAIN();` at the end of the source file and link against 120*58b9f456SAndroid Build Coastguard Worker`-lbenchmark_main` to get the same default behavior. 121*58b9f456SAndroid Build Coastguard Worker 122*58b9f456SAndroid Build Coastguard WorkerThe benchmark library will measure and report the timing for code within the 123*58b9f456SAndroid Build Coastguard Worker`for(...)` loop. 124*58b9f456SAndroid Build Coastguard Worker 125*58b9f456SAndroid Build Coastguard Worker#### Platform-specific libraries 126*58b9f456SAndroid Build Coastguard WorkerWhen the library is built using GCC it is necessary to link with the pthread 127*58b9f456SAndroid Build Coastguard Workerlibrary due to how GCC implements `std::thread`. Failing to link to pthread will 128*58b9f456SAndroid Build Coastguard Workerlead to runtime exceptions (unless you're using libc++), not linker errors. See 129*58b9f456SAndroid Build Coastguard Worker[issue #67](https://github.com/google/benchmark/issues/67) for more details. You 130*58b9f456SAndroid Build Coastguard Workercan link to pthread by adding `-pthread` to your linker command. Note, you can 131*58b9f456SAndroid Build Coastguard Workeralso use `-lpthread`, but there are potential issues with ordering of command 132*58b9f456SAndroid Build Coastguard Workerline parameters if you use that. 133*58b9f456SAndroid Build Coastguard Worker 134*58b9f456SAndroid Build Coastguard WorkerIf you're running benchmarks on Windows, the shlwapi library (`-lshlwapi`) is 135*58b9f456SAndroid Build Coastguard Workeralso required. 136*58b9f456SAndroid Build Coastguard Worker 137*58b9f456SAndroid Build Coastguard WorkerIf you're running benchmarks on solaris, you'll want the kstat library linked in 138*58b9f456SAndroid Build Coastguard Workertoo (`-lkstat`). 139*58b9f456SAndroid Build Coastguard Worker 140*58b9f456SAndroid Build Coastguard Worker### Passing arguments 141*58b9f456SAndroid Build Coastguard WorkerSometimes a family of benchmarks can be implemented with just one routine that 142*58b9f456SAndroid Build Coastguard Workertakes an extra argument to specify which one of the family of benchmarks to 143*58b9f456SAndroid Build Coastguard Workerrun. For example, the following code defines a family of benchmarks for 144*58b9f456SAndroid Build Coastguard Workermeasuring the speed of `memcpy()` calls of different lengths: 145*58b9f456SAndroid Build Coastguard Worker 146*58b9f456SAndroid Build Coastguard Worker```c++ 147*58b9f456SAndroid Build Coastguard Workerstatic void BM_memcpy(benchmark::State& state) { 148*58b9f456SAndroid Build Coastguard Worker char* src = new char[state.range(0)]; 149*58b9f456SAndroid Build Coastguard Worker char* dst = new char[state.range(0)]; 150*58b9f456SAndroid Build Coastguard Worker memset(src, 'x', state.range(0)); 151*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) 152*58b9f456SAndroid Build Coastguard Worker memcpy(dst, src, state.range(0)); 153*58b9f456SAndroid Build Coastguard Worker state.SetBytesProcessed(int64_t(state.iterations()) * 154*58b9f456SAndroid Build Coastguard Worker int64_t(state.range(0))); 155*58b9f456SAndroid Build Coastguard Worker delete[] src; 156*58b9f456SAndroid Build Coastguard Worker delete[] dst; 157*58b9f456SAndroid Build Coastguard Worker} 158*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10); 159*58b9f456SAndroid Build Coastguard Worker``` 160*58b9f456SAndroid Build Coastguard Worker 161*58b9f456SAndroid Build Coastguard WorkerThe preceding code is quite repetitive, and can be replaced with the following 162*58b9f456SAndroid Build Coastguard Workershort-hand. The following invocation will pick a few appropriate arguments in 163*58b9f456SAndroid Build Coastguard Workerthe specified range and will generate a benchmark for each such argument. 164*58b9f456SAndroid Build Coastguard Worker 165*58b9f456SAndroid Build Coastguard Worker```c++ 166*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_memcpy)->Range(8, 8<<10); 167*58b9f456SAndroid Build Coastguard Worker``` 168*58b9f456SAndroid Build Coastguard Worker 169*58b9f456SAndroid Build Coastguard WorkerBy default the arguments in the range are generated in multiples of eight and 170*58b9f456SAndroid Build Coastguard Workerthe command above selects [ 8, 64, 512, 4k, 8k ]. In the following code the 171*58b9f456SAndroid Build Coastguard Workerrange multiplier is changed to multiples of two. 172*58b9f456SAndroid Build Coastguard Worker 173*58b9f456SAndroid Build Coastguard Worker```c++ 174*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_memcpy)->RangeMultiplier(2)->Range(8, 8<<10); 175*58b9f456SAndroid Build Coastguard Worker``` 176*58b9f456SAndroid Build Coastguard WorkerNow arguments generated are [ 8, 16, 32, 64, 128, 256, 512, 1024, 2k, 4k, 8k ]. 177*58b9f456SAndroid Build Coastguard Worker 178*58b9f456SAndroid Build Coastguard WorkerYou might have a benchmark that depends on two or more inputs. For example, the 179*58b9f456SAndroid Build Coastguard Workerfollowing code defines a family of benchmarks for measuring the speed of set 180*58b9f456SAndroid Build Coastguard Workerinsertion. 181*58b9f456SAndroid Build Coastguard Worker 182*58b9f456SAndroid Build Coastguard Worker```c++ 183*58b9f456SAndroid Build Coastguard Workerstatic void BM_SetInsert(benchmark::State& state) { 184*58b9f456SAndroid Build Coastguard Worker std::set<int> data; 185*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 186*58b9f456SAndroid Build Coastguard Worker state.PauseTiming(); 187*58b9f456SAndroid Build Coastguard Worker data = ConstructRandomSet(state.range(0)); 188*58b9f456SAndroid Build Coastguard Worker state.ResumeTiming(); 189*58b9f456SAndroid Build Coastguard Worker for (int j = 0; j < state.range(1); ++j) 190*58b9f456SAndroid Build Coastguard Worker data.insert(RandomNumber()); 191*58b9f456SAndroid Build Coastguard Worker } 192*58b9f456SAndroid Build Coastguard Worker} 193*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_SetInsert) 194*58b9f456SAndroid Build Coastguard Worker ->Args({1<<10, 128}) 195*58b9f456SAndroid Build Coastguard Worker ->Args({2<<10, 128}) 196*58b9f456SAndroid Build Coastguard Worker ->Args({4<<10, 128}) 197*58b9f456SAndroid Build Coastguard Worker ->Args({8<<10, 128}) 198*58b9f456SAndroid Build Coastguard Worker ->Args({1<<10, 512}) 199*58b9f456SAndroid Build Coastguard Worker ->Args({2<<10, 512}) 200*58b9f456SAndroid Build Coastguard Worker ->Args({4<<10, 512}) 201*58b9f456SAndroid Build Coastguard Worker ->Args({8<<10, 512}); 202*58b9f456SAndroid Build Coastguard Worker``` 203*58b9f456SAndroid Build Coastguard Worker 204*58b9f456SAndroid Build Coastguard WorkerThe preceding code is quite repetitive, and can be replaced with the following 205*58b9f456SAndroid Build Coastguard Workershort-hand. The following macro will pick a few appropriate arguments in the 206*58b9f456SAndroid Build Coastguard Workerproduct of the two specified ranges and will generate a benchmark for each such 207*58b9f456SAndroid Build Coastguard Workerpair. 208*58b9f456SAndroid Build Coastguard Worker 209*58b9f456SAndroid Build Coastguard Worker```c++ 210*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {128, 512}}); 211*58b9f456SAndroid Build Coastguard Worker``` 212*58b9f456SAndroid Build Coastguard Worker 213*58b9f456SAndroid Build Coastguard WorkerFor more complex patterns of inputs, passing a custom function to `Apply` allows 214*58b9f456SAndroid Build Coastguard Workerprogrammatic specification of an arbitrary set of arguments on which to run the 215*58b9f456SAndroid Build Coastguard Workerbenchmark. The following example enumerates a dense range on one parameter, 216*58b9f456SAndroid Build Coastguard Workerand a sparse range on the second. 217*58b9f456SAndroid Build Coastguard Worker 218*58b9f456SAndroid Build Coastguard Worker```c++ 219*58b9f456SAndroid Build Coastguard Workerstatic void CustomArguments(benchmark::internal::Benchmark* b) { 220*58b9f456SAndroid Build Coastguard Worker for (int i = 0; i <= 10; ++i) 221*58b9f456SAndroid Build Coastguard Worker for (int j = 32; j <= 1024*1024; j *= 8) 222*58b9f456SAndroid Build Coastguard Worker b->Args({i, j}); 223*58b9f456SAndroid Build Coastguard Worker} 224*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_SetInsert)->Apply(CustomArguments); 225*58b9f456SAndroid Build Coastguard Worker``` 226*58b9f456SAndroid Build Coastguard Worker 227*58b9f456SAndroid Build Coastguard Worker### Calculate asymptotic complexity (Big O) 228*58b9f456SAndroid Build Coastguard WorkerAsymptotic complexity might be calculated for a family of benchmarks. The 229*58b9f456SAndroid Build Coastguard Workerfollowing code will calculate the coefficient for the high-order term in the 230*58b9f456SAndroid Build Coastguard Workerrunning time and the normalized root-mean square error of string comparison. 231*58b9f456SAndroid Build Coastguard Worker 232*58b9f456SAndroid Build Coastguard Worker```c++ 233*58b9f456SAndroid Build Coastguard Workerstatic void BM_StringCompare(benchmark::State& state) { 234*58b9f456SAndroid Build Coastguard Worker std::string s1(state.range(0), '-'); 235*58b9f456SAndroid Build Coastguard Worker std::string s2(state.range(0), '-'); 236*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 237*58b9f456SAndroid Build Coastguard Worker benchmark::DoNotOptimize(s1.compare(s2)); 238*58b9f456SAndroid Build Coastguard Worker } 239*58b9f456SAndroid Build Coastguard Worker state.SetComplexityN(state.range(0)); 240*58b9f456SAndroid Build Coastguard Worker} 241*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_StringCompare) 242*58b9f456SAndroid Build Coastguard Worker ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(benchmark::oN); 243*58b9f456SAndroid Build Coastguard Worker``` 244*58b9f456SAndroid Build Coastguard Worker 245*58b9f456SAndroid Build Coastguard WorkerAs shown in the following invocation, asymptotic complexity might also be 246*58b9f456SAndroid Build Coastguard Workercalculated automatically. 247*58b9f456SAndroid Build Coastguard Worker 248*58b9f456SAndroid Build Coastguard Worker```c++ 249*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_StringCompare) 250*58b9f456SAndroid Build Coastguard Worker ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(); 251*58b9f456SAndroid Build Coastguard Worker``` 252*58b9f456SAndroid Build Coastguard Worker 253*58b9f456SAndroid Build Coastguard WorkerThe following code will specify asymptotic complexity with a lambda function, 254*58b9f456SAndroid Build Coastguard Workerthat might be used to customize high-order term calculation. 255*58b9f456SAndroid Build Coastguard Worker 256*58b9f456SAndroid Build Coastguard Worker```c++ 257*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_StringCompare)->RangeMultiplier(2) 258*58b9f456SAndroid Build Coastguard Worker ->Range(1<<10, 1<<18)->Complexity([](int64_t n)->double{return n; }); 259*58b9f456SAndroid Build Coastguard Worker``` 260*58b9f456SAndroid Build Coastguard Worker 261*58b9f456SAndroid Build Coastguard Worker### Templated benchmarks 262*58b9f456SAndroid Build Coastguard WorkerTemplated benchmarks work the same way: This example produces and consumes 263*58b9f456SAndroid Build Coastguard Workermessages of size `sizeof(v)` `range_x` times. It also outputs throughput in the 264*58b9f456SAndroid Build Coastguard Workerabsence of multiprogramming. 265*58b9f456SAndroid Build Coastguard Worker 266*58b9f456SAndroid Build Coastguard Worker```c++ 267*58b9f456SAndroid Build Coastguard Workertemplate <class Q> void BM_Sequential(benchmark::State& state) { 268*58b9f456SAndroid Build Coastguard Worker Q q; 269*58b9f456SAndroid Build Coastguard Worker typename Q::value_type v; 270*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 271*58b9f456SAndroid Build Coastguard Worker for (int i = state.range(0); i--; ) 272*58b9f456SAndroid Build Coastguard Worker q.push(v); 273*58b9f456SAndroid Build Coastguard Worker for (int e = state.range(0); e--; ) 274*58b9f456SAndroid Build Coastguard Worker q.Wait(&v); 275*58b9f456SAndroid Build Coastguard Worker } 276*58b9f456SAndroid Build Coastguard Worker // actually messages, not bytes: 277*58b9f456SAndroid Build Coastguard Worker state.SetBytesProcessed( 278*58b9f456SAndroid Build Coastguard Worker static_cast<int64_t>(state.iterations())*state.range(0)); 279*58b9f456SAndroid Build Coastguard Worker} 280*58b9f456SAndroid Build Coastguard WorkerBENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10); 281*58b9f456SAndroid Build Coastguard Worker``` 282*58b9f456SAndroid Build Coastguard Worker 283*58b9f456SAndroid Build Coastguard WorkerThree macros are provided for adding benchmark templates. 284*58b9f456SAndroid Build Coastguard Worker 285*58b9f456SAndroid Build Coastguard Worker```c++ 286*58b9f456SAndroid Build Coastguard Worker#ifdef BENCHMARK_HAS_CXX11 287*58b9f456SAndroid Build Coastguard Worker#define BENCHMARK_TEMPLATE(func, ...) // Takes any number of parameters. 288*58b9f456SAndroid Build Coastguard Worker#else // C++ < C++11 289*58b9f456SAndroid Build Coastguard Worker#define BENCHMARK_TEMPLATE(func, arg1) 290*58b9f456SAndroid Build Coastguard Worker#endif 291*58b9f456SAndroid Build Coastguard Worker#define BENCHMARK_TEMPLATE1(func, arg1) 292*58b9f456SAndroid Build Coastguard Worker#define BENCHMARK_TEMPLATE2(func, arg1, arg2) 293*58b9f456SAndroid Build Coastguard Worker``` 294*58b9f456SAndroid Build Coastguard Worker 295*58b9f456SAndroid Build Coastguard Worker### A Faster KeepRunning loop 296*58b9f456SAndroid Build Coastguard Worker 297*58b9f456SAndroid Build Coastguard WorkerIn C++11 mode, a ranged-based for loop should be used in preference to 298*58b9f456SAndroid Build Coastguard Workerthe `KeepRunning` loop for running the benchmarks. For example: 299*58b9f456SAndroid Build Coastguard Worker 300*58b9f456SAndroid Build Coastguard Worker```c++ 301*58b9f456SAndroid Build Coastguard Workerstatic void BM_Fast(benchmark::State &state) { 302*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 303*58b9f456SAndroid Build Coastguard Worker FastOperation(); 304*58b9f456SAndroid Build Coastguard Worker } 305*58b9f456SAndroid Build Coastguard Worker} 306*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_Fast); 307*58b9f456SAndroid Build Coastguard Worker``` 308*58b9f456SAndroid Build Coastguard Worker 309*58b9f456SAndroid Build Coastguard WorkerThe reason the ranged-for loop is faster than using `KeepRunning`, is 310*58b9f456SAndroid Build Coastguard Workerbecause `KeepRunning` requires a memory load and store of the iteration count 311*58b9f456SAndroid Build Coastguard Workerever iteration, whereas the ranged-for variant is able to keep the iteration count 312*58b9f456SAndroid Build Coastguard Workerin a register. 313*58b9f456SAndroid Build Coastguard Worker 314*58b9f456SAndroid Build Coastguard WorkerFor example, an empty inner loop of using the ranged-based for method looks like: 315*58b9f456SAndroid Build Coastguard Worker 316*58b9f456SAndroid Build Coastguard Worker```asm 317*58b9f456SAndroid Build Coastguard Worker# Loop Init 318*58b9f456SAndroid Build Coastguard Worker mov rbx, qword ptr [r14 + 104] 319*58b9f456SAndroid Build Coastguard Worker call benchmark::State::StartKeepRunning() 320*58b9f456SAndroid Build Coastguard Worker test rbx, rbx 321*58b9f456SAndroid Build Coastguard Worker je .LoopEnd 322*58b9f456SAndroid Build Coastguard Worker.LoopHeader: # =>This Inner Loop Header: Depth=1 323*58b9f456SAndroid Build Coastguard Worker add rbx, -1 324*58b9f456SAndroid Build Coastguard Worker jne .LoopHeader 325*58b9f456SAndroid Build Coastguard Worker.LoopEnd: 326*58b9f456SAndroid Build Coastguard Worker``` 327*58b9f456SAndroid Build Coastguard Worker 328*58b9f456SAndroid Build Coastguard WorkerCompared to an empty `KeepRunning` loop, which looks like: 329*58b9f456SAndroid Build Coastguard Worker 330*58b9f456SAndroid Build Coastguard Worker```asm 331*58b9f456SAndroid Build Coastguard Worker.LoopHeader: # in Loop: Header=BB0_3 Depth=1 332*58b9f456SAndroid Build Coastguard Worker cmp byte ptr [rbx], 1 333*58b9f456SAndroid Build Coastguard Worker jne .LoopInit 334*58b9f456SAndroid Build Coastguard Worker.LoopBody: # =>This Inner Loop Header: Depth=1 335*58b9f456SAndroid Build Coastguard Worker mov rax, qword ptr [rbx + 8] 336*58b9f456SAndroid Build Coastguard Worker lea rcx, [rax + 1] 337*58b9f456SAndroid Build Coastguard Worker mov qword ptr [rbx + 8], rcx 338*58b9f456SAndroid Build Coastguard Worker cmp rax, qword ptr [rbx + 104] 339*58b9f456SAndroid Build Coastguard Worker jb .LoopHeader 340*58b9f456SAndroid Build Coastguard Worker jmp .LoopEnd 341*58b9f456SAndroid Build Coastguard Worker.LoopInit: 342*58b9f456SAndroid Build Coastguard Worker mov rdi, rbx 343*58b9f456SAndroid Build Coastguard Worker call benchmark::State::StartKeepRunning() 344*58b9f456SAndroid Build Coastguard Worker jmp .LoopBody 345*58b9f456SAndroid Build Coastguard Worker.LoopEnd: 346*58b9f456SAndroid Build Coastguard Worker``` 347*58b9f456SAndroid Build Coastguard Worker 348*58b9f456SAndroid Build Coastguard WorkerUnless C++03 compatibility is required, the ranged-for variant of writing 349*58b9f456SAndroid Build Coastguard Workerthe benchmark loop should be preferred. 350*58b9f456SAndroid Build Coastguard Worker 351*58b9f456SAndroid Build Coastguard Worker## Passing arbitrary arguments to a benchmark 352*58b9f456SAndroid Build Coastguard WorkerIn C++11 it is possible to define a benchmark that takes an arbitrary number 353*58b9f456SAndroid Build Coastguard Workerof extra arguments. The `BENCHMARK_CAPTURE(func, test_case_name, ...args)` 354*58b9f456SAndroid Build Coastguard Workermacro creates a benchmark that invokes `func` with the `benchmark::State` as 355*58b9f456SAndroid Build Coastguard Workerthe first argument followed by the specified `args...`. 356*58b9f456SAndroid Build Coastguard WorkerThe `test_case_name` is appended to the name of the benchmark and 357*58b9f456SAndroid Build Coastguard Workershould describe the values passed. 358*58b9f456SAndroid Build Coastguard Worker 359*58b9f456SAndroid Build Coastguard Worker```c++ 360*58b9f456SAndroid Build Coastguard Workertemplate <class ...ExtraArgs> 361*58b9f456SAndroid Build Coastguard Workervoid BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) { 362*58b9f456SAndroid Build Coastguard Worker [...] 363*58b9f456SAndroid Build Coastguard Worker} 364*58b9f456SAndroid Build Coastguard Worker// Registers a benchmark named "BM_takes_args/int_string_test" that passes 365*58b9f456SAndroid Build Coastguard Worker// the specified values to `extra_args`. 366*58b9f456SAndroid Build Coastguard WorkerBENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc")); 367*58b9f456SAndroid Build Coastguard Worker``` 368*58b9f456SAndroid Build Coastguard WorkerNote that elements of `...args` may refer to global variables. Users should 369*58b9f456SAndroid Build Coastguard Workeravoid modifying global state inside of a benchmark. 370*58b9f456SAndroid Build Coastguard Worker 371*58b9f456SAndroid Build Coastguard Worker## Using RegisterBenchmark(name, fn, args...) 372*58b9f456SAndroid Build Coastguard Worker 373*58b9f456SAndroid Build Coastguard WorkerThe `RegisterBenchmark(name, func, args...)` function provides an alternative 374*58b9f456SAndroid Build Coastguard Workerway to create and register benchmarks. 375*58b9f456SAndroid Build Coastguard Worker`RegisterBenchmark(name, func, args...)` creates, registers, and returns a 376*58b9f456SAndroid Build Coastguard Workerpointer to a new benchmark with the specified `name` that invokes 377*58b9f456SAndroid Build Coastguard Worker`func(st, args...)` where `st` is a `benchmark::State` object. 378*58b9f456SAndroid Build Coastguard Worker 379*58b9f456SAndroid Build Coastguard WorkerUnlike the `BENCHMARK` registration macros, which can only be used at the global 380*58b9f456SAndroid Build Coastguard Workerscope, the `RegisterBenchmark` can be called anywhere. This allows for 381*58b9f456SAndroid Build Coastguard Workerbenchmark tests to be registered programmatically. 382*58b9f456SAndroid Build Coastguard Worker 383*58b9f456SAndroid Build Coastguard WorkerAdditionally `RegisterBenchmark` allows any callable object to be registered 384*58b9f456SAndroid Build Coastguard Workeras a benchmark. Including capturing lambdas and function objects. 385*58b9f456SAndroid Build Coastguard Worker 386*58b9f456SAndroid Build Coastguard WorkerFor Example: 387*58b9f456SAndroid Build Coastguard Worker```c++ 388*58b9f456SAndroid Build Coastguard Workerauto BM_test = [](benchmark::State& st, auto Inputs) { /* ... */ }; 389*58b9f456SAndroid Build Coastguard Worker 390*58b9f456SAndroid Build Coastguard Workerint main(int argc, char** argv) { 391*58b9f456SAndroid Build Coastguard Worker for (auto& test_input : { /* ... */ }) 392*58b9f456SAndroid Build Coastguard Worker benchmark::RegisterBenchmark(test_input.name(), BM_test, test_input); 393*58b9f456SAndroid Build Coastguard Worker benchmark::Initialize(&argc, argv); 394*58b9f456SAndroid Build Coastguard Worker benchmark::RunSpecifiedBenchmarks(); 395*58b9f456SAndroid Build Coastguard Worker} 396*58b9f456SAndroid Build Coastguard Worker``` 397*58b9f456SAndroid Build Coastguard Worker 398*58b9f456SAndroid Build Coastguard Worker### Multithreaded benchmarks 399*58b9f456SAndroid Build Coastguard WorkerIn a multithreaded test (benchmark invoked by multiple threads simultaneously), 400*58b9f456SAndroid Build Coastguard Workerit is guaranteed that none of the threads will start until all have reached 401*58b9f456SAndroid Build Coastguard Workerthe start of the benchmark loop, and all will have finished before any thread 402*58b9f456SAndroid Build Coastguard Workerexits the benchmark loop. (This behavior is also provided by the `KeepRunning()` 403*58b9f456SAndroid Build Coastguard WorkerAPI) As such, any global setup or teardown can be wrapped in a check against the thread 404*58b9f456SAndroid Build Coastguard Workerindex: 405*58b9f456SAndroid Build Coastguard Worker 406*58b9f456SAndroid Build Coastguard Worker```c++ 407*58b9f456SAndroid Build Coastguard Workerstatic void BM_MultiThreaded(benchmark::State& state) { 408*58b9f456SAndroid Build Coastguard Worker if (state.thread_index == 0) { 409*58b9f456SAndroid Build Coastguard Worker // Setup code here. 410*58b9f456SAndroid Build Coastguard Worker } 411*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 412*58b9f456SAndroid Build Coastguard Worker // Run the test as normal. 413*58b9f456SAndroid Build Coastguard Worker } 414*58b9f456SAndroid Build Coastguard Worker if (state.thread_index == 0) { 415*58b9f456SAndroid Build Coastguard Worker // Teardown code here. 416*58b9f456SAndroid Build Coastguard Worker } 417*58b9f456SAndroid Build Coastguard Worker} 418*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_MultiThreaded)->Threads(2); 419*58b9f456SAndroid Build Coastguard Worker``` 420*58b9f456SAndroid Build Coastguard Worker 421*58b9f456SAndroid Build Coastguard WorkerIf the benchmarked code itself uses threads and you want to compare it to 422*58b9f456SAndroid Build Coastguard Workersingle-threaded code, you may want to use real-time ("wallclock") measurements 423*58b9f456SAndroid Build Coastguard Workerfor latency comparisons: 424*58b9f456SAndroid Build Coastguard Worker 425*58b9f456SAndroid Build Coastguard Worker```c++ 426*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_test)->Range(8, 8<<10)->UseRealTime(); 427*58b9f456SAndroid Build Coastguard Worker``` 428*58b9f456SAndroid Build Coastguard Worker 429*58b9f456SAndroid Build Coastguard WorkerWithout `UseRealTime`, CPU time is used by default. 430*58b9f456SAndroid Build Coastguard Worker 431*58b9f456SAndroid Build Coastguard Worker## Controlling timers 432*58b9f456SAndroid Build Coastguard WorkerNormally, the entire duration of the work loop (`for (auto _ : state) {}`) 433*58b9f456SAndroid Build Coastguard Workeris measured. But sometimes, it is nessesary to do some work inside of 434*58b9f456SAndroid Build Coastguard Workerthat loop, every iteration, but without counting that time to the benchmark time. 435*58b9f456SAndroid Build Coastguard WorkerThat is possible, althought it is not recommended, since it has high overhead. 436*58b9f456SAndroid Build Coastguard Worker 437*58b9f456SAndroid Build Coastguard Worker```c++ 438*58b9f456SAndroid Build Coastguard Workerstatic void BM_SetInsert_With_Timer_Control(benchmark::State& state) { 439*58b9f456SAndroid Build Coastguard Worker std::set<int> data; 440*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 441*58b9f456SAndroid Build Coastguard Worker state.PauseTiming(); // Stop timers. They will not count until they are resumed. 442*58b9f456SAndroid Build Coastguard Worker data = ConstructRandomSet(state.range(0)); // Do something that should not be measured 443*58b9f456SAndroid Build Coastguard Worker state.ResumeTiming(); // And resume timers. They are now counting again. 444*58b9f456SAndroid Build Coastguard Worker // The rest will be measured. 445*58b9f456SAndroid Build Coastguard Worker for (int j = 0; j < state.range(1); ++j) 446*58b9f456SAndroid Build Coastguard Worker data.insert(RandomNumber()); 447*58b9f456SAndroid Build Coastguard Worker } 448*58b9f456SAndroid Build Coastguard Worker} 449*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_SetInsert_With_Timer_Control)->Ranges({{1<<10, 8<<10}, {128, 512}}); 450*58b9f456SAndroid Build Coastguard Worker``` 451*58b9f456SAndroid Build Coastguard Worker 452*58b9f456SAndroid Build Coastguard Worker## Manual timing 453*58b9f456SAndroid Build Coastguard WorkerFor benchmarking something for which neither CPU time nor real-time are 454*58b9f456SAndroid Build Coastguard Workercorrect or accurate enough, completely manual timing is supported using 455*58b9f456SAndroid Build Coastguard Workerthe `UseManualTime` function. 456*58b9f456SAndroid Build Coastguard Worker 457*58b9f456SAndroid Build Coastguard WorkerWhen `UseManualTime` is used, the benchmarked code must call 458*58b9f456SAndroid Build Coastguard Worker`SetIterationTime` once per iteration of the benchmark loop to 459*58b9f456SAndroid Build Coastguard Workerreport the manually measured time. 460*58b9f456SAndroid Build Coastguard Worker 461*58b9f456SAndroid Build Coastguard WorkerAn example use case for this is benchmarking GPU execution (e.g. OpenCL 462*58b9f456SAndroid Build Coastguard Workeror CUDA kernels, OpenGL or Vulkan or Direct3D draw calls), which cannot 463*58b9f456SAndroid Build Coastguard Workerbe accurately measured using CPU time or real-time. Instead, they can be 464*58b9f456SAndroid Build Coastguard Workermeasured accurately using a dedicated API, and these measurement results 465*58b9f456SAndroid Build Coastguard Workercan be reported back with `SetIterationTime`. 466*58b9f456SAndroid Build Coastguard Worker 467*58b9f456SAndroid Build Coastguard Worker```c++ 468*58b9f456SAndroid Build Coastguard Workerstatic void BM_ManualTiming(benchmark::State& state) { 469*58b9f456SAndroid Build Coastguard Worker int microseconds = state.range(0); 470*58b9f456SAndroid Build Coastguard Worker std::chrono::duration<double, std::micro> sleep_duration { 471*58b9f456SAndroid Build Coastguard Worker static_cast<double>(microseconds) 472*58b9f456SAndroid Build Coastguard Worker }; 473*58b9f456SAndroid Build Coastguard Worker 474*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 475*58b9f456SAndroid Build Coastguard Worker auto start = std::chrono::high_resolution_clock::now(); 476*58b9f456SAndroid Build Coastguard Worker // Simulate some useful workload with a sleep 477*58b9f456SAndroid Build Coastguard Worker std::this_thread::sleep_for(sleep_duration); 478*58b9f456SAndroid Build Coastguard Worker auto end = std::chrono::high_resolution_clock::now(); 479*58b9f456SAndroid Build Coastguard Worker 480*58b9f456SAndroid Build Coastguard Worker auto elapsed_seconds = 481*58b9f456SAndroid Build Coastguard Worker std::chrono::duration_cast<std::chrono::duration<double>>( 482*58b9f456SAndroid Build Coastguard Worker end - start); 483*58b9f456SAndroid Build Coastguard Worker 484*58b9f456SAndroid Build Coastguard Worker state.SetIterationTime(elapsed_seconds.count()); 485*58b9f456SAndroid Build Coastguard Worker } 486*58b9f456SAndroid Build Coastguard Worker} 487*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_ManualTiming)->Range(1, 1<<17)->UseManualTime(); 488*58b9f456SAndroid Build Coastguard Worker``` 489*58b9f456SAndroid Build Coastguard Worker 490*58b9f456SAndroid Build Coastguard Worker### Preventing optimisation 491*58b9f456SAndroid Build Coastguard WorkerTo prevent a value or expression from being optimized away by the compiler 492*58b9f456SAndroid Build Coastguard Workerthe `benchmark::DoNotOptimize(...)` and `benchmark::ClobberMemory()` 493*58b9f456SAndroid Build Coastguard Workerfunctions can be used. 494*58b9f456SAndroid Build Coastguard Worker 495*58b9f456SAndroid Build Coastguard Worker```c++ 496*58b9f456SAndroid Build Coastguard Workerstatic void BM_test(benchmark::State& state) { 497*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 498*58b9f456SAndroid Build Coastguard Worker int x = 0; 499*58b9f456SAndroid Build Coastguard Worker for (int i=0; i < 64; ++i) { 500*58b9f456SAndroid Build Coastguard Worker benchmark::DoNotOptimize(x += i); 501*58b9f456SAndroid Build Coastguard Worker } 502*58b9f456SAndroid Build Coastguard Worker } 503*58b9f456SAndroid Build Coastguard Worker} 504*58b9f456SAndroid Build Coastguard Worker``` 505*58b9f456SAndroid Build Coastguard Worker 506*58b9f456SAndroid Build Coastguard Worker`DoNotOptimize(<expr>)` forces the *result* of `<expr>` to be stored in either 507*58b9f456SAndroid Build Coastguard Workermemory or a register. For GNU based compilers it acts as read/write barrier 508*58b9f456SAndroid Build Coastguard Workerfor global memory. More specifically it forces the compiler to flush pending 509*58b9f456SAndroid Build Coastguard Workerwrites to memory and reload any other values as necessary. 510*58b9f456SAndroid Build Coastguard Worker 511*58b9f456SAndroid Build Coastguard WorkerNote that `DoNotOptimize(<expr>)` does not prevent optimizations on `<expr>` 512*58b9f456SAndroid Build Coastguard Workerin any way. `<expr>` may even be removed entirely when the result is already 513*58b9f456SAndroid Build Coastguard Workerknown. For example: 514*58b9f456SAndroid Build Coastguard Worker 515*58b9f456SAndroid Build Coastguard Worker```c++ 516*58b9f456SAndroid Build Coastguard Worker /* Example 1: `<expr>` is removed entirely. */ 517*58b9f456SAndroid Build Coastguard Worker int foo(int x) { return x + 42; } 518*58b9f456SAndroid Build Coastguard Worker while (...) DoNotOptimize(foo(0)); // Optimized to DoNotOptimize(42); 519*58b9f456SAndroid Build Coastguard Worker 520*58b9f456SAndroid Build Coastguard Worker /* Example 2: Result of '<expr>' is only reused */ 521*58b9f456SAndroid Build Coastguard Worker int bar(int) __attribute__((const)); 522*58b9f456SAndroid Build Coastguard Worker while (...) DoNotOptimize(bar(0)); // Optimized to: 523*58b9f456SAndroid Build Coastguard Worker // int __result__ = bar(0); 524*58b9f456SAndroid Build Coastguard Worker // while (...) DoNotOptimize(__result__); 525*58b9f456SAndroid Build Coastguard Worker``` 526*58b9f456SAndroid Build Coastguard Worker 527*58b9f456SAndroid Build Coastguard WorkerThe second tool for preventing optimizations is `ClobberMemory()`. In essence 528*58b9f456SAndroid Build Coastguard Worker`ClobberMemory()` forces the compiler to perform all pending writes to global 529*58b9f456SAndroid Build Coastguard Workermemory. Memory managed by block scope objects must be "escaped" using 530*58b9f456SAndroid Build Coastguard Worker`DoNotOptimize(...)` before it can be clobbered. In the below example 531*58b9f456SAndroid Build Coastguard Worker`ClobberMemory()` prevents the call to `v.push_back(42)` from being optimized 532*58b9f456SAndroid Build Coastguard Workeraway. 533*58b9f456SAndroid Build Coastguard Worker 534*58b9f456SAndroid Build Coastguard Worker```c++ 535*58b9f456SAndroid Build Coastguard Workerstatic void BM_vector_push_back(benchmark::State& state) { 536*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 537*58b9f456SAndroid Build Coastguard Worker std::vector<int> v; 538*58b9f456SAndroid Build Coastguard Worker v.reserve(1); 539*58b9f456SAndroid Build Coastguard Worker benchmark::DoNotOptimize(v.data()); // Allow v.data() to be clobbered. 540*58b9f456SAndroid Build Coastguard Worker v.push_back(42); 541*58b9f456SAndroid Build Coastguard Worker benchmark::ClobberMemory(); // Force 42 to be written to memory. 542*58b9f456SAndroid Build Coastguard Worker } 543*58b9f456SAndroid Build Coastguard Worker} 544*58b9f456SAndroid Build Coastguard Worker``` 545*58b9f456SAndroid Build Coastguard Worker 546*58b9f456SAndroid Build Coastguard WorkerNote that `ClobberMemory()` is only available for GNU or MSVC based compilers. 547*58b9f456SAndroid Build Coastguard Worker 548*58b9f456SAndroid Build Coastguard Worker### Set time unit manually 549*58b9f456SAndroid Build Coastguard WorkerIf a benchmark runs a few milliseconds it may be hard to visually compare the 550*58b9f456SAndroid Build Coastguard Workermeasured times, since the output data is given in nanoseconds per default. In 551*58b9f456SAndroid Build Coastguard Workerorder to manually set the time unit, you can specify it manually: 552*58b9f456SAndroid Build Coastguard Worker 553*58b9f456SAndroid Build Coastguard Worker```c++ 554*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_test)->Unit(benchmark::kMillisecond); 555*58b9f456SAndroid Build Coastguard Worker``` 556*58b9f456SAndroid Build Coastguard Worker 557*58b9f456SAndroid Build Coastguard Worker### Reporting the mean, median and standard deviation by repeated benchmarks 558*58b9f456SAndroid Build Coastguard WorkerBy default each benchmark is run once and that single result is reported. 559*58b9f456SAndroid Build Coastguard WorkerHowever benchmarks are often noisy and a single result may not be representative 560*58b9f456SAndroid Build Coastguard Workerof the overall behavior. For this reason it's possible to repeatedly rerun the 561*58b9f456SAndroid Build Coastguard Workerbenchmark. 562*58b9f456SAndroid Build Coastguard Worker 563*58b9f456SAndroid Build Coastguard WorkerThe number of runs of each benchmark is specified globally by the 564*58b9f456SAndroid Build Coastguard Worker`--benchmark_repetitions` flag or on a per benchmark basis by calling 565*58b9f456SAndroid Build Coastguard Worker`Repetitions` on the registered benchmark object. When a benchmark is run more 566*58b9f456SAndroid Build Coastguard Workerthan once the mean, median and standard deviation of the runs will be reported. 567*58b9f456SAndroid Build Coastguard Worker 568*58b9f456SAndroid Build Coastguard WorkerAdditionally the `--benchmark_report_aggregates_only={true|false}`, 569*58b9f456SAndroid Build Coastguard Worker`--benchmark_display_aggregates_only={true|false}` flags or 570*58b9f456SAndroid Build Coastguard Worker`ReportAggregatesOnly(bool)`, `DisplayAggregatesOnly(bool)` functions can be 571*58b9f456SAndroid Build Coastguard Workerused to change how repeated tests are reported. By default the result of each 572*58b9f456SAndroid Build Coastguard Workerrepeated run is reported. When `report aggregates only` option is `true`, 573*58b9f456SAndroid Build Coastguard Workeronly the aggregates (i.e. mean, median and standard deviation, maybe complexity 574*58b9f456SAndroid Build Coastguard Workermeasurements if they were requested) of the runs is reported, to both the 575*58b9f456SAndroid Build Coastguard Workerreporters - standard output (console), and the file. 576*58b9f456SAndroid Build Coastguard WorkerHowever when only the `display aggregates only` option is `true`, 577*58b9f456SAndroid Build Coastguard Workeronly the aggregates are displayed in the standard output, while the file 578*58b9f456SAndroid Build Coastguard Workeroutput still contains everything. 579*58b9f456SAndroid Build Coastguard WorkerCalling `ReportAggregatesOnly(bool)` / `DisplayAggregatesOnly(bool)` on a 580*58b9f456SAndroid Build Coastguard Workerregistered benchmark object overrides the value of the appropriate flag for that 581*58b9f456SAndroid Build Coastguard Workerbenchmark. 582*58b9f456SAndroid Build Coastguard Worker 583*58b9f456SAndroid Build Coastguard Worker## User-defined statistics for repeated benchmarks 584*58b9f456SAndroid Build Coastguard WorkerWhile having mean, median and standard deviation is nice, this may not be 585*58b9f456SAndroid Build Coastguard Workerenough for everyone. For example you may want to know what is the largest 586*58b9f456SAndroid Build Coastguard Workerobservation, e.g. because you have some real-time constraints. This is easy. 587*58b9f456SAndroid Build Coastguard WorkerThe following code will specify a custom statistic to be calculated, defined 588*58b9f456SAndroid Build Coastguard Workerby a lambda function. 589*58b9f456SAndroid Build Coastguard Worker 590*58b9f456SAndroid Build Coastguard Worker```c++ 591*58b9f456SAndroid Build Coastguard Workervoid BM_spin_empty(benchmark::State& state) { 592*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 593*58b9f456SAndroid Build Coastguard Worker for (int x = 0; x < state.range(0); ++x) { 594*58b9f456SAndroid Build Coastguard Worker benchmark::DoNotOptimize(x); 595*58b9f456SAndroid Build Coastguard Worker } 596*58b9f456SAndroid Build Coastguard Worker } 597*58b9f456SAndroid Build Coastguard Worker} 598*58b9f456SAndroid Build Coastguard Worker 599*58b9f456SAndroid Build Coastguard WorkerBENCHMARK(BM_spin_empty) 600*58b9f456SAndroid Build Coastguard Worker ->ComputeStatistics("max", [](const std::vector<double>& v) -> double { 601*58b9f456SAndroid Build Coastguard Worker return *(std::max_element(std::begin(v), std::end(v))); 602*58b9f456SAndroid Build Coastguard Worker }) 603*58b9f456SAndroid Build Coastguard Worker ->Arg(512); 604*58b9f456SAndroid Build Coastguard Worker``` 605*58b9f456SAndroid Build Coastguard Worker 606*58b9f456SAndroid Build Coastguard Worker## Fixtures 607*58b9f456SAndroid Build Coastguard WorkerFixture tests are created by 608*58b9f456SAndroid Build Coastguard Workerfirst defining a type that derives from `::benchmark::Fixture` and then 609*58b9f456SAndroid Build Coastguard Workercreating/registering the tests using the following macros: 610*58b9f456SAndroid Build Coastguard Worker 611*58b9f456SAndroid Build Coastguard Worker* `BENCHMARK_F(ClassName, Method)` 612*58b9f456SAndroid Build Coastguard Worker* `BENCHMARK_DEFINE_F(ClassName, Method)` 613*58b9f456SAndroid Build Coastguard Worker* `BENCHMARK_REGISTER_F(ClassName, Method)` 614*58b9f456SAndroid Build Coastguard Worker 615*58b9f456SAndroid Build Coastguard WorkerFor Example: 616*58b9f456SAndroid Build Coastguard Worker 617*58b9f456SAndroid Build Coastguard Worker```c++ 618*58b9f456SAndroid Build Coastguard Workerclass MyFixture : public benchmark::Fixture {}; 619*58b9f456SAndroid Build Coastguard Worker 620*58b9f456SAndroid Build Coastguard WorkerBENCHMARK_F(MyFixture, FooTest)(benchmark::State& st) { 621*58b9f456SAndroid Build Coastguard Worker for (auto _ : st) { 622*58b9f456SAndroid Build Coastguard Worker ... 623*58b9f456SAndroid Build Coastguard Worker } 624*58b9f456SAndroid Build Coastguard Worker} 625*58b9f456SAndroid Build Coastguard Worker 626*58b9f456SAndroid Build Coastguard WorkerBENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State& st) { 627*58b9f456SAndroid Build Coastguard Worker for (auto _ : st) { 628*58b9f456SAndroid Build Coastguard Worker ... 629*58b9f456SAndroid Build Coastguard Worker } 630*58b9f456SAndroid Build Coastguard Worker} 631*58b9f456SAndroid Build Coastguard Worker/* BarTest is NOT registered */ 632*58b9f456SAndroid Build Coastguard WorkerBENCHMARK_REGISTER_F(MyFixture, BarTest)->Threads(2); 633*58b9f456SAndroid Build Coastguard Worker/* BarTest is now registered */ 634*58b9f456SAndroid Build Coastguard Worker``` 635*58b9f456SAndroid Build Coastguard Worker 636*58b9f456SAndroid Build Coastguard Worker### Templated fixtures 637*58b9f456SAndroid Build Coastguard WorkerAlso you can create templated fixture by using the following macros: 638*58b9f456SAndroid Build Coastguard Worker 639*58b9f456SAndroid Build Coastguard Worker* `BENCHMARK_TEMPLATE_F(ClassName, Method, ...)` 640*58b9f456SAndroid Build Coastguard Worker* `BENCHMARK_TEMPLATE_DEFINE_F(ClassName, Method, ...)` 641*58b9f456SAndroid Build Coastguard Worker 642*58b9f456SAndroid Build Coastguard WorkerFor example: 643*58b9f456SAndroid Build Coastguard Worker```c++ 644*58b9f456SAndroid Build Coastguard Workertemplate<typename T> 645*58b9f456SAndroid Build Coastguard Workerclass MyFixture : public benchmark::Fixture {}; 646*58b9f456SAndroid Build Coastguard Worker 647*58b9f456SAndroid Build Coastguard WorkerBENCHMARK_TEMPLATE_F(MyFixture, IntTest, int)(benchmark::State& st) { 648*58b9f456SAndroid Build Coastguard Worker for (auto _ : st) { 649*58b9f456SAndroid Build Coastguard Worker ... 650*58b9f456SAndroid Build Coastguard Worker } 651*58b9f456SAndroid Build Coastguard Worker} 652*58b9f456SAndroid Build Coastguard Worker 653*58b9f456SAndroid Build Coastguard WorkerBENCHMARK_TEMPLATE_DEFINE_F(MyFixture, DoubleTest, double)(benchmark::State& st) { 654*58b9f456SAndroid Build Coastguard Worker for (auto _ : st) { 655*58b9f456SAndroid Build Coastguard Worker ... 656*58b9f456SAndroid Build Coastguard Worker } 657*58b9f456SAndroid Build Coastguard Worker} 658*58b9f456SAndroid Build Coastguard Worker 659*58b9f456SAndroid Build Coastguard WorkerBENCHMARK_REGISTER_F(MyFixture, DoubleTest)->Threads(2); 660*58b9f456SAndroid Build Coastguard Worker``` 661*58b9f456SAndroid Build Coastguard Worker 662*58b9f456SAndroid Build Coastguard Worker## User-defined counters 663*58b9f456SAndroid Build Coastguard Worker 664*58b9f456SAndroid Build Coastguard WorkerYou can add your own counters with user-defined names. The example below 665*58b9f456SAndroid Build Coastguard Workerwill add columns "Foo", "Bar" and "Baz" in its output: 666*58b9f456SAndroid Build Coastguard Worker 667*58b9f456SAndroid Build Coastguard Worker```c++ 668*58b9f456SAndroid Build Coastguard Workerstatic void UserCountersExample1(benchmark::State& state) { 669*58b9f456SAndroid Build Coastguard Worker double numFoos = 0, numBars = 0, numBazs = 0; 670*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 671*58b9f456SAndroid Build Coastguard Worker // ... count Foo,Bar,Baz events 672*58b9f456SAndroid Build Coastguard Worker } 673*58b9f456SAndroid Build Coastguard Worker state.counters["Foo"] = numFoos; 674*58b9f456SAndroid Build Coastguard Worker state.counters["Bar"] = numBars; 675*58b9f456SAndroid Build Coastguard Worker state.counters["Baz"] = numBazs; 676*58b9f456SAndroid Build Coastguard Worker} 677*58b9f456SAndroid Build Coastguard Worker``` 678*58b9f456SAndroid Build Coastguard Worker 679*58b9f456SAndroid Build Coastguard WorkerThe `state.counters` object is a `std::map` with `std::string` keys 680*58b9f456SAndroid Build Coastguard Workerand `Counter` values. The latter is a `double`-like class, via an implicit 681*58b9f456SAndroid Build Coastguard Workerconversion to `double&`. Thus you can use all of the standard arithmetic 682*58b9f456SAndroid Build Coastguard Workerassignment operators (`=,+=,-=,*=,/=`) to change the value of each counter. 683*58b9f456SAndroid Build Coastguard Worker 684*58b9f456SAndroid Build Coastguard WorkerIn multithreaded benchmarks, each counter is set on the calling thread only. 685*58b9f456SAndroid Build Coastguard WorkerWhen the benchmark finishes, the counters from each thread will be summed; 686*58b9f456SAndroid Build Coastguard Workerthe resulting sum is the value which will be shown for the benchmark. 687*58b9f456SAndroid Build Coastguard Worker 688*58b9f456SAndroid Build Coastguard WorkerThe `Counter` constructor accepts three parameters: the value as a `double` 689*58b9f456SAndroid Build Coastguard Worker; a bit flag which allows you to show counters as rates, and/or as per-thread 690*58b9f456SAndroid Build Coastguard Workeriteration, and/or as per-thread averages, and/or iteration invariants; 691*58b9f456SAndroid Build Coastguard Workerand a flag specifying the 'unit' - i.e. is 1k a 1000 (default, 692*58b9f456SAndroid Build Coastguard Worker`benchmark::Counter::OneK::kIs1000`), or 1024 693*58b9f456SAndroid Build Coastguard Worker(`benchmark::Counter::OneK::kIs1024`)? 694*58b9f456SAndroid Build Coastguard Worker 695*58b9f456SAndroid Build Coastguard Worker```c++ 696*58b9f456SAndroid Build Coastguard Worker // sets a simple counter 697*58b9f456SAndroid Build Coastguard Worker state.counters["Foo"] = numFoos; 698*58b9f456SAndroid Build Coastguard Worker 699*58b9f456SAndroid Build Coastguard Worker // Set the counter as a rate. It will be presented divided 700*58b9f456SAndroid Build Coastguard Worker // by the duration of the benchmark. 701*58b9f456SAndroid Build Coastguard Worker state.counters["FooRate"] = Counter(numFoos, benchmark::Counter::kIsRate); 702*58b9f456SAndroid Build Coastguard Worker 703*58b9f456SAndroid Build Coastguard Worker // Set the counter as a thread-average quantity. It will 704*58b9f456SAndroid Build Coastguard Worker // be presented divided by the number of threads. 705*58b9f456SAndroid Build Coastguard Worker state.counters["FooAvg"] = Counter(numFoos, benchmark::Counter::kAvgThreads); 706*58b9f456SAndroid Build Coastguard Worker 707*58b9f456SAndroid Build Coastguard Worker // There's also a combined flag: 708*58b9f456SAndroid Build Coastguard Worker state.counters["FooAvgRate"] = Counter(numFoos,benchmark::Counter::kAvgThreadsRate); 709*58b9f456SAndroid Build Coastguard Worker 710*58b9f456SAndroid Build Coastguard Worker // This says that we process with the rate of state.range(0) bytes every iteration: 711*58b9f456SAndroid Build Coastguard Worker state.counters["BytesProcessed"] = Counter(state.range(0), benchmark::Counter::kIsIterationInvariantRate, benchmark::Counter::OneK::kIs1024); 712*58b9f456SAndroid Build Coastguard Worker``` 713*58b9f456SAndroid Build Coastguard Worker 714*58b9f456SAndroid Build Coastguard WorkerWhen you're compiling in C++11 mode or later you can use `insert()` with 715*58b9f456SAndroid Build Coastguard Worker`std::initializer_list`: 716*58b9f456SAndroid Build Coastguard Worker 717*58b9f456SAndroid Build Coastguard Worker```c++ 718*58b9f456SAndroid Build Coastguard Worker // With C++11, this can be done: 719*58b9f456SAndroid Build Coastguard Worker state.counters.insert({{"Foo", numFoos}, {"Bar", numBars}, {"Baz", numBazs}}); 720*58b9f456SAndroid Build Coastguard Worker // ... instead of: 721*58b9f456SAndroid Build Coastguard Worker state.counters["Foo"] = numFoos; 722*58b9f456SAndroid Build Coastguard Worker state.counters["Bar"] = numBars; 723*58b9f456SAndroid Build Coastguard Worker state.counters["Baz"] = numBazs; 724*58b9f456SAndroid Build Coastguard Worker``` 725*58b9f456SAndroid Build Coastguard Worker 726*58b9f456SAndroid Build Coastguard Worker### Counter reporting 727*58b9f456SAndroid Build Coastguard Worker 728*58b9f456SAndroid Build Coastguard WorkerWhen using the console reporter, by default, user counters are are printed at 729*58b9f456SAndroid Build Coastguard Workerthe end after the table, the same way as ``bytes_processed`` and 730*58b9f456SAndroid Build Coastguard Worker``items_processed``. This is best for cases in which there are few counters, 731*58b9f456SAndroid Build Coastguard Workeror where there are only a couple of lines per benchmark. Here's an example of 732*58b9f456SAndroid Build Coastguard Workerthe default output: 733*58b9f456SAndroid Build Coastguard Worker 734*58b9f456SAndroid Build Coastguard Worker``` 735*58b9f456SAndroid Build Coastguard Worker------------------------------------------------------------------------------ 736*58b9f456SAndroid Build Coastguard WorkerBenchmark Time CPU Iterations UserCounters... 737*58b9f456SAndroid Build Coastguard Worker------------------------------------------------------------------------------ 738*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:8 2248 ns 10277 ns 68808 Bar=16 Bat=40 Baz=24 Foo=8 739*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:1 9797 ns 9788 ns 71523 Bar=2 Bat=5 Baz=3 Foo=1024m 740*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:2 4924 ns 9842 ns 71036 Bar=4 Bat=10 Baz=6 Foo=2 741*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:4 2589 ns 10284 ns 68012 Bar=8 Bat=20 Baz=12 Foo=4 742*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:8 2212 ns 10287 ns 68040 Bar=16 Bat=40 Baz=24 Foo=8 743*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:16 1782 ns 10278 ns 68144 Bar=32 Bat=80 Baz=48 Foo=16 744*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:32 1291 ns 10296 ns 68256 Bar=64 Bat=160 Baz=96 Foo=32 745*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:4 2615 ns 10307 ns 68040 Bar=8 Bat=20 Baz=12 Foo=4 746*58b9f456SAndroid Build Coastguard WorkerBM_Factorial 26 ns 26 ns 26608979 40320 747*58b9f456SAndroid Build Coastguard WorkerBM_Factorial/real_time 26 ns 26 ns 26587936 40320 748*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/1 16 ns 16 ns 45704255 0 749*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/8 73 ns 73 ns 9520927 3.28374 750*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/64 609 ns 609 ns 1140647 3.15746 751*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/512 4900 ns 4901 ns 142696 3.14355 752*58b9f456SAndroid Build Coastguard Worker``` 753*58b9f456SAndroid Build Coastguard Worker 754*58b9f456SAndroid Build Coastguard WorkerIf this doesn't suit you, you can print each counter as a table column by 755*58b9f456SAndroid Build Coastguard Workerpassing the flag `--benchmark_counters_tabular=true` to the benchmark 756*58b9f456SAndroid Build Coastguard Workerapplication. This is best for cases in which there are a lot of counters, or 757*58b9f456SAndroid Build Coastguard Workera lot of lines per individual benchmark. Note that this will trigger a 758*58b9f456SAndroid Build Coastguard Workerreprinting of the table header any time the counter set changes between 759*58b9f456SAndroid Build Coastguard Workerindividual benchmarks. Here's an example of corresponding output when 760*58b9f456SAndroid Build Coastguard Worker`--benchmark_counters_tabular=true` is passed: 761*58b9f456SAndroid Build Coastguard Worker 762*58b9f456SAndroid Build Coastguard Worker``` 763*58b9f456SAndroid Build Coastguard Worker--------------------------------------------------------------------------------------- 764*58b9f456SAndroid Build Coastguard WorkerBenchmark Time CPU Iterations Bar Bat Baz Foo 765*58b9f456SAndroid Build Coastguard Worker--------------------------------------------------------------------------------------- 766*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:8 2198 ns 9953 ns 70688 16 40 24 8 767*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:1 9504 ns 9504 ns 73787 2 5 3 1 768*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:2 4775 ns 9550 ns 72606 4 10 6 2 769*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:4 2508 ns 9951 ns 70332 8 20 12 4 770*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:8 2055 ns 9933 ns 70344 16 40 24 8 771*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:16 1610 ns 9946 ns 70720 32 80 48 16 772*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:32 1192 ns 9948 ns 70496 64 160 96 32 773*58b9f456SAndroid Build Coastguard WorkerBM_UserCounter/threads:4 2506 ns 9949 ns 70332 8 20 12 4 774*58b9f456SAndroid Build Coastguard Worker-------------------------------------------------------------- 775*58b9f456SAndroid Build Coastguard WorkerBenchmark Time CPU Iterations 776*58b9f456SAndroid Build Coastguard Worker-------------------------------------------------------------- 777*58b9f456SAndroid Build Coastguard WorkerBM_Factorial 26 ns 26 ns 26392245 40320 778*58b9f456SAndroid Build Coastguard WorkerBM_Factorial/real_time 26 ns 26 ns 26494107 40320 779*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/1 15 ns 15 ns 45571597 0 780*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/8 74 ns 74 ns 9450212 3.28374 781*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/64 595 ns 595 ns 1173901 3.15746 782*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/512 4752 ns 4752 ns 147380 3.14355 783*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/4k 37970 ns 37972 ns 18453 3.14184 784*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/32k 303733 ns 303744 ns 2305 3.14162 785*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/256k 2434095 ns 2434186 ns 288 3.1416 786*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePiRange/1024k 9721140 ns 9721413 ns 71 3.14159 787*58b9f456SAndroid Build Coastguard WorkerBM_CalculatePi/threads:8 2255 ns 9943 ns 70936 788*58b9f456SAndroid Build Coastguard Worker``` 789*58b9f456SAndroid Build Coastguard WorkerNote above the additional header printed when the benchmark changes from 790*58b9f456SAndroid Build Coastguard Worker``BM_UserCounter`` to ``BM_Factorial``. This is because ``BM_Factorial`` does 791*58b9f456SAndroid Build Coastguard Workernot have the same counter set as ``BM_UserCounter``. 792*58b9f456SAndroid Build Coastguard Worker 793*58b9f456SAndroid Build Coastguard Worker## Exiting Benchmarks in Error 794*58b9f456SAndroid Build Coastguard Worker 795*58b9f456SAndroid Build Coastguard WorkerWhen errors caused by external influences, such as file I/O and network 796*58b9f456SAndroid Build Coastguard Workercommunication, occur within a benchmark the 797*58b9f456SAndroid Build Coastguard Worker`State::SkipWithError(const char* msg)` function can be used to skip that run 798*58b9f456SAndroid Build Coastguard Workerof benchmark and report the error. Note that only future iterations of the 799*58b9f456SAndroid Build Coastguard Worker`KeepRunning()` are skipped. For the ranged-for version of the benchmark loop 800*58b9f456SAndroid Build Coastguard WorkerUsers must explicitly exit the loop, otherwise all iterations will be performed. 801*58b9f456SAndroid Build Coastguard WorkerUsers may explicitly return to exit the benchmark immediately. 802*58b9f456SAndroid Build Coastguard Worker 803*58b9f456SAndroid Build Coastguard WorkerThe `SkipWithError(...)` function may be used at any point within the benchmark, 804*58b9f456SAndroid Build Coastguard Workerincluding before and after the benchmark loop. 805*58b9f456SAndroid Build Coastguard Worker 806*58b9f456SAndroid Build Coastguard WorkerFor example: 807*58b9f456SAndroid Build Coastguard Worker 808*58b9f456SAndroid Build Coastguard Worker```c++ 809*58b9f456SAndroid Build Coastguard Workerstatic void BM_test(benchmark::State& state) { 810*58b9f456SAndroid Build Coastguard Worker auto resource = GetResource(); 811*58b9f456SAndroid Build Coastguard Worker if (!resource.good()) { 812*58b9f456SAndroid Build Coastguard Worker state.SkipWithError("Resource is not good!"); 813*58b9f456SAndroid Build Coastguard Worker // KeepRunning() loop will not be entered. 814*58b9f456SAndroid Build Coastguard Worker } 815*58b9f456SAndroid Build Coastguard Worker for (state.KeepRunning()) { 816*58b9f456SAndroid Build Coastguard Worker auto data = resource.read_data(); 817*58b9f456SAndroid Build Coastguard Worker if (!resource.good()) { 818*58b9f456SAndroid Build Coastguard Worker state.SkipWithError("Failed to read data!"); 819*58b9f456SAndroid Build Coastguard Worker break; // Needed to skip the rest of the iteration. 820*58b9f456SAndroid Build Coastguard Worker } 821*58b9f456SAndroid Build Coastguard Worker do_stuff(data); 822*58b9f456SAndroid Build Coastguard Worker } 823*58b9f456SAndroid Build Coastguard Worker} 824*58b9f456SAndroid Build Coastguard Worker 825*58b9f456SAndroid Build Coastguard Workerstatic void BM_test_ranged_fo(benchmark::State & state) { 826*58b9f456SAndroid Build Coastguard Worker state.SkipWithError("test will not be entered"); 827*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) { 828*58b9f456SAndroid Build Coastguard Worker state.SkipWithError("Failed!"); 829*58b9f456SAndroid Build Coastguard Worker break; // REQUIRED to prevent all further iterations. 830*58b9f456SAndroid Build Coastguard Worker } 831*58b9f456SAndroid Build Coastguard Worker} 832*58b9f456SAndroid Build Coastguard Worker``` 833*58b9f456SAndroid Build Coastguard Worker 834*58b9f456SAndroid Build Coastguard Worker## Running a subset of the benchmarks 835*58b9f456SAndroid Build Coastguard Worker 836*58b9f456SAndroid Build Coastguard WorkerThe `--benchmark_filter=<regex>` option can be used to only run the benchmarks 837*58b9f456SAndroid Build Coastguard Workerwhich match the specified `<regex>`. For example: 838*58b9f456SAndroid Build Coastguard Worker 839*58b9f456SAndroid Build Coastguard Worker```bash 840*58b9f456SAndroid Build Coastguard Worker$ ./run_benchmarks.x --benchmark_filter=BM_memcpy/32 841*58b9f456SAndroid Build Coastguard WorkerRun on (1 X 2300 MHz CPU ) 842*58b9f456SAndroid Build Coastguard Worker2016-06-25 19:34:24 843*58b9f456SAndroid Build Coastguard WorkerBenchmark Time CPU Iterations 844*58b9f456SAndroid Build Coastguard Worker---------------------------------------------------- 845*58b9f456SAndroid Build Coastguard WorkerBM_memcpy/32 11 ns 11 ns 79545455 846*58b9f456SAndroid Build Coastguard WorkerBM_memcpy/32k 2181 ns 2185 ns 324074 847*58b9f456SAndroid Build Coastguard WorkerBM_memcpy/32 12 ns 12 ns 54687500 848*58b9f456SAndroid Build Coastguard WorkerBM_memcpy/32k 1834 ns 1837 ns 357143 849*58b9f456SAndroid Build Coastguard Worker``` 850*58b9f456SAndroid Build Coastguard Worker 851*58b9f456SAndroid Build Coastguard Worker## Runtime and reporting considerations 852*58b9f456SAndroid Build Coastguard WorkerWhen the benchmark binary is executed, each benchmark function is run serially. 853*58b9f456SAndroid Build Coastguard WorkerThe number of iterations to run is determined dynamically by running the 854*58b9f456SAndroid Build Coastguard Workerbenchmark a few times and measuring the time taken and ensuring that the 855*58b9f456SAndroid Build Coastguard Workerultimate result will be statistically stable. As such, faster benchmark 856*58b9f456SAndroid Build Coastguard Workerfunctions will be run for more iterations than slower benchmark functions, and 857*58b9f456SAndroid Build Coastguard Workerthe number of iterations is thus reported. 858*58b9f456SAndroid Build Coastguard Worker 859*58b9f456SAndroid Build Coastguard WorkerIn all cases, the number of iterations for which the benchmark is run is 860*58b9f456SAndroid Build Coastguard Workergoverned by the amount of time the benchmark takes. Concretely, the number of 861*58b9f456SAndroid Build Coastguard Workeriterations is at least one, not more than 1e9, until CPU time is greater than 862*58b9f456SAndroid Build Coastguard Workerthe minimum time, or the wallclock time is 5x minimum time. The minimum time is 863*58b9f456SAndroid Build Coastguard Workerset per benchmark by calling `MinTime` on the registered benchmark object. 864*58b9f456SAndroid Build Coastguard Worker 865*58b9f456SAndroid Build Coastguard WorkerAverage timings are then reported over the iterations run. If multiple 866*58b9f456SAndroid Build Coastguard Workerrepetitions are requested using the `--benchmark_repetitions` command-line 867*58b9f456SAndroid Build Coastguard Workeroption, or at registration time, the benchmark function will be run several 868*58b9f456SAndroid Build Coastguard Workertimes and statistical results across these repetitions will also be reported. 869*58b9f456SAndroid Build Coastguard Worker 870*58b9f456SAndroid Build Coastguard WorkerAs well as the per-benchmark entries, a preamble in the report will include 871*58b9f456SAndroid Build Coastguard Workerinformation about the machine on which the benchmarks are run. 872*58b9f456SAndroid Build Coastguard Worker 873*58b9f456SAndroid Build Coastguard Worker### Output Formats 874*58b9f456SAndroid Build Coastguard WorkerThe library supports multiple output formats. Use the 875*58b9f456SAndroid Build Coastguard Worker`--benchmark_format=<console|json|csv>` flag to set the format type. `console` 876*58b9f456SAndroid Build Coastguard Workeris the default format. 877*58b9f456SAndroid Build Coastguard Worker 878*58b9f456SAndroid Build Coastguard WorkerThe Console format is intended to be a human readable format. By default 879*58b9f456SAndroid Build Coastguard Workerthe format generates color output. Context is output on stderr and the 880*58b9f456SAndroid Build Coastguard Workertabular data on stdout. Example tabular output looks like: 881*58b9f456SAndroid Build Coastguard Worker``` 882*58b9f456SAndroid Build Coastguard WorkerBenchmark Time(ns) CPU(ns) Iterations 883*58b9f456SAndroid Build Coastguard Worker---------------------------------------------------------------------- 884*58b9f456SAndroid Build Coastguard WorkerBM_SetInsert/1024/1 28928 29349 23853 133.097kB/s 33.2742k items/s 885*58b9f456SAndroid Build Coastguard WorkerBM_SetInsert/1024/8 32065 32913 21375 949.487kB/s 237.372k items/s 886*58b9f456SAndroid Build Coastguard WorkerBM_SetInsert/1024/10 33157 33648 21431 1.13369MB/s 290.225k items/s 887*58b9f456SAndroid Build Coastguard Worker``` 888*58b9f456SAndroid Build Coastguard Worker 889*58b9f456SAndroid Build Coastguard WorkerThe JSON format outputs human readable json split into two top level attributes. 890*58b9f456SAndroid Build Coastguard WorkerThe `context` attribute contains information about the run in general, including 891*58b9f456SAndroid Build Coastguard Workerinformation about the CPU and the date. 892*58b9f456SAndroid Build Coastguard WorkerThe `benchmarks` attribute contains a list of every benchmark run. Example json 893*58b9f456SAndroid Build Coastguard Workeroutput looks like: 894*58b9f456SAndroid Build Coastguard Worker```json 895*58b9f456SAndroid Build Coastguard Worker{ 896*58b9f456SAndroid Build Coastguard Worker "context": { 897*58b9f456SAndroid Build Coastguard Worker "date": "2015/03/17-18:40:25", 898*58b9f456SAndroid Build Coastguard Worker "num_cpus": 40, 899*58b9f456SAndroid Build Coastguard Worker "mhz_per_cpu": 2801, 900*58b9f456SAndroid Build Coastguard Worker "cpu_scaling_enabled": false, 901*58b9f456SAndroid Build Coastguard Worker "build_type": "debug" 902*58b9f456SAndroid Build Coastguard Worker }, 903*58b9f456SAndroid Build Coastguard Worker "benchmarks": [ 904*58b9f456SAndroid Build Coastguard Worker { 905*58b9f456SAndroid Build Coastguard Worker "name": "BM_SetInsert/1024/1", 906*58b9f456SAndroid Build Coastguard Worker "iterations": 94877, 907*58b9f456SAndroid Build Coastguard Worker "real_time": 29275, 908*58b9f456SAndroid Build Coastguard Worker "cpu_time": 29836, 909*58b9f456SAndroid Build Coastguard Worker "bytes_per_second": 134066, 910*58b9f456SAndroid Build Coastguard Worker "items_per_second": 33516 911*58b9f456SAndroid Build Coastguard Worker }, 912*58b9f456SAndroid Build Coastguard Worker { 913*58b9f456SAndroid Build Coastguard Worker "name": "BM_SetInsert/1024/8", 914*58b9f456SAndroid Build Coastguard Worker "iterations": 21609, 915*58b9f456SAndroid Build Coastguard Worker "real_time": 32317, 916*58b9f456SAndroid Build Coastguard Worker "cpu_time": 32429, 917*58b9f456SAndroid Build Coastguard Worker "bytes_per_second": 986770, 918*58b9f456SAndroid Build Coastguard Worker "items_per_second": 246693 919*58b9f456SAndroid Build Coastguard Worker }, 920*58b9f456SAndroid Build Coastguard Worker { 921*58b9f456SAndroid Build Coastguard Worker "name": "BM_SetInsert/1024/10", 922*58b9f456SAndroid Build Coastguard Worker "iterations": 21393, 923*58b9f456SAndroid Build Coastguard Worker "real_time": 32724, 924*58b9f456SAndroid Build Coastguard Worker "cpu_time": 33355, 925*58b9f456SAndroid Build Coastguard Worker "bytes_per_second": 1199226, 926*58b9f456SAndroid Build Coastguard Worker "items_per_second": 299807 927*58b9f456SAndroid Build Coastguard Worker } 928*58b9f456SAndroid Build Coastguard Worker ] 929*58b9f456SAndroid Build Coastguard Worker} 930*58b9f456SAndroid Build Coastguard Worker``` 931*58b9f456SAndroid Build Coastguard Worker 932*58b9f456SAndroid Build Coastguard WorkerThe CSV format outputs comma-separated values. The `context` is output on stderr 933*58b9f456SAndroid Build Coastguard Workerand the CSV itself on stdout. Example CSV output looks like: 934*58b9f456SAndroid Build Coastguard Worker``` 935*58b9f456SAndroid Build Coastguard Workername,iterations,real_time,cpu_time,bytes_per_second,items_per_second,label 936*58b9f456SAndroid Build Coastguard Worker"BM_SetInsert/1024/1",65465,17890.7,8407.45,475768,118942, 937*58b9f456SAndroid Build Coastguard Worker"BM_SetInsert/1024/8",116606,18810.1,9766.64,3.27646e+06,819115, 938*58b9f456SAndroid Build Coastguard Worker"BM_SetInsert/1024/10",106365,17238.4,8421.53,4.74973e+06,1.18743e+06, 939*58b9f456SAndroid Build Coastguard Worker``` 940*58b9f456SAndroid Build Coastguard Worker 941*58b9f456SAndroid Build Coastguard Worker### Output Files 942*58b9f456SAndroid Build Coastguard WorkerThe library supports writing the output of the benchmark to a file specified 943*58b9f456SAndroid Build Coastguard Workerby `--benchmark_out=<filename>`. The format of the output can be specified 944*58b9f456SAndroid Build Coastguard Workerusing `--benchmark_out_format={json|console|csv}`. Specifying 945*58b9f456SAndroid Build Coastguard Worker`--benchmark_out` does not suppress the console output. 946*58b9f456SAndroid Build Coastguard Worker 947*58b9f456SAndroid Build Coastguard Worker## Result comparison 948*58b9f456SAndroid Build Coastguard Worker 949*58b9f456SAndroid Build Coastguard WorkerIt is possible to compare the benchmarking results. See [Additional Tooling Documentation](docs/tools.md) 950*58b9f456SAndroid Build Coastguard Worker 951*58b9f456SAndroid Build Coastguard Worker## Debug vs Release 952*58b9f456SAndroid Build Coastguard WorkerBy default, benchmark builds as a debug library. You will see a warning in the 953*58b9f456SAndroid Build Coastguard Workeroutput when this is the case. To build it as a release library instead, use: 954*58b9f456SAndroid Build Coastguard Worker 955*58b9f456SAndroid Build Coastguard Worker``` 956*58b9f456SAndroid Build Coastguard Workercmake -DCMAKE_BUILD_TYPE=Release 957*58b9f456SAndroid Build Coastguard Worker``` 958*58b9f456SAndroid Build Coastguard Worker 959*58b9f456SAndroid Build Coastguard WorkerTo enable link-time optimisation, use 960*58b9f456SAndroid Build Coastguard Worker 961*58b9f456SAndroid Build Coastguard Worker``` 962*58b9f456SAndroid Build Coastguard Workercmake -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_LTO=true 963*58b9f456SAndroid Build Coastguard Worker``` 964*58b9f456SAndroid Build Coastguard Worker 965*58b9f456SAndroid Build Coastguard WorkerIf you are using gcc, you might need to set `GCC_AR` and `GCC_RANLIB` cmake 966*58b9f456SAndroid Build Coastguard Workercache variables, if autodetection fails. 967*58b9f456SAndroid Build Coastguard Worker 968*58b9f456SAndroid Build Coastguard WorkerIf you are using clang, you may need to set `LLVMAR_EXECUTABLE`, 969*58b9f456SAndroid Build Coastguard Worker`LLVMNM_EXECUTABLE` and `LLVMRANLIB_EXECUTABLE` cmake cache variables. 970*58b9f456SAndroid Build Coastguard Worker 971*58b9f456SAndroid Build Coastguard Worker## Compiler Support 972*58b9f456SAndroid Build Coastguard Worker 973*58b9f456SAndroid Build Coastguard WorkerGoogle Benchmark uses C++11 when building the library. As such we require 974*58b9f456SAndroid Build Coastguard Workera modern C++ toolchain, both compiler and standard library. 975*58b9f456SAndroid Build Coastguard Worker 976*58b9f456SAndroid Build Coastguard WorkerThe following minimum versions are strongly recommended build the library: 977*58b9f456SAndroid Build Coastguard Worker 978*58b9f456SAndroid Build Coastguard Worker* GCC 4.8 979*58b9f456SAndroid Build Coastguard Worker* Clang 3.4 980*58b9f456SAndroid Build Coastguard Worker* Visual Studio 2013 981*58b9f456SAndroid Build Coastguard Worker* Intel 2015 Update 1 982*58b9f456SAndroid Build Coastguard Worker 983*58b9f456SAndroid Build Coastguard WorkerAnything older *may* work. 984*58b9f456SAndroid Build Coastguard Worker 985*58b9f456SAndroid Build Coastguard WorkerNote: Using the library and its headers in C++03 is supported. C++11 is only 986*58b9f456SAndroid Build Coastguard Workerrequired to build the library. 987*58b9f456SAndroid Build Coastguard Worker 988*58b9f456SAndroid Build Coastguard Worker## Disable CPU frequency scaling 989*58b9f456SAndroid Build Coastguard WorkerIf you see this error: 990*58b9f456SAndroid Build Coastguard Worker``` 991*58b9f456SAndroid Build Coastguard Worker***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead. 992*58b9f456SAndroid Build Coastguard Worker``` 993*58b9f456SAndroid Build Coastguard Workeryou might want to disable the CPU frequency scaling while running the benchmark: 994*58b9f456SAndroid Build Coastguard Worker```bash 995*58b9f456SAndroid Build Coastguard Workersudo cpupower frequency-set --governor performance 996*58b9f456SAndroid Build Coastguard Worker./mybench 997*58b9f456SAndroid Build Coastguard Workersudo cpupower frequency-set --governor powersave 998*58b9f456SAndroid Build Coastguard Worker``` 999