From 6e51dcbcc3965b3f4b13d4bab5e43895c1a73290 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Fri, 11 Feb 2022 10:23:05 +0000 Subject: [PATCH] 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 --- include/benchmark/benchmark.h | 5 +++++ src/benchmark.cc | 15 +++++++++++---- test/benchmark_random_interleaving_gtest.cc | 5 ++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index c8ced387..504fae1f 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -293,6 +293,11 @@ bool ReportUnrecognizedArguments(int argc, char** argv); // Returns the current value of --benchmark_filter. 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 // and if --benchmark_list_tests is specified return after printing the name // of each matching benchmark. Otherwise run each matching benchmark and diff --git a/src/benchmark.cc b/src/benchmark.cc index 1a76b585..f88d9054 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -388,9 +388,9 @@ std::unique_ptr CreateReporter( if (name == "console") { return PtrType(new ConsoleReporter(output_opts)); } else if (name == "json") { - return PtrType(new JSONReporter); + return PtrType(new JSONReporter()); } else if (name == "csv") { - return PtrType(new CSVReporter); + return PtrType(new CSVReporter()); } else { std::cerr << "Unexpected format: '" << name << "'\n"; std::exit(1); @@ -431,6 +431,14 @@ ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color) { } // 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() { return RunSpecifiedBenchmarks(nullptr, nullptr, FLAGS_benchmark_filter); } @@ -466,8 +474,7 @@ size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, std::unique_ptr default_display_reporter; std::unique_ptr default_file_reporter; if (!display_reporter) { - default_display_reporter = internal::CreateReporter( - FLAGS_benchmark_format, internal::GetOutputOptions()); + default_display_reporter.reset(CreateDefaultDisplayReporter()); display_reporter = default_display_reporter.get(); } auto& Out = display_reporter->GetOutputStream(); diff --git a/test/benchmark_random_interleaving_gtest.cc b/test/benchmark_random_interleaving_gtest.cc index d04befa8..7f208675 100644 --- a/test/benchmark_random_interleaving_gtest.cc +++ b/test/benchmark_random_interleaving_gtest.cc @@ -51,10 +51,9 @@ class BenchmarkTest : public testing::Test { void Execute(const std::string& pattern) { queue->Clear(); - BenchmarkReporter* reporter = new NullReporter; + std::unique_ptr reporter(new NullReporter()); FLAGS_benchmark_filter = pattern; - RunSpecifiedBenchmarks(reporter); - delete reporter; + RunSpecifiedBenchmarks(reporter.get()); queue->Put("DONE"); // End marker }