Skip to content

Commit d97af0a

Browse files
committed
Verify results in internal_threading_test
Run 10 iterations os 50ms busyloop and check that measured time. Signed-off-by: Konstantin Khlebnikov <[email protected]>
1 parent 9dd524c commit d97af0a

File tree

1 file changed

+144
-38
lines changed

1 file changed

+144
-38
lines changed

test/internal_threading_test.cc

Lines changed: 144 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
#include <chrono>
55
#include <thread>
66
#include "../src/timers.h"
7+
#include "../src/check.h"
78
#include "benchmark/benchmark.h"
8-
#include "output_test.h"
99

1010
static const std::chrono::duration<double, std::milli> time_frame(50);
11+
static const double time_frame_in_ns(
12+
std::chrono::duration_cast<std::chrono::duration<double, std::nano>>(
13+
time_frame)
14+
.count());
1115
static const double time_frame_in_sec(
1216
std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(
1317
time_frame)
1418
.count());
1519

20+
static const int nr_iterations = 10;
21+
1622
void MyBusySpinwait() {
1723
const auto start = benchmark::ChronoClockNow();
1824

@@ -26,6 +32,53 @@ void MyBusySpinwait() {
2632
}
2733
}
2834

35+
class TestReporter : public benchmark::ConsoleReporter {
36+
int num_cpus = 0;
37+
public:
38+
virtual bool ReportContext(const Context& context) {
39+
num_cpus = context.cpu_info.num_cpus;
40+
return ConsoleReporter::ReportContext(context);
41+
};
42+
43+
virtual void ReportRuns(const std::vector<Run>& report) {
44+
ConsoleReporter::ReportRuns(report);
45+
46+
for (auto &run: report) {
47+
double expected_cpus = 1;
48+
int64_t min_cpus = run.threads;
49+
50+
if (run.run_name.function_name == "BM_WorkerThread") {
51+
if (run.run_name.time_type == "" ||
52+
run.run_name.time_type == "real_time" ||
53+
run.run_name.time_type == "manual_time") {
54+
expected_cpus = 0;
55+
}
56+
}
57+
58+
if (run.run_name.function_name == "BM_MainThreadAndWorkerThread") {
59+
min_cpus *= 2;
60+
if (run.run_name.time_type == "process_time" ||
61+
run.run_name.time_type == "process_time/real_time" ||
62+
run.run_name.time_type == "process_time/manual_time") {
63+
expected_cpus = 2;
64+
}
65+
}
66+
67+
if (num_cpus < min_cpus) {
68+
VLOG(0) << "Not enough cpus to get valid time\n";
69+
continue;
70+
}
71+
72+
// -/+ 20%
73+
CHECK_FLOAT_EQ(run.GetAdjustedRealTime() / time_frame_in_ns, 1.0, 0.2);
74+
CHECK_FLOAT_EQ(run.GetAdjustedCPUTime() / time_frame_in_ns, expected_cpus, 0.2);
75+
}
76+
}
77+
78+
TestReporter() {}
79+
virtual ~TestReporter() {}
80+
};
81+
2982
// ========================================================================= //
3083
// --------------------------- TEST CASES BEGIN ---------------------------- //
3184
// ========================================================================= //
@@ -42,32 +95,54 @@ void BM_MainThread(benchmark::State& state) {
4295
benchmark::Counter{1, benchmark::Counter::kIsRate};
4396
}
4497

45-
BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1);
46-
BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseRealTime();
47-
BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseManualTime();
48-
BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
4998
BENCHMARK(BM_MainThread)
50-
->Iterations(1)
99+
->Iterations(nr_iterations)
100+
->Threads(1);
101+
BENCHMARK(BM_MainThread)
102+
->Iterations(nr_iterations)
103+
->Threads(1)
104+
->UseRealTime();
105+
BENCHMARK(BM_MainThread)
106+
->Iterations(nr_iterations)
107+
->Threads(1)
108+
->UseManualTime();
109+
BENCHMARK(BM_MainThread)
110+
->Iterations(nr_iterations)
111+
->Threads(1)
112+
->MeasureProcessCPUTime();
113+
BENCHMARK(BM_MainThread)
114+
->Iterations(nr_iterations)
51115
->Threads(1)
52116
->MeasureProcessCPUTime()
53117
->UseRealTime();
54118
BENCHMARK(BM_MainThread)
55-
->Iterations(1)
119+
->Iterations(nr_iterations)
56120
->Threads(1)
57121
->MeasureProcessCPUTime()
58122
->UseManualTime();
59123

60-
BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2);
61-
BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseRealTime();
62-
BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseManualTime();
63-
BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
64124
BENCHMARK(BM_MainThread)
65-
->Iterations(1)
125+
->Iterations(nr_iterations)
126+
->Threads(2);
127+
BENCHMARK(BM_MainThread)
128+
->Iterations(nr_iterations)
129+
->Threads(2)
130+
->UseRealTime();
131+
BENCHMARK(BM_MainThread)
132+
->Iterations(nr_iterations)
133+
->Threads(2)
134+
->UseManualTime();
135+
BENCHMARK(BM_MainThread)
136+
->Iterations(nr_iterations)
137+
->Threads(2)
138+
->MeasureProcessCPUTime();
139+
BENCHMARK(BM_MainThread)
140+
->Iterations(nr_iterations)
66141
->Threads(2)
67142
->MeasureProcessCPUTime()
68143
->UseRealTime();
69144
BENCHMARK(BM_MainThread)
70-
->Iterations(1)
145+
->Iterations(nr_iterations)
71146
->Threads(2)
72147
->MeasureProcessCPUTime()
73148
->UseManualTime();
@@ -85,32 +160,54 @@ void BM_WorkerThread(benchmark::State& state) {
85160
benchmark::Counter{1, benchmark::Counter::kIsRate};
86161
}
87162

