basic_test.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <cstddef>
  2. #include "benchmark/benchmark_api.h"
  3. #define BASIC_BENCHMARK_TEST(x) \
  4. BENCHMARK(x)->Arg(8)->Arg(512)->Arg(8192)
  5. void BM_empty(benchmark::State& state) {
  6. while (state.KeepRunning()) {
  7. benchmark::DoNotOptimize(state.iterations());
  8. }
  9. }
  10. BENCHMARK(BM_empty);
  11. BENCHMARK(BM_empty)->ThreadPerCpu();
  12. void BM_spin_empty(benchmark::State& state) {
  13. while (state.KeepRunning()) {
  14. for (int x = 0; x < state.range_x(); ++x) {
  15. benchmark::DoNotOptimize(x);
  16. }
  17. }
  18. }
  19. BASIC_BENCHMARK_TEST(BM_spin_empty);
  20. BASIC_BENCHMARK_TEST(BM_spin_empty)->ThreadPerCpu();
  21. void BM_spin_pause_before(benchmark::State& state) {
  22. for (int i = 0; i < state.range_x(); ++i) {
  23. benchmark::DoNotOptimize(i);
  24. }
  25. while(state.KeepRunning()) {
  26. for (int i = 0; i < state.range_x(); ++i) {
  27. benchmark::DoNotOptimize(i);
  28. }
  29. }
  30. }
  31. BASIC_BENCHMARK_TEST(BM_spin_pause_before);
  32. BASIC_BENCHMARK_TEST(BM_spin_pause_before)->ThreadPerCpu();
  33. void BM_spin_pause_during(benchmark::State& state) {
  34. while(state.KeepRunning()) {
  35. state.PauseTiming();
  36. for (int i = 0; i < state.range_x(); ++i) {
  37. benchmark::DoNotOptimize(i);
  38. }
  39. state.ResumeTiming();
  40. for (int i = 0; i < state.range_x(); ++i) {
  41. benchmark::DoNotOptimize(i);
  42. }
  43. }
  44. }
  45. BASIC_BENCHMARK_TEST(BM_spin_pause_during);
  46. BASIC_BENCHMARK_TEST(BM_spin_pause_during)->ThreadPerCpu();
  47. void BM_pause_during(benchmark::State& state) {
  48. while(state.KeepRunning()) {
  49. state.PauseTiming();
  50. state.ResumeTiming();
  51. }
  52. }
  53. BENCHMARK(BM_pause_during);
  54. BENCHMARK(BM_pause_during)->ThreadPerCpu();
  55. BENCHMARK(BM_pause_during)->UseRealTime();
  56. BENCHMARK(BM_pause_during)->UseRealTime()->ThreadPerCpu();
  57. void BM_spin_pause_after(benchmark::State& state) {
  58. while(state.KeepRunning()) {
  59. for (int i = 0; i < state.range_x(); ++i) {
  60. benchmark::DoNotOptimize(i);
  61. }
  62. }
  63. for (int i = 0; i < state.range_x(); ++i) {
  64. benchmark::DoNotOptimize(i);
  65. }
  66. }
  67. BASIC_BENCHMARK_TEST(BM_spin_pause_after);
  68. BASIC_BENCHMARK_TEST(BM_spin_pause_after)->ThreadPerCpu();
  69. void BM_spin_pause_before_and_after(benchmark::State& state) {
  70. for (int i = 0; i < state.range_x(); ++i) {
  71. benchmark::DoNotOptimize(i);
  72. }
  73. while(state.KeepRunning()) {
  74. for (int i = 0; i < state.range_x(); ++i) {
  75. benchmark::DoNotOptimize(i);
  76. }
  77. }
  78. for (int i = 0; i < state.range_x(); ++i) {
  79. benchmark::DoNotOptimize(i);
  80. }
  81. }
  82. BASIC_BENCHMARK_TEST(BM_spin_pause_before_and_after);
  83. BASIC_BENCHMARK_TEST(BM_spin_pause_before_and_after)->ThreadPerCpu();
  84. void BM_empty_stop_start(benchmark::State& state) {
  85. while (state.KeepRunning()) { }
  86. }
  87. BENCHMARK(BM_empty_stop_start);
  88. BENCHMARK(BM_empty_stop_start)->ThreadPerCpu();
  89. BENCHMARK_MAIN()