7 #include "../src/timers.h"
8 #include "benchmark/benchmark.h"
9 #include "output_test.h"
11 static const std::chrono::duration
<double, std::milli
> time_frame(50);
12 static const double time_frame_in_sec(
13 std::chrono::duration_cast
<std::chrono::duration
<double, std::ratio
<1, 1>>>(
17 void MyBusySpinwait() {
18 const auto start
= benchmark::ChronoClockNow();
21 const auto now
= benchmark::ChronoClockNow();
22 const auto elapsed
= now
- start
;
24 if (std::chrono::duration
<double, std::chrono::seconds::period
>(elapsed
) >=
30 // ========================================================================= //
31 // --------------------------- TEST CASES BEGIN ---------------------------- //
32 // ========================================================================= //
34 // ========================================================================= //
37 void BM_MainThread(benchmark::State
& state
) {
38 for (auto _
: state
) {
40 state
.SetIterationTime(time_frame_in_sec
);
42 state
.counters
["invtime"] =
43 benchmark::Counter
{1, benchmark::Counter::kIsRate
};
46 BENCHMARK(BM_MainThread
)->Iterations(1)->Threads(1);
47 BENCHMARK(BM_MainThread
)->Iterations(1)->Threads(1)->UseRealTime();
48 BENCHMARK(BM_MainThread
)->Iterations(1)->Threads(1)->UseManualTime();
49 BENCHMARK(BM_MainThread
)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
50 BENCHMARK(BM_MainThread
)
53 ->MeasureProcessCPUTime()
55 BENCHMARK(BM_MainThread
)
58 ->MeasureProcessCPUTime()
61 BENCHMARK(BM_MainThread
)->Iterations(1)->Threads(2);
62 BENCHMARK(BM_MainThread
)->Iterations(1)->Threads(2)->UseRealTime();
63 BENCHMARK(BM_MainThread
)->Iterations(1)->Threads(2)->UseManualTime();
64 BENCHMARK(BM_MainThread
)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
65 BENCHMARK(BM_MainThread
)
68 ->MeasureProcessCPUTime()
70 BENCHMARK(BM_MainThread
)
73 ->MeasureProcessCPUTime()
76 // ========================================================================= //
79 void BM_WorkerThread(benchmark::State
& state
) {
80 for (auto _
: state
) {
81 std::thread
Worker(&MyBusySpinwait
);
83 state
.SetIterationTime(time_frame_in_sec
);
85 state
.counters
["invtime"] =
86 benchmark::Counter
{1, benchmark::Counter::kIsRate
};
89 BENCHMARK(BM_WorkerThread
)->Iterations(1)->Threads(1);
90 BENCHMARK(BM_WorkerThread
)->Iterations(1)->Threads(1)->UseRealTime();
91 BENCHMARK(BM_WorkerThread
)->Iterations(1)->Threads(1)->UseManualTime();
92 BENCHMARK(BM_WorkerThread
)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
93 BENCHMARK(BM_WorkerThread
)
96 ->MeasureProcessCPUTime()
98 BENCHMARK(BM_WorkerThread
)
101 ->MeasureProcessCPUTime()
104 BENCHMARK(BM_WorkerThread
)->Iterations(1)->Threads(2);
105 BENCHMARK(BM_WorkerThread
)->Iterations(1)->Threads(2)->UseRealTime();
106 BENCHMARK(BM_WorkerThread
)->Iterations(1)->Threads(2)->UseManualTime();
107 BENCHMARK(BM_WorkerThread
)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
108 BENCHMARK(BM_WorkerThread
)
111 ->MeasureProcessCPUTime()
113 BENCHMARK(BM_WorkerThread
)
116 ->MeasureProcessCPUTime()
119 // ========================================================================= //
120 // BM_MainThreadAndWorkerThread
122 void BM_MainThreadAndWorkerThread(benchmark::State
& state
) {
123 for (auto _
: state
) {
124 std::thread
Worker(&MyBusySpinwait
);
127 state
.SetIterationTime(time_frame_in_sec
);
129 state
.counters
["invtime"] =
130 benchmark::Counter
{1, benchmark::Counter::kIsRate
};
133 BENCHMARK(BM_MainThreadAndWorkerThread
)->Iterations(1)->Threads(1);
134 BENCHMARK(BM_MainThreadAndWorkerThread
)
138 BENCHMARK(BM_MainThreadAndWorkerThread
)
142 BENCHMARK(BM_MainThreadAndWorkerThread
)
145 ->MeasureProcessCPUTime();
146 BENCHMARK(BM_MainThreadAndWorkerThread
)
149 ->MeasureProcessCPUTime()
151 BENCHMARK(BM_MainThreadAndWorkerThread
)
154 ->MeasureProcessCPUTime()
157 BENCHMARK(BM_MainThreadAndWorkerThread
)->Iterations(1)->Threads(2);
158 BENCHMARK(BM_MainThreadAndWorkerThread
)
162 BENCHMARK(BM_MainThreadAndWorkerThread
)
166 BENCHMARK(BM_MainThreadAndWorkerThread
)
169 ->MeasureProcessCPUTime();
170 BENCHMARK(BM_MainThreadAndWorkerThread
)
173 ->MeasureProcessCPUTime()
175 BENCHMARK(BM_MainThreadAndWorkerThread
)
178 ->MeasureProcessCPUTime()
181 // ========================================================================= //
182 // ---------------------------- TEST CASES END ----------------------------- //
183 // ========================================================================= //
185 int main(int argc
, char* argv
[]) { RunOutputTests(argc
, argv
); }