LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
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.

180 lines
5.2 KiB

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