Expose default display reporter creation in public API (#1344)

* Expose default display reporter creation in public API

this is useful when a custom reporter wants to fall back on the default
display reporter, but doesn't necessarily have access to the benchmark
library flag configuration.

* Make use of unique_ptr in the random interleaving test.

* clang-format
This commit is contained in:
Dominic Hamon 2022-02-11 10:23:05 +00:00 committed by GitHub
parent d2cbd4b26a
commit 6e51dcbcc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 7 deletions

View File

@ -293,6 +293,11 @@ bool ReportUnrecognizedArguments(int argc, char** argv);
// Returns the current value of --benchmark_filter. // Returns the current value of --benchmark_filter.
std::string GetBenchmarkFilter(); std::string GetBenchmarkFilter();
// Creates a default display reporter. Used by the library when no display
// reporter is provided, but also made available for external use in case a
// custom reporter should respect the `--benchmark_format` flag as a fallback
BenchmarkReporter* CreateDefaultDisplayReporter();
// Generate a list of benchmarks matching 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 // and if --benchmark_list_tests is specified return after printing the name
// of each matching benchmark. Otherwise run each matching benchmark and // of each matching benchmark. Otherwise run each matching benchmark and

View File

@ -388,9 +388,9 @@ std::unique_ptr<BenchmarkReporter> CreateReporter(
if (name == "console") { if (name == "console") {
return PtrType(new ConsoleReporter(output_opts)); return PtrType(new ConsoleReporter(output_opts));
} else if (name == "json") { } else if (name == "json") {
return PtrType(new JSONReporter); return PtrType(new JSONReporter());
} else if (name == "csv") { } else if (name == "csv") {
return PtrType(new CSVReporter); return PtrType(new CSVReporter());
} else { } else {
std::cerr << "Unexpected format: '" << name << "'\n"; std::cerr << "Unexpected format: '" << name << "'\n";
std::exit(1); std::exit(1);
@ -431,6 +431,14 @@ ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color) {
} // end namespace internal } // end namespace internal
BenchmarkReporter* CreateDefaultDisplayReporter() {
static auto default_display_reporter =
internal::CreateReporter(FLAGS_benchmark_format,
internal::GetOutputOptions())
.release();
return default_display_reporter;
}
size_t RunSpecifiedBenchmarks() { size_t RunSpecifiedBenchmarks() {
return RunSpecifiedBenchmarks(nullptr, nullptr, FLAGS_benchmark_filter); return RunSpecifiedBenchmarks(nullptr, nullptr, FLAGS_benchmark_filter);
} }
@ -466,8 +474,7 @@ size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
std::unique_ptr<BenchmarkReporter> default_display_reporter; std::unique_ptr<BenchmarkReporter> default_display_reporter;
std::unique_ptr<BenchmarkReporter> default_file_reporter; std::unique_ptr<BenchmarkReporter> default_file_reporter;
if (!display_reporter) { if (!display_reporter) {
default_display_reporter = internal::CreateReporter( default_display_reporter.reset(CreateDefaultDisplayReporter());
FLAGS_benchmark_format, internal::GetOutputOptions());
display_reporter = default_display_reporter.get(); display_reporter = default_display_reporter.get();
} }
auto& Out = display_reporter->GetOutputStream(); auto& Out = display_reporter->GetOutputStream();

View File

@ -51,10 +51,9 @@ class BenchmarkTest : public testing::Test {
void Execute(const std::string& pattern) { void Execute(const std::string& pattern) {
queue->Clear(); queue->Clear();
BenchmarkReporter* reporter = new NullReporter; std::unique_ptr<BenchmarkReporter> reporter(new NullReporter());
FLAGS_benchmark_filter = pattern; FLAGS_benchmark_filter = pattern;
RunSpecifiedBenchmarks(reporter); RunSpecifiedBenchmarks(reporter.get());
delete reporter;
queue->Put("DONE"); // End marker queue->Put("DONE"); // End marker
} }