1#include <chrono>
2#include <thread>
3
4#include "benchmark/benchmark.h"
5
6#if defined(NDEBUG)
7#undef NDEBUG
8#endif
9#include <cassert>
10
11void BM_basic(benchmark::State& state) {
12 for (auto _ : state) {
13 }
14}
15
16void BM_basic_slow(benchmark::State& state) {
17 std::chrono::milliseconds sleep_duration(state.range(pos: 0));
18 for (auto _ : state) {
19 std::this_thread::sleep_for(
20 rtime: std::chrono::duration_cast<std::chrono::nanoseconds>(d: sleep_duration));
21 }
22}
23
24BENCHMARK(BM_basic);
25BENCHMARK(BM_basic)->Arg(x: 42);
26BENCHMARK(BM_basic_slow)->Arg(x: 10)->Unit(unit: benchmark::kNanosecond);
27BENCHMARK(BM_basic_slow)->Arg(x: 100)->Unit(unit: benchmark::kMicrosecond);
28BENCHMARK(BM_basic_slow)->Arg(x: 1000)->Unit(unit: benchmark::kMillisecond);
29BENCHMARK(BM_basic_slow)->Arg(x: 1000)->Unit(unit: benchmark::kSecond);
30BENCHMARK(BM_basic)->Range(start: 1, limit: 8);
31BENCHMARK(BM_basic)->RangeMultiplier(multiplier: 2)->Range(start: 1, limit: 8);
32BENCHMARK(BM_basic)->DenseRange(start: 10, limit: 15);
33BENCHMARK(BM_basic)->Args(args: {42, 42});
34BENCHMARK(BM_basic)->Ranges(ranges: {{64, 512}, {64, 512}});
35BENCHMARK(BM_basic)->MinTime(t: 0.7);
36BENCHMARK(BM_basic)->MinWarmUpTime(t: 0.8);
37BENCHMARK(BM_basic)->MinTime(t: 0.1)->MinWarmUpTime(t: 0.2);
38BENCHMARK(BM_basic)->UseRealTime();
39BENCHMARK(BM_basic)->ThreadRange(min_threads: 2, max_threads: 4);
40BENCHMARK(BM_basic)->ThreadPerCpu();
41BENCHMARK(BM_basic)->Repetitions(n: 3);
42BENCHMARK(BM_basic)
43 ->RangeMultiplier(multiplier: std::numeric_limits<int>::max())
44 ->Range(start: std::numeric_limits<int64_t>::min(),
45 limit: std::numeric_limits<int64_t>::max());
46
47// Negative ranges
48BENCHMARK(BM_basic)->Range(start: -64, limit: -1);
49BENCHMARK(BM_basic)->RangeMultiplier(multiplier: 4)->Range(start: -8, limit: 8);
50BENCHMARK(BM_basic)->DenseRange(start: -2, limit: 2, step: 1);
51BENCHMARK(BM_basic)->Ranges(ranges: {{-64, 1}, {-8, -1}});
52
53void CustomArgs(benchmark::internal::Benchmark* b) {
54 for (int i = 0; i < 10; ++i) {
55 b->Arg(x: i);
56 }
57}
58
59BENCHMARK(BM_basic)->Apply(func: CustomArgs);
60
61void BM_explicit_iteration_count(benchmark::State& state) {
62 // Test that benchmarks specified with an explicit iteration count are
63 // only run once.
64 static bool invoked_before = false;
65 assert(!invoked_before);
66 invoked_before = true;
67
68 // Test that the requested iteration count is respected.
69 assert(state.max_iterations == 42);
70 for (auto _ : state) {
71 }
72 assert(state.iterations() == state.max_iterations);
73 assert(state.iterations() == 42);
74}
75BENCHMARK(BM_explicit_iteration_count)->Iterations(n: 42);
76
77BENCHMARK_MAIN();
78

source code of third-party/benchmark/test/options_test.cc