From 9341d705a14afed7c4e0f8a22b1e6e8a6259fcc4 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Tue, 24 May 2016 00:42:11 -0600 Subject: [PATCH] Change --benchmark_list_tests to respect the benchmark filter. This behavior mirrors how GTest handles these two flags. --- include/benchmark/benchmark_api.h | 9 ++++-- src/benchmark.cc | 47 ++++++++++++------------------- test/CMakeLists.txt | 1 + test/filter_test.cc | 28 +++++++++++++----- 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/include/benchmark/benchmark_api.h b/include/benchmark/benchmark_api.h index 81f7b579..fb698e3b 100644 --- a/include/benchmark/benchmark_api.h +++ b/include/benchmark/benchmark_api.h @@ -160,11 +160,14 @@ class BenchmarkReporter; void Initialize(int* argc, char** argv); -// Run all benchmarks which match the specified --benchmark_filter flag. +// Generate a list of benchmarks matching the specified --benchmark_filter flag +// and if --benchmark_list_tests is specified return after printing the name +// of each matching benchmark. Otherwise run each matching benchmark and +// report the results. +// // The second overload reports the results using the specified 'reporter'. // -// RETURNS: The number of benchmarks run, not including repetitions. If -// '--benchmark_list_tests' is specified '0' is returned. +// RETURNS: The number of matching benchmarks. size_t RunSpecifiedBenchmarks(); size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter); diff --git a/src/benchmark.cc b/src/benchmark.cc index f78f2a36..86109dd5 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -853,24 +853,9 @@ void State::SetLabel(const char* label) { namespace internal { namespace { -void PrintBenchmarkList() { - std::vector benchmarks; - auto families = BenchmarkFamilies::GetInstance(); - if (!families->FindBenchmarks(".", &benchmarks)) return; - - for (const internal::Benchmark::Instance& benchmark : benchmarks) { - std::cout << benchmark.name << "\n"; - } -} - -size_t RunMatchingBenchmarks(const std::string& spec, - BenchmarkReporter* reporter) { +void RunMatchingBenchmarks(const std::vector& benchmarks, + BenchmarkReporter* reporter) { CHECK(reporter != nullptr); - CHECK(!spec.empty()); - - std::vector benchmarks; - auto families = BenchmarkFamilies::GetInstance(); - if (!families->FindBenchmarks(spec, &benchmarks)) return 0; // Determine the width of the name field using a minimum width of 10. size_t name_field_width = 10; @@ -894,7 +879,6 @@ size_t RunMatchingBenchmarks(const std::string& spec, RunBenchmark(benchmark, reporter); } } - return benchmarks.size(); } std::unique_ptr GetDefaultReporter() { @@ -919,22 +903,27 @@ size_t RunSpecifiedBenchmarks() { } size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter) { - if (FLAGS_benchmark_list_tests) { - internal::PrintBenchmarkList(); - return 0; - } std::string spec = FLAGS_benchmark_filter; if (spec.empty() || spec == "all") spec = "."; // Regexp that matches all benchmarks - std::unique_ptr default_reporter; - if (!reporter) { - default_reporter = internal::GetDefaultReporter(); - reporter = default_reporter.get(); + std::vector benchmarks; + auto families = internal::BenchmarkFamilies::GetInstance(); + if (!families->FindBenchmarks(spec, &benchmarks)) return 0; + + if (FLAGS_benchmark_list_tests) { + for (auto const& benchmark : benchmarks) + std::cout << benchmark.name << "\n"; + } else { + std::unique_ptr default_reporter; + if (!reporter) { + default_reporter = internal::GetDefaultReporter(); + reporter = default_reporter.get(); + } + internal::RunMatchingBenchmarks(benchmarks, reporter); + reporter->Finalize(); } - size_t num_run = internal::RunMatchingBenchmarks(spec, reporter); - reporter->Finalize(); - return num_run; + return benchmarks.size(); } namespace internal { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dd62b646..419865dc 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,6 +14,7 @@ add_test(benchmark benchmark_test --benchmark_min_time=0.01) compile_benchmark_test(filter_test) macro(add_filter_test name filter expect) add_test(${name} filter_test --benchmark_min_time=0.01 --benchmark_filter=${filter} ${expect}) + add_test(${name}_list_only filter_test --benchmark_list_tests --benchmark_filter=${filter} ${expect}) endmacro(add_filter_test) add_filter_test(filter_simple "Foo" 3) diff --git a/test/filter_test.cc b/test/filter_test.cc index 3f58242e..0ba40711 100644 --- a/test/filter_test.cc +++ b/test/filter_test.cc @@ -68,7 +68,11 @@ BENCHMARK(BM_FooBa); -int main(int argc, char* argv[]) { +int main(int argc, char** argv) { + bool list_only = false; + for (int i=0; i < argc; ++i) + list_only |= std::string(argv[i]).find("--benchmark_list_tests") != std::string::npos; + benchmark::Initialize(&argc, argv); TestReporter test_reporter; @@ -77,15 +81,25 @@ int main(int argc, char* argv[]) { if (argc == 2) { // Make sure we ran all of the tests std::stringstream ss(argv[1]); - size_t expected; - ss >> expected; + size_t expected_return; + ss >> expected_return; - const size_t count = test_reporter.GetCount(); - if (count != expected || returned_count != expected) { - std::cerr << "ERROR: Expected " << expected << " tests to be run but returned_count = " - << returned_count << " and reporter_count = " << count << std::endl; + if (returned_count != expected_return) { + std::cerr << "ERROR: Expected " << expected_return + << " tests to match the filter but returned_count = " + << returned_count << std::endl; + return -1; + } + + const size_t expected_reports = list_only ? 0 : expected_return; + const size_t reports_count = test_reporter.GetCount(); + if (reports_count != expected_reports) { + std::cerr << "ERROR: Expected " << expected_reports + << " tests to be run but reported_count = " << reports_count + << std::endl; return -1; } } + return 0; }