From 32a1e3972014c27b683945f45a2b4a37ab1040c1 Mon Sep 17 00:00:00 2001 From: Eric Backus <52862004+EricBackus@users.noreply.github.com> Date: Sat, 27 Jul 2019 09:02:31 -0700 Subject: [PATCH] Bugfix/wsl selftest fixes. Fixes #839 (#843) * Update AUTHORS and CONTRIBUTORS * Fix WSL self-test failures Some of the benchmark self-tests expect and check for a particular output format from the benchmark library. The numerical values must not be infinity or not-a-number, or the test will report an error. Some of the values are computed bytes-per-second or items-per-second values, so these require that the measured CPU time for the test to be non-zero. But the loop that is being measured was empty, so the measured CPU time for the loop was extremely small. On systems like Windows Subsystem for Linux (WSL) the timer doesn't have enough resolution to measure this, so the measured CPU time was zero. This fix just makes sure that these tests have something within the timing loop, so that the benchmark library will not decide that the loop takes zero CPU time. This makes these tests more robust, and in particular makes them pass on WSL. --- AUTHORS | 1 + CONTRIBUTORS | 1 + test/complexity_test.cc | 2 ++ test/reporter_output_test.cc | 6 ++++++ test/user_counters_tabular_test.cc | 2 ++ test/user_counters_test.cc | 10 ++++++++++ 6 files changed, 22 insertions(+) diff --git a/AUTHORS b/AUTHORS index 912cbbc1..15418631 100644 --- a/AUTHORS +++ b/AUTHORS @@ -19,6 +19,7 @@ David Coeurjolly Deniz Evrenci Dirac Research Dominik Czarnota +Eric Backus Eric Fiselier Eugene Zhuk Evgeny Safronov diff --git a/CONTRIBUTORS b/CONTRIBUTORS index b680efc8..167165f4 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -35,6 +35,7 @@ David Coeurjolly Deniz Evrenci Dominic Hamon Dominik Czarnota +Eric Backus Eric Fiselier Eugene Zhuk Evgeny Safronov diff --git a/test/complexity_test.cc b/test/complexity_test.cc index d4febbbc..5681fdcf 100644 --- a/test/complexity_test.cc +++ b/test/complexity_test.cc @@ -190,6 +190,8 @@ ADD_COMPLEXITY_CASES(n_lg_n_test_name, big_o_n_lg_n_test_name, void BM_ComplexityCaptureArgs(benchmark::State& state, int n) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } state.SetComplexityN(n); } diff --git a/test/reporter_output_test.cc b/test/reporter_output_test.cc index c8090d4a..8486d590 100644 --- a/test/reporter_output_test.cc +++ b/test/reporter_output_test.cc @@ -90,6 +90,8 @@ ADD_CASES(TC_CSVOut, {{"^\"BM_basic\",%csv_report$"}}); void BM_bytes_per_second(benchmark::State& state) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } state.SetBytesProcessed(1); } @@ -117,6 +119,8 @@ ADD_CASES(TC_CSVOut, {{"^\"BM_bytes_per_second\",%csv_bytes_report$"}}); void BM_items_per_second(benchmark::State& state) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } state.SetItemsProcessed(1); } @@ -262,6 +266,8 @@ ADD_CASES(TC_ConsoleOut, {{"^BM_BigArgs/1073741824 %console_report$"}, void BM_Complexity_O1(benchmark::State& state) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } state.SetComplexityN(state.range(0)); } diff --git a/test/user_counters_tabular_test.cc b/test/user_counters_tabular_test.cc index 099464ef..18373c0a 100644 --- a/test/user_counters_tabular_test.cc +++ b/test/user_counters_tabular_test.cc @@ -107,6 +107,8 @@ CHECK_BENCHMARK_RESULTS("BM_Counters_Tabular/threads:%int", &CheckTabular); void BM_CounterRates_Tabular(benchmark::State& state) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } namespace bm = benchmark; state.counters.insert({ diff --git a/test/user_counters_test.cc b/test/user_counters_test.cc index 0775bc01..4d308dfb 100644 --- a/test/user_counters_test.cc +++ b/test/user_counters_test.cc @@ -64,6 +64,8 @@ int num_calls1 = 0; } void BM_Counters_WithBytesAndItemsPSec(benchmark::State& state) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } state.counters["foo"] = 1; state.counters["bar"] = ++num_calls1; @@ -111,6 +113,8 @@ CHECK_BENCHMARK_RESULTS("BM_Counters_WithBytesAndItemsPSec", void BM_Counters_Rate(benchmark::State& state) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } namespace bm = benchmark; state.counters["foo"] = bm::Counter{1, bm::Counter::kIsRate}; @@ -228,6 +232,8 @@ CHECK_BENCHMARK_RESULTS("BM_Counters_AvgThreads/threads:%int", void BM_Counters_AvgThreadsRate(benchmark::State& state) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } namespace bm = benchmark; state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgThreadsRate}; @@ -309,6 +315,8 @@ CHECK_BENCHMARK_RESULTS("BM_Counters_IterationInvariant", void BM_Counters_kIsIterationInvariantRate(benchmark::State& state) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } namespace bm = benchmark; state.counters["foo"] = @@ -394,6 +402,8 @@ CHECK_BENCHMARK_RESULTS("BM_Counters_AvgIterations", &CheckAvgIterations); void BM_Counters_kAvgIterationsRate(benchmark::State& state) { for (auto _ : state) { + // This test requires a non-zero CPU time to avoid divide-by-zero + benchmark::DoNotOptimize(state.iterations()); } namespace bm = benchmark; state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgIterationsRate};