[Workflow] Try to fix code-formatter failing to find changes in some cases.
[llvm-project.git] / third-party / benchmark / test / options_test.cc
blobd424d40b9518d1b076f1ab129754813708c8923d
1 #include <chrono>
2 #include <thread>
4 #include "benchmark/benchmark.h"
6 #if defined(NDEBUG)
7 #undef NDEBUG
8 #endif
9 #include <cassert>
11 void BM_basic(benchmark::State& state) {
12 for (auto _ : state) {
16 void BM_basic_slow(benchmark::State& state) {
17 std::chrono::milliseconds sleep_duration(state.range(0));
18 for (auto _ : state) {
19 std::this_thread::sleep_for(
20 std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
24 BENCHMARK(BM_basic);
25 BENCHMARK(BM_basic)->Arg(42);
26 BENCHMARK(BM_basic_slow)->Arg(10)->Unit(benchmark::kNanosecond);
27 BENCHMARK(BM_basic_slow)->Arg(100)->Unit(benchmark::kMicrosecond);
28 BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kMillisecond);
29 BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kSecond);
30 BENCHMARK(BM_basic)->Range(1, 8);
31 BENCHMARK(BM_basic)->RangeMultiplier(2)->Range(1, 8);
32 BENCHMARK(BM_basic)->DenseRange(10, 15);
33 BENCHMARK(BM_basic)->Args({42, 42});
34 BENCHMARK(BM_basic)->Ranges({{64, 512}, {64, 512}});
35 BENCHMARK(BM_basic)->MinTime(0.7);
36 BENCHMARK(BM_basic)->UseRealTime();
37 BENCHMARK(BM_basic)->ThreadRange(2, 4);
38 BENCHMARK(BM_basic)->ThreadPerCpu();
39 BENCHMARK(BM_basic)->Repetitions(3);
40 BENCHMARK(BM_basic)
41 ->RangeMultiplier(std::numeric_limits<int>::max())
42 ->Range(std::numeric_limits<int64_t>::min(),
43 std::numeric_limits<int64_t>::max());
45 // Negative ranges
46 BENCHMARK(BM_basic)->Range(-64, -1);
47 BENCHMARK(BM_basic)->RangeMultiplier(4)->Range(-8, 8);
48 BENCHMARK(BM_basic)->DenseRange(-2, 2, 1);
49 BENCHMARK(BM_basic)->Ranges({{-64, 1}, {-8, -1}});
51 void CustomArgs(benchmark::internal::Benchmark* b) {
52 for (int i = 0; i < 10; ++i) {
53 b->Arg(i);
57 BENCHMARK(BM_basic)->Apply(CustomArgs);
59 void BM_explicit_iteration_count(benchmark::State& state) {
60 // Test that benchmarks specified with an explicit iteration count are
61 // only run once.
62 static bool invoked_before = false;
63 assert(!invoked_before);
64 invoked_before = true;
66 // Test that the requested iteration count is respected.
67 assert(state.max_iterations == 42);
68 size_t actual_iterations = 0;
69 for (auto _ : state) ++actual_iterations;
70 assert(state.iterations() == state.max_iterations);
71 assert(state.iterations() == 42);
73 BENCHMARK(BM_explicit_iteration_count)->Iterations(42);
75 BENCHMARK_MAIN();