mirror of
https://github.com/google/benchmark.git
synced 2025-01-14 13:50:13 +08:00
Change --benchmark_list_tests to respect the benchmark filter.
This behavior mirrors how GTest handles these two flags.
This commit is contained in:
parent
e0de8171c6
commit
9341d705a1
@ -160,11 +160,14 @@ class BenchmarkReporter;
|
|||||||
|
|
||||||
void Initialize(int* argc, char** argv);
|
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'.
|
// The second overload reports the results using the specified 'reporter'.
|
||||||
//
|
//
|
||||||
// RETURNS: The number of benchmarks run, not including repetitions. If
|
// RETURNS: The number of matching benchmarks.
|
||||||
// '--benchmark_list_tests' is specified '0' is returned.
|
|
||||||
size_t RunSpecifiedBenchmarks();
|
size_t RunSpecifiedBenchmarks();
|
||||||
size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter);
|
size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter);
|
||||||
|
|
||||||
|
@ -853,24 +853,9 @@ void State::SetLabel(const char* label) {
|
|||||||
namespace internal {
|
namespace internal {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void PrintBenchmarkList() {
|
void RunMatchingBenchmarks(const std::vector<Benchmark::Instance>& benchmarks,
|
||||||
std::vector<Benchmark::Instance> benchmarks;
|
BenchmarkReporter* reporter) {
|
||||||
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) {
|
|
||||||
CHECK(reporter != nullptr);
|
CHECK(reporter != nullptr);
|
||||||
CHECK(!spec.empty());
|
|
||||||
|
|
||||||
std::vector<Benchmark::Instance> 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.
|
// Determine the width of the name field using a minimum width of 10.
|
||||||
size_t name_field_width = 10;
|
size_t name_field_width = 10;
|
||||||
@ -894,7 +879,6 @@ size_t RunMatchingBenchmarks(const std::string& spec,
|
|||||||
RunBenchmark(benchmark, reporter);
|
RunBenchmark(benchmark, reporter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return benchmarks.size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<BenchmarkReporter> GetDefaultReporter() {
|
std::unique_ptr<BenchmarkReporter> GetDefaultReporter() {
|
||||||
@ -919,22 +903,27 @@ size_t RunSpecifiedBenchmarks() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
|
size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
|
||||||
if (FLAGS_benchmark_list_tests) {
|
|
||||||
internal::PrintBenchmarkList();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
std::string spec = FLAGS_benchmark_filter;
|
std::string spec = FLAGS_benchmark_filter;
|
||||||
if (spec.empty() || spec == "all")
|
if (spec.empty() || spec == "all")
|
||||||
spec = "."; // Regexp that matches all benchmarks
|
spec = "."; // Regexp that matches all benchmarks
|
||||||
|
|
||||||
std::unique_ptr<BenchmarkReporter> default_reporter;
|
std::vector<internal::Benchmark::Instance> benchmarks;
|
||||||
if (!reporter) {
|
auto families = internal::BenchmarkFamilies::GetInstance();
|
||||||
default_reporter = internal::GetDefaultReporter();
|
if (!families->FindBenchmarks(spec, &benchmarks)) return 0;
|
||||||
reporter = default_reporter.get();
|
|
||||||
|
if (FLAGS_benchmark_list_tests) {
|
||||||
|
for (auto const& benchmark : benchmarks)
|
||||||
|
std::cout << benchmark.name << "\n";
|
||||||
|
} else {
|
||||||
|
std::unique_ptr<BenchmarkReporter> 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);
|
return benchmarks.size();
|
||||||
reporter->Finalize();
|
|
||||||
return num_run;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
@ -14,6 +14,7 @@ add_test(benchmark benchmark_test --benchmark_min_time=0.01)
|
|||||||
compile_benchmark_test(filter_test)
|
compile_benchmark_test(filter_test)
|
||||||
macro(add_filter_test name filter expect)
|
macro(add_filter_test name filter expect)
|
||||||
add_test(${name} filter_test --benchmark_min_time=0.01 --benchmark_filter=${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)
|
endmacro(add_filter_test)
|
||||||
|
|
||||||
add_filter_test(filter_simple "Foo" 3)
|
add_filter_test(filter_simple "Foo" 3)
|
||||||
|
@ -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);
|
benchmark::Initialize(&argc, argv);
|
||||||
|
|
||||||
TestReporter test_reporter;
|
TestReporter test_reporter;
|
||||||
@ -77,15 +81,25 @@ int main(int argc, char* argv[]) {
|
|||||||
if (argc == 2) {
|
if (argc == 2) {
|
||||||
// Make sure we ran all of the tests
|
// Make sure we ran all of the tests
|
||||||
std::stringstream ss(argv[1]);
|
std::stringstream ss(argv[1]);
|
||||||
size_t expected;
|
size_t expected_return;
|
||||||
ss >> expected;
|
ss >> expected_return;
|
||||||
|
|
||||||
const size_t count = test_reporter.GetCount();
|
if (returned_count != expected_return) {
|
||||||
if (count != expected || returned_count != expected) {
|
std::cerr << "ERROR: Expected " << expected_return
|
||||||
std::cerr << "ERROR: Expected " << expected << " tests to be run but returned_count = "
|
<< " tests to match the filter but returned_count = "
|
||||||
<< returned_count << " and reporter_count = " << count << std::endl;
|
<< 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 -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user