You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.5 KiB

  1. #include "benchmark/benchmark.h"
  2. #define BASIC_BENCHMARK_TEST(x) BENCHMARK(x)->Arg(8)->Arg(512)->Arg(8192)
  3. void BM_empty(benchmark::State& state) {
  4. for (auto _ : state) {
  5. benchmark::DoNotOptimize(state.iterations());
  6. }
  7. }
  8. BENCHMARK(BM_empty);
  9. BENCHMARK(BM_empty)->ThreadPerCpu();
  10. void BM_spin_empty(benchmark::State& state) {
  11. for (auto _ : state) {
  12. for (int x = 0; x < state.range(0); ++x) {
  13. benchmark::DoNotOptimize(x);
  14. }
  15. }
  16. }
  17. BASIC_BENCHMARK_TEST(BM_spin_empty);
  18. BASIC_BENCHMARK_TEST(BM_spin_empty)->ThreadPerCpu();
  19. void BM_spin_pause_before(benchmark::State& state) {
  20. for (int i = 0; i < state.range(0); ++i) {
  21. benchmark::DoNotOptimize(i);
  22. }
  23. for (auto _ : state) {
  24. for (int i = 0; i < state.range(0); ++i) {
  25. benchmark::DoNotOptimize(i);
  26. }
  27. }
  28. }
  29. BASIC_BENCHMARK_TEST(BM_spin_pause_before);
  30. BASIC_BENCHMARK_TEST(BM_spin_pause_before)->ThreadPerCpu();
  31. void BM_spin_pause_during(benchmark::State& state) {
  32. for (auto _ : state) {
  33. state.PauseTiming();
  34. for (int i = 0; i < state.range(0); ++i) {
  35. benchmark::DoNotOptimize(i);
  36. }
  37. state.ResumeTiming();
  38. for (int i = 0; i < state.range(0); ++i) {
  39. benchmark::DoNotOptimize(i);
  40. }
  41. }
  42. }
  43. BASIC_BENCHMARK_TEST(BM_spin_pause_during);
  44. BASIC_BENCHMARK_TEST(BM_spin_pause_during)->ThreadPerCpu();
  45. void BM_pause_during(benchmark::State& state) {
  46. for (auto _ : state) {
  47. state.PauseTiming();
  48. state.ResumeTiming();
  49. }
  50. }
  51. BENCHMARK(BM_pause_during);
  52. BENCHMARK(BM_pause_during)->ThreadPerCpu();
  53. BENCHMARK(BM_pause_during)->UseRealTime();
  54. BENCHMARK(BM_pause_during)->UseRealTime()->ThreadPerCpu();
  55. void BM_spin_pause_after(benchmark::State& state) {
  56. for (auto _ : state) {
  57. for (int i = 0; i < state.range(0); ++i) {
  58. benchmark::DoNotOptimize(i);
  59. }
  60. }
  61. for (int i = 0; i < state.range(0); ++i) {
  62. benchmark::DoNotOptimize(i);
  63. }
  64. }
  65. BASIC_BENCHMARK_TEST(BM_spin_pause_after);
  66. BASIC_BENCHMARK_TEST(BM_spin_pause_after)->ThreadPerCpu();
  67. void BM_spin_pause_before_and_after(benchmark::State& state) {
  68. for (int i = 0; i < state.range(0); ++i) {
  69. benchmark::DoNotOptimize(i);
  70. }
  71. for (auto _ : state) {
  72. for (int i = 0; i < state.range(0); ++i) {
  73. benchmark::DoNotOptimize(i);
  74. }
  75. }
  76. for (int i = 0; i < state.range(0); ++i) {
  77. benchmark::DoNotOptimize(i);
  78. }
  79. }
  80. BASIC_BENCHMARK_TEST(BM_spin_pause_before_and_after);
  81. BASIC_BENCHMARK_TEST(BM_spin_pause_before_and_after)->ThreadPerCpu();
  82. void BM_empty_stop_start(benchmark::State& state) {
  83. for (auto _ : state) {
  84. }
  85. }
  86. BENCHMARK(BM_empty_stop_start);
  87. BENCHMARK(BM_empty_stop_start)->ThreadPerCpu();
  88. void BM_KeepRunning(benchmark::State& state) {
  89. benchmark::IterationCount iter_count = 0;
  90. assert(iter_count == state.iterations());
  91. while (state.KeepRunning()) {
  92. ++iter_count;
  93. }
  94. assert(iter_count == state.iterations());
  95. }
  96. BENCHMARK(BM_KeepRunning);
  97. void BM_KeepRunningBatch(benchmark::State& state) {
  98. // Choose a batch size >1000 to skip the typical runs with iteration
  99. // targets of 10, 100 and 1000. If these are not actually skipped the
  100. // bug would be detectable as consecutive runs with the same iteration
  101. // count. Below we assert that this does not happen.
  102. const benchmark::IterationCount batch_size = 1009;
  103. static benchmark::IterationCount prior_iter_count = 0;
  104. benchmark::IterationCount iter_count = 0;
  105. while (state.KeepRunningBatch(batch_size)) {
  106. iter_count += batch_size;
  107. }
  108. assert(state.iterations() == iter_count);
  109. // Verify that the iteration count always increases across runs (see
  110. // comment above).
  111. assert(iter_count == batch_size // max_iterations == 1
  112. || iter_count > prior_iter_count); // max_iterations > batch_size
  113. prior_iter_count = iter_count;
  114. }
  115. // Register with a fixed repetition count to establish the invariant that
  116. // the iteration count should always change across runs. This overrides
  117. // the --benchmark_repetitions command line flag, which would otherwise
  118. // cause this test to fail if set > 1.
  119. BENCHMARK(BM_KeepRunningBatch)->Repetitions(1);
  120. void BM_RangedFor(benchmark::State& state) {
  121. benchmark::IterationCount iter_count = 0;
  122. for (auto _ : state) {
  123. ++iter_count;
  124. }
  125. assert(iter_count == state.max_iterations);
  126. }
  127. BENCHMARK(BM_RangedFor);
  128. // Ensure that StateIterator provides all the necessary typedefs required to
  129. // instantiate std::iterator_traits.
  130. static_assert(std::is_same<
  131. typename std::iterator_traits<benchmark::State::StateIterator>::value_type,
  132. typename benchmark::State::StateIterator::value_type>::value, "");
  133. BENCHMARK_MAIN();