Change RunSpecifiedBenchmarks to return the number of benchmarks run. Fixes #145

This commit is contained in:
Eric Fiselier 2016-05-23 23:32:05 -06:00
parent 9fcdd6fc25
commit e0de8171c6
3 changed files with 23 additions and 17 deletions

View File

@ -160,10 +160,14 @@ class BenchmarkReporter;
void Initialize(int* argc, char** argv);
// Otherwise, run all benchmarks specified by the --benchmark_filter flag,
// and exit after running the benchmarks.
void RunSpecifiedBenchmarks();
void RunSpecifiedBenchmarks(BenchmarkReporter* reporter);
// Run all benchmarks which match the specified --benchmark_filter flag.
// 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.
size_t RunSpecifiedBenchmarks();
size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter);
// If this routine is called, peak memory allocation past this point in the
// benchmark is reported at the end of the benchmark report line. (It is

View File

@ -863,14 +863,14 @@ void PrintBenchmarkList() {
}
}
void RunMatchingBenchmarks(const std::string& spec,
BenchmarkReporter* reporter) {
size_t RunMatchingBenchmarks(const std::string& spec,
BenchmarkReporter* reporter) {
CHECK(reporter != nullptr);
if (spec.empty()) return;
CHECK(!spec.empty());
std::vector<Benchmark::Instance> benchmarks;
auto families = BenchmarkFamilies::GetInstance();
if (!families->FindBenchmarks(spec, &benchmarks)) return;
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,6 +894,7 @@ void RunMatchingBenchmarks(const std::string& spec,
RunBenchmark(benchmark, reporter);
}
}
return benchmarks.size();
}
std::unique_ptr<BenchmarkReporter> GetDefaultReporter() {
@ -913,14 +914,14 @@ std::unique_ptr<BenchmarkReporter> GetDefaultReporter() {
} // end namespace
} // end namespace internal
void RunSpecifiedBenchmarks() {
RunSpecifiedBenchmarks(nullptr);
size_t RunSpecifiedBenchmarks() {
return RunSpecifiedBenchmarks(nullptr);
}
void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
if (FLAGS_benchmark_list_tests) {
internal::PrintBenchmarkList();
return;
return 0;
}
std::string spec = FLAGS_benchmark_filter;
if (spec.empty() || spec == "all")
@ -931,8 +932,9 @@ void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
default_reporter = internal::GetDefaultReporter();
reporter = default_reporter.get();
}
internal::RunMatchingBenchmarks(spec, reporter);
size_t num_run = internal::RunMatchingBenchmarks(spec, reporter);
reporter->Finalize();
return num_run;
}
namespace internal {

View File

@ -72,7 +72,7 @@ int main(int argc, char* argv[]) {
benchmark::Initialize(&argc, argv);
TestReporter test_reporter;
benchmark::RunSpecifiedBenchmarks(&test_reporter);
const size_t returned_count = benchmark::RunSpecifiedBenchmarks(&test_reporter);
if (argc == 2) {
// Make sure we ran all of the tests
@ -81,9 +81,9 @@ int main(int argc, char* argv[]) {
ss >> expected;
const size_t count = test_reporter.GetCount();
if (count != expected) {
std::cerr << "ERROR: Expected " << expected << " tests to be ran but only "
<< count << " completed" << std::endl;
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;
return -1;
}
}