Commit e0de8171 by Eric Fiselier

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

parent 9fcdd6fc
...@@ -160,10 +160,14 @@ class BenchmarkReporter; ...@@ -160,10 +160,14 @@ class BenchmarkReporter;
void Initialize(int* argc, char** argv); void Initialize(int* argc, char** argv);
// Otherwise, run all benchmarks specified by the --benchmark_filter flag, // Run all benchmarks which match the specified --benchmark_filter flag.
// and exit after running the benchmarks. // The second overload reports the results using the specified 'reporter'.
void RunSpecifiedBenchmarks(); //
void RunSpecifiedBenchmarks(BenchmarkReporter* 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 // 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 // benchmark is reported at the end of the benchmark report line. (It is
......
...@@ -863,14 +863,14 @@ void PrintBenchmarkList() { ...@@ -863,14 +863,14 @@ void PrintBenchmarkList() {
} }
} }
void RunMatchingBenchmarks(const std::string& spec, size_t RunMatchingBenchmarks(const std::string& spec,
BenchmarkReporter* reporter) { BenchmarkReporter* reporter) {
CHECK(reporter != nullptr); CHECK(reporter != nullptr);
if (spec.empty()) return; CHECK(!spec.empty());
std::vector<Benchmark::Instance> benchmarks; std::vector<Benchmark::Instance> benchmarks;
auto families = BenchmarkFamilies::GetInstance(); 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. // 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,6 +894,7 @@ void RunMatchingBenchmarks(const std::string& spec, ...@@ -894,6 +894,7 @@ void RunMatchingBenchmarks(const std::string& spec,
RunBenchmark(benchmark, reporter); RunBenchmark(benchmark, reporter);
} }
} }
return benchmarks.size();
} }
std::unique_ptr<BenchmarkReporter> GetDefaultReporter() { std::unique_ptr<BenchmarkReporter> GetDefaultReporter() {
...@@ -913,14 +914,14 @@ std::unique_ptr<BenchmarkReporter> GetDefaultReporter() { ...@@ -913,14 +914,14 @@ std::unique_ptr<BenchmarkReporter> GetDefaultReporter() {
} // end namespace } // end namespace
} // end namespace internal } // end namespace internal
void RunSpecifiedBenchmarks() { size_t RunSpecifiedBenchmarks() {
RunSpecifiedBenchmarks(nullptr); return RunSpecifiedBenchmarks(nullptr);
} }
void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) { size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
if (FLAGS_benchmark_list_tests) { if (FLAGS_benchmark_list_tests) {
internal::PrintBenchmarkList(); internal::PrintBenchmarkList();
return; return 0;
} }
std::string spec = FLAGS_benchmark_filter; std::string spec = FLAGS_benchmark_filter;
if (spec.empty() || spec == "all") if (spec.empty() || spec == "all")
...@@ -931,8 +932,9 @@ void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) { ...@@ -931,8 +932,9 @@ void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
default_reporter = internal::GetDefaultReporter(); default_reporter = internal::GetDefaultReporter();
reporter = default_reporter.get(); reporter = default_reporter.get();
} }
internal::RunMatchingBenchmarks(spec, reporter); size_t num_run = internal::RunMatchingBenchmarks(spec, reporter);
reporter->Finalize(); reporter->Finalize();
return num_run;
} }
namespace internal { namespace internal {
......
...@@ -72,7 +72,7 @@ int main(int argc, char* argv[]) { ...@@ -72,7 +72,7 @@ int main(int argc, char* argv[]) {
benchmark::Initialize(&argc, argv); benchmark::Initialize(&argc, argv);
TestReporter test_reporter; TestReporter test_reporter;
benchmark::RunSpecifiedBenchmarks(&test_reporter); const size_t returned_count = benchmark::RunSpecifiedBenchmarks(&test_reporter);
if (argc == 2) { if (argc == 2) {
// Make sure we ran all of the tests // Make sure we ran all of the tests
...@@ -81,9 +81,9 @@ int main(int argc, char* argv[]) { ...@@ -81,9 +81,9 @@ int main(int argc, char* argv[]) {
ss >> expected; ss >> expected;
const size_t count = test_reporter.GetCount(); const size_t count = test_reporter.GetCount();
if (count != expected) { if (count != expected || returned_count != expected) {
std::cerr << "ERROR: Expected " << expected << " tests to be ran but only " std::cerr << "ERROR: Expected " << expected << " tests to be run but returned_count = "
<< count << " completed" << std::endl; << returned_count << " and reporter_count = " << count << std::endl;
return -1; return -1;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment