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