internal_threading_test.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #undef NDEBUG
  2. #include <chrono>
  3. #include <thread>
  4. #include "../src/timers.h"
  5. #include "benchmark/benchmark.h"
  6. #include "output_test.h"
  7. static const std::chrono::duration<double, std::milli> time_frame(50);
  8. static const double time_frame_in_sec(
  9. std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(
  10. time_frame)
  11. .count());
  12. void MyBusySpinwait() {
  13. const auto start = benchmark::ChronoClockNow();
  14. while (true) {
  15. const auto now = benchmark::ChronoClockNow();
  16. const auto elapsed = now - start;
  17. if (std::chrono::duration<double, std::chrono::seconds::period>(elapsed) >=
  18. time_frame)
  19. return;
  20. }
  21. }
  22. // ========================================================================= //
  23. // --------------------------- TEST CASES BEGIN ---------------------------- //
  24. // ========================================================================= //
  25. // ========================================================================= //
  26. // BM_MainThread
  27. void BM_MainThread(benchmark::State& state) {
  28. for (auto _ : state) {
  29. MyBusySpinwait();
  30. state.SetIterationTime(time_frame_in_sec);
  31. }
  32. state.counters["invtime"] =
  33. benchmark::Counter{1, benchmark::Counter::kIsRate};
  34. }
  35. BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1);
  36. BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseRealTime();
  37. BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseManualTime();
  38. BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
  39. BENCHMARK(BM_MainThread)
  40. ->Iterations(1)
  41. ->Threads(1)
  42. ->MeasureProcessCPUTime()
  43. ->UseRealTime();
  44. BENCHMARK(BM_MainThread)
  45. ->Iterations(1)
  46. ->Threads(1)
  47. ->MeasureProcessCPUTime()
  48. ->UseManualTime();
  49. BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2);
  50. BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseRealTime();
  51. BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseManualTime();
  52. BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
  53. BENCHMARK(BM_MainThread)
  54. ->Iterations(1)
  55. ->Threads(2)
  56. ->MeasureProcessCPUTime()
  57. ->UseRealTime();
  58. BENCHMARK(BM_MainThread)
  59. ->Iterations(1)
  60. ->Threads(2)
  61. ->MeasureProcessCPUTime()
  62. ->UseManualTime();
  63. // ========================================================================= //
  64. // BM_WorkerThread
  65. void BM_WorkerThread(benchmark::State& state) {
  66. for (auto _ : state) {
  67. std::thread Worker(&MyBusySpinwait);
  68. Worker.join();
  69. state.SetIterationTime(time_frame_in_sec);
  70. }
  71. state.counters["invtime"] =
  72. benchmark::Counter{1, benchmark::Counter::kIsRate};
  73. }
  74. BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1);
  75. BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseRealTime();
  76. BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseManualTime();
  77. BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
  78. BENCHMARK(BM_WorkerThread)
  79. ->Iterations(1)
  80. ->Threads(1)
  81. ->MeasureProcessCPUTime()
  82. ->UseRealTime();
  83. BENCHMARK(BM_WorkerThread)
  84. ->Iterations(1)
  85. ->Threads(1)
  86. ->MeasureProcessCPUTime()
  87. ->UseManualTime();
  88. BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2);
  89. BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseRealTime();
  90. BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseManualTime();
  91. BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
  92. BENCHMARK(BM_WorkerThread)
  93. ->Iterations(1)
  94. ->Threads(2)
  95. ->MeasureProcessCPUTime()
  96. ->UseRealTime();
  97. BENCHMARK(BM_WorkerThread)
  98. ->Iterations(1)
  99. ->Threads(2)
  100. ->MeasureProcessCPUTime()
  101. ->UseManualTime();
  102. // ========================================================================= //
  103. // BM_MainThreadAndWorkerThread
  104. void BM_MainThreadAndWorkerThread(benchmark::State& state) {
  105. for (auto _ : state) {
  106. std::thread Worker(&MyBusySpinwait);
  107. MyBusySpinwait();
  108. Worker.join();
  109. state.SetIterationTime(time_frame_in_sec);
  110. }
  111. state.counters["invtime"] =
  112. benchmark::Counter{1, benchmark::Counter::kIsRate};
  113. }
  114. BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(1);
  115. BENCHMARK(BM_MainThreadAndWorkerThread)
  116. ->Iterations(1)
  117. ->Threads(1)
  118. ->UseRealTime();
  119. BENCHMARK(BM_MainThreadAndWorkerThread)
  120. ->Iterations(1)
  121. ->Threads(1)
  122. ->UseManualTime();
  123. BENCHMARK(BM_MainThreadAndWorkerThread)
  124. ->Iterations(1)
  125. ->Threads(1)
  126. ->MeasureProcessCPUTime();
  127. BENCHMARK(BM_MainThreadAndWorkerThread)
  128. ->Iterations(1)
  129. ->Threads(1)
  130. ->MeasureProcessCPUTime()
  131. ->UseRealTime();
  132. BENCHMARK(BM_MainThreadAndWorkerThread)
  133. ->Iterations(1)
  134. ->Threads(1)
  135. ->MeasureProcessCPUTime()
  136. ->UseManualTime();
  137. BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(2);
  138. BENCHMARK(BM_MainThreadAndWorkerThread)
  139. ->Iterations(1)
  140. ->Threads(2)
  141. ->UseRealTime();
  142. BENCHMARK(BM_MainThreadAndWorkerThread)
  143. ->Iterations(1)
  144. ->Threads(2)
  145. ->UseManualTime();
  146. BENCHMARK(BM_MainThreadAndWorkerThread)
  147. ->Iterations(1)
  148. ->Threads(2)
  149. ->MeasureProcessCPUTime();
  150. BENCHMARK(BM_MainThreadAndWorkerThread)
  151. ->Iterations(1)
  152. ->Threads(2)
  153. ->MeasureProcessCPUTime()
  154. ->UseRealTime();
  155. BENCHMARK(BM_MainThreadAndWorkerThread)
  156. ->Iterations(1)
  157. ->Threads(2)
  158. ->MeasureProcessCPUTime()
  159. ->UseManualTime();
  160. // ========================================================================= //
  161. // ---------------------------- TEST CASES END ----------------------------- //
  162. // ========================================================================= //
  163. int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }