Skip to content

Commit

Permalink
Verify results in internal_threading_test
Browse files Browse the repository at this point in the history
Measurement is very noisy in CI VMs. Require one extra core, because
even with with huge ±30% margin test fails due to background activity.

Signed-off-by: Konstantin Khlebnikov <[email protected]>
  • Loading branch information
koct9i committed Mar 29, 2020
1 parent c521091 commit c39ee1c
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions test/internal_threading_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
#include <chrono>
#include <thread>
#include "../src/timers.h"
#include "../src/check.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_ns(
std::chrono::duration_cast<std::chrono::duration<double, std::nano>>(
time_frame)
.count());
static const double time_frame_in_sec(
std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(
time_frame)
Expand All @@ -26,6 +30,55 @@ void MyBusySpinwait() {
}
}

class TestReporter : public benchmark::ConsoleReporter {
int num_cpus = 0;
public:
virtual bool ReportContext(const Context& context) {
num_cpus = context.cpu_info.num_cpus;
return ConsoleReporter::ReportContext(context);
};

virtual void ReportRuns(const std::vector<Run>& report) {
ConsoleReporter::ReportRuns(report);

for (auto &run: report) {
double expected_cpus = 1;
int min_cpus = run.threads;

if (run.run_name.function_name == "BM_WorkerThread") {
if (run.run_name.time_type == "" ||
run.run_name.time_type == "real_time" ||
run.run_name.time_type == "manual_time") {
expected_cpus = 0;
}
}

if (run.run_name.function_name == "BM_MainThreadAndWorkerThread") {
min_cpus *= 2;
if (run.run_name.time_type == "process_time" ||
run.run_name.time_type == "process_time/real_time" ||
run.run_name.time_type == "process_time/manual_time") {
expected_cpus = 2;
}
}

if (num_cpus <= min_cpus) {
VLOG(0) << "Not enough cpus to get valid time\n";
continue;
}

// +/- 30%
CHECK_FLOAT_EQ(run.GetAdjustedRealTime() / time_frame_in_ns, 1.0, 0.3);

// -30% .. +70%
CHECK_FLOAT_EQ(run.GetAdjustedCPUTime() / time_frame_in_ns, expected_cpus + 0.2, 0.5);
}
}

TestReporter() {}
virtual ~TestReporter() {}
};

// ========================================================================= //
// --------------------------- TEST CASES BEGIN ---------------------------- //
// ========================================================================= //
Expand Down Expand Up @@ -181,4 +234,9 @@ BENCHMARK(BM_MainThreadAndWorkerThread)
// ---------------------------- TEST CASES END ----------------------------- //
// ========================================================================= //

int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }
int main(int argc, char* argv[]) {
benchmark::Initialize(&argc, argv);
TestReporter test_reporter;
benchmark::RunSpecifiedBenchmarks(&test_reporter);
return 0;
}

0 comments on commit c39ee1c

Please sign in to comment.