123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #undef NDEBUG
- #include <chrono>
- #include <thread>
- #include "../src/timers.h"
- #include "benchmark/benchmark.h"
- #include "output_test.h"
- static const std::chrono::duration<double, std::milli> time_frame(50);
- static const double time_frame_in_sec(
- std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(
- time_frame)
- .count());
- void MyBusySpinwait() {
- const auto start = benchmark::ChronoClockNow();
- while (true) {
- const auto now = benchmark::ChronoClockNow();
- const auto elapsed = now - start;
- if (std::chrono::duration<double, std::chrono::seconds::period>(elapsed) >=
- time_frame)
- return;
- }
- }
- // ========================================================================= //
- // --------------------------- TEST CASES BEGIN ---------------------------- //
- // ========================================================================= //
- // ========================================================================= //
- // BM_MainThread
- void BM_MainThread(benchmark::State& state) {
- for (auto _ : state) {
- MyBusySpinwait();
- state.SetIterationTime(time_frame_in_sec);
- }
- state.counters["invtime"] =
- benchmark::Counter{1, benchmark::Counter::kIsRate};
- }
- BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1);
- BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseRealTime();
- BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseManualTime();
- BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
- BENCHMARK(BM_MainThread)
- ->Iterations(1)
- ->Threads(1)
- ->MeasureProcessCPUTime()
- ->UseRealTime();
- BENCHMARK(BM_MainThread)
- ->Iterations(1)
- ->Threads(1)
- ->MeasureProcessCPUTime()
- ->UseManualTime();
- BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2);
- BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseRealTime();
- BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseManualTime();
- BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
- BENCHMARK(BM_MainThread)
- ->Iterations(1)
- ->Threads(2)
- ->MeasureProcessCPUTime()
- ->UseRealTime();
- BENCHMARK(BM_MainThread)
- ->Iterations(1)
- ->Threads(2)
- ->MeasureProcessCPUTime()
- ->UseManualTime();
- // ========================================================================= //
- // BM_WorkerThread
- void BM_WorkerThread(benchmark::State& state) {
- for (auto _ : state) {
- std::thread Worker(&MyBusySpinwait);
- Worker.join();
- state.SetIterationTime(time_frame_in_sec);
- }
- state.counters["invtime"] =
- benchmark::Counter{1, benchmark::Counter::kIsRate};
- }
- BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1);
- BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseRealTime();
- BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseManualTime();
- BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
- BENCHMARK(BM_WorkerThread)
- ->Iterations(1)
- ->Threads(1)
- ->MeasureProcessCPUTime()
- ->UseRealTime();
- BENCHMARK(BM_WorkerThread)
- ->Iterations(1)
- ->Threads(1)
- ->MeasureProcessCPUTime()
- ->UseManualTime();
- BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2);
- BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseRealTime();
- BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseManualTime();
- BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
- BENCHMARK(BM_WorkerThread)
- ->Iterations(1)
- ->Threads(2)
- ->MeasureProcessCPUTime()
- ->UseRealTime();
- BENCHMARK(BM_WorkerThread)
- ->Iterations(1)
- ->Threads(2)
- ->MeasureProcessCPUTime()
- ->UseManualTime();
- // ========================================================================= //
- // BM_MainThreadAndWorkerThread
- void BM_MainThreadAndWorkerThread(benchmark::State& state) {
- for (auto _ : state) {
- std::thread Worker(&MyBusySpinwait);
- MyBusySpinwait();
- Worker.join();
- state.SetIterationTime(time_frame_in_sec);
- }
- state.counters["invtime"] =
- benchmark::Counter{1, benchmark::Counter::kIsRate};
- }
- BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(1);
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(1)
- ->UseRealTime();
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(1)
- ->UseManualTime();
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(1)
- ->MeasureProcessCPUTime();
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(1)
- ->MeasureProcessCPUTime()
- ->UseRealTime();
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(1)
- ->MeasureProcessCPUTime()
- ->UseManualTime();
- BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(2);
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(2)
- ->UseRealTime();
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(2)
- ->UseManualTime();
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(2)
- ->MeasureProcessCPUTime();
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(2)
- ->MeasureProcessCPUTime()
- ->UseRealTime();
- BENCHMARK(BM_MainThreadAndWorkerThread)
- ->Iterations(1)
- ->Threads(2)
- ->MeasureProcessCPUTime()
- ->UseManualTime();
- // ========================================================================= //
- // ---------------------------- TEST CASES END ----------------------------- //
- // ========================================================================= //
- int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }
|