88-
BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1);
89-
BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseRealTime();
90-
BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseManualTime();
91-
BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
92163
BENCHMARK(BM_WorkerThread)
93-
->Iterations(1)
164+
->Iterations(nr_iterations)
165+
->Threads(1);
166+
BENCHMARK(BM_WorkerThread)
167+
->Iterations(nr_iterations)
168+
->Threads(1)
169+
->UseRealTime();
170+
BENCHMARK(BM_WorkerThread)
171+
->Iterations(nr_iterations)
172+
->Threads(1)
173+
->UseManualTime();
174+
BENCHMARK(BM_WorkerThread)
175+
->Iterations(nr_iterations)
176+
->Threads(1)
177+
->MeasureProcessCPUTime();
178+
BENCHMARK(BM_WorkerThread)
179+
->Iterations(nr_iterations)
94180
->Threads(1)
95181
->MeasureProcessCPUTime()
96182
->UseRealTime();
97183
BENCHMARK(BM_WorkerThread)
98-
->Iterations(1)
184+
->Iterations(nr_iterations)
99185
->Threads(1)
100186
->MeasureProcessCPUTime()
101187
->UseManualTime();
102188

103-
BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2);
104-
BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseRealTime();
105-
BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseManualTime();
106-
BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
107189
BENCHMARK(BM_WorkerThread)
108-
->Iterations(1)
190+
->Iterations(nr_iterations)
191+
->Threads(2);
192+
BENCHMARK(BM_WorkerThread)
193+
->Iterations(nr_iterations)
194+
->Threads(2)
195+
->UseRealTime();
196+
BENCHMARK(BM_WorkerThread)
197+
->Iterations(nr_iterations)
198+
->Threads(2)
199+
->UseManualTime();
200+
BENCHMARK(BM_WorkerThread)
201+
->Iterations(nr_iterations)
202+
->Threads(2)
203+
->MeasureProcessCPUTime();
204+
BENCHMARK(BM_WorkerThread)
205+
->Iterations(nr_iterations)
109206
->Threads(2)
110207
->MeasureProcessCPUTime()
111208
->UseRealTime();
112209
BENCHMARK(BM_WorkerThread)
113-
->Iterations(1)
210+
->Iterations(nr_iterations)
114211
->Threads(2)
115212
->MeasureProcessCPUTime()
116213
->UseManualTime();
@@ -129,50 +226,54 @@ void BM_MainThreadAndWorkerThread(benchmark::State& state) {
129226
benchmark::Counter{1, benchmark::Counter::kIsRate};
130227
}
131228

132-
BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(1);
133229
BENCHMARK(BM_MainThreadAndWorkerThread)
134-
->Iterations(1)
230+
->Iterations(nr_iterations)
231+
->Threads(1);
232+
BENCHMARK(BM_MainThreadAndWorkerThread)
233+
->Iterations(nr_iterations)
135234
->Threads(1)
136235
->UseRealTime();
137236
BENCHMARK(BM_MainThreadAndWorkerThread)
138-
->Iterations(1)
237+
->Iterations(nr_iterations)
139238
->Threads(1)
140239
->UseManualTime();
141240
BENCHMARK(BM_MainThreadAndWorkerThread)
142-
->Iterations(1)
241+
->Iterations(nr_iterations)
143242
->Threads(1)
144243
->MeasureProcessCPUTime();
145244
BENCHMARK(BM_MainThreadAndWorkerThread)
146-
->Iterations(1)
245+
->Iterations(nr_iterations)
147246
->Threads(1)
148247
->MeasureProcessCPUTime()
149248
->UseRealTime();
150249
BENCHMARK(BM_MainThreadAndWorkerThread)
151-
->Iterations(1)
250+
->Iterations(nr_iterations)
152251
->Threads(1)
153252
->MeasureProcessCPUTime()
154253
->UseManualTime();
155254

156-
BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(2);
157255
BENCHMARK(BM_MainThreadAndWorkerThread)
158-
->Iterations(1)
256+
->Iterations(nr_iterations)
257+
->Threads(2);
258+
BENCHMARK(BM_MainThreadAndWorkerThread)
259+
->Iterations(nr_iterations)
159260
->Threads(2)
160261
->UseRealTime();
161262
BENCHMARK(BM_MainThreadAndWorkerThread)
162-
->Iterations(1)
263+
->Iterations(nr_iterations)
163264
->Threads(2)
164265
->UseManualTime();
165266
BENCHMARK(BM_MainThreadAndWorkerThread)
166-
->Iterations(1)
267+
->Iterations(nr_iterations)
167268
->Threads(2)
168269
->MeasureProcessCPUTime();
169270
BENCHMARK(BM_MainThreadAndWorkerThread)
170-
->Iterations(1)
271+
->Iterations(nr_iterations)
171272
->Threads(2)
172273
->MeasureProcessCPUTime()
173274
->UseRealTime();
174275
BENCHMARK(BM_MainThreadAndWorkerThread)
175-
->Iterations(1)
276+
->Iterations(nr_iterations)
176277
->Threads(2)
177278
->MeasureProcessCPUTime()
178279
->UseManualTime();
@@ -181,4 +282,9 @@ BENCHMARK(BM_MainThreadAndWorkerThread)
181282
// ---------------------------- TEST CASES END ----------------------------- //
182283
// ========================================================================= //
183284

184-
int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }
285+
int main(int argc, char* argv[]) {
286+
benchmark::Initialize(&argc, argv);
287+
TestReporter test_reporter;
288+
benchmark::RunSpecifiedBenchmarks(&test_reporter);
289+
return 0;
290+
}

0 commit comments

Comments
 (0)