benchmark/test/basic_test.cc
Go to the documentation of this file.
1 
2 #include "benchmark/benchmark.h"
3 
4 #define BASIC_BENCHMARK_TEST(x) BENCHMARK(x)->Arg(8)->Arg(512)->Arg(8192)
5 
7  for (auto _ : state) {
8  benchmark::DoNotOptimize(state.iterations());
9  }
10 }
12 BENCHMARK(BM_empty)->ThreadPerCpu();
13 
15  for (auto _ : state) {
16  for (int x = 0; x < state.range(0); ++x) {
18  }
19  }
20 }
22 BASIC_BENCHMARK_TEST(BM_spin_empty)->ThreadPerCpu();
23 
25  for (int i = 0; i < state.range(0); ++i) {
27  }
28  for (auto _ : state) {
29  for (int i = 0; i < state.range(0); ++i) {
31  }
32  }
33 }
36 
38  for (auto _ : state) {
39  state.PauseTiming();
40  for (int i = 0; i < state.range(0); ++i) {
42  }
43  state.ResumeTiming();
44  for (int i = 0; i < state.range(0); ++i) {
46  }
47  }
48 }
51 
53  for (auto _ : state) {
54  state.PauseTiming();
55  state.ResumeTiming();
56  }
57 }
59 BENCHMARK(BM_pause_during)->ThreadPerCpu();
60 BENCHMARK(BM_pause_during)->UseRealTime();
61 BENCHMARK(BM_pause_during)->UseRealTime()->ThreadPerCpu();
62 
64  for (auto _ : state) {
65  for (int i = 0; i < state.range(0); ++i) {
67  }
68  }
69  for (int i = 0; i < state.range(0); ++i) {
71  }
72 }
75 
77  for (int i = 0; i < state.range(0); ++i) {
79  }
80  for (auto _ : state) {
81  for (int i = 0; i < state.range(0); ++i) {
83  }
84  }
85  for (int i = 0; i < state.range(0); ++i) {
87  }
88 }
91 
93  for (auto _ : state) {
94  }
95 }
97 BENCHMARK(BM_empty_stop_start)->ThreadPerCpu();
98 
99 
101  benchmark::IterationCount iter_count = 0;
102  assert(iter_count == state.iterations());
103  while (state.KeepRunning()) {
104  ++iter_count;
105  }
106  assert(iter_count == state.iterations());
107 }
109 
111  // Choose a batch size >1000 to skip the typical runs with iteration
112  // targets of 10, 100 and 1000. If these are not actually skipped the
113  // bug would be detectable as consecutive runs with the same iteration
114  // count. Below we assert that this does not happen.
115  const benchmark::IterationCount batch_size = 1009;
116 
117  static benchmark::IterationCount prior_iter_count = 0;
118  benchmark::IterationCount iter_count = 0;
119  while (state.KeepRunningBatch(batch_size)) {
120  iter_count += batch_size;
121  }
122  assert(state.iterations() == iter_count);
123 
124  // Verify that the iteration count always increases across runs (see
125  // comment above).
126  assert(iter_count == batch_size // max_iterations == 1
127  || iter_count > prior_iter_count); // max_iterations > batch_size
128  prior_iter_count = iter_count;
129 }
130 // Register with a fixed repetition count to establish the invariant that
131 // the iteration count should always change across runs. This overrides
132 // the --benchmark_repetitions command line flag, which would otherwise
133 // cause this test to fail if set > 1.
134 BENCHMARK(BM_KeepRunningBatch)->Repetitions(1);
135 
137  benchmark::IterationCount iter_count = 0;
138  for (auto _ : state) {
139  ++iter_count;
140  }
141  assert(iter_count == state.max_iterations);
142 }
144 
145 // Ensure that StateIterator provides all the necessary typedefs required to
146 // instantiate std::iterator_traits.
147 static_assert(std::is_same<
150 
BM_pause_during
void BM_pause_during(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:52
BM_empty
void BM_empty(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:6
BENCHMARK
BENCHMARK(BM_empty)
BM_spin_pause_after
void BM_spin_pause_after(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:63
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
BM_RangedFor
void BM_RangedFor(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:136
benchmark::IterationCount
uint64_t IterationCount
Definition: benchmark/include/benchmark/benchmark.h:451
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
absl::compare_internal::value_type
int8_t value_type
Definition: abseil-cpp/absl/types/compare.h:45
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
BM_spin_pause_before
void BM_spin_pause_before(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:24
BM_KeepRunning
void BM_KeepRunning(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:100
BM_KeepRunningBatch
void BM_KeepRunningBatch(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:110
value
const char * value
Definition: hpack_parser_table.cc:165
BASIC_BENCHMARK_TEST
#define BASIC_BENCHMARK_TEST(x)
Definition: benchmark/test/basic_test.cc:4
BM_spin_pause_during
void BM_spin_pause_during(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:37
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
BM_spin_empty
void BM_spin_empty(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:14
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
BM_spin_pause_before_and_after
void BM_spin_pause_before_and_after(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:76
BENCHMARK_MAIN
BENCHMARK_MAIN()
benchmark::State::StateIterator::Value
Definition: benchmark/include/benchmark/benchmark.h:777
BM_empty_stop_start
void BM_empty_stop_start(benchmark::State &state)
Definition: benchmark/test/basic_test.cc:92
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:45