|
15 | 15 | #define THIRD_PARTY_FLEETBENCH_COMMON_COMMON_H_ |
16 | 16 |
|
17 | 17 | #include <filesystem> // NOLINT |
| 18 | +#include <memory> |
18 | 19 | #include <optional> |
19 | 20 | #include <random> |
20 | 21 | #include <string> |
|
23 | 24 | #include "absl/container/btree_map.h" |
24 | 25 | #include "absl/flags/declare.h" |
25 | 26 | #include "absl/strings/string_view.h" |
| 27 | +#include "benchmark/benchmark.h" |
26 | 28 |
|
27 | 29 | // Exposed for testing only. |
28 | 30 | ABSL_DECLARE_FLAG(bool, fixed_seed); |
@@ -90,5 +92,59 @@ std::string GetFleetbenchRuntimePath(absl::string_view path); |
90 | 92 |
|
91 | 93 | int GetCacheSize(int cache_level, absl::string_view cache_type = ""); |
92 | 94 |
|
| 95 | +// A BenchmarkReporter adapter that removes the iteration count from the |
| 96 | +// benchmark name. |
| 97 | +// By default, the benchmark framework creates a benchmark name that contains |
| 98 | +// `/iterations:x` if the iteration count is specified by calling |
| 99 | +// `->Iterations(x)` on the benchmark object, but not if it is determined |
| 100 | +// automatically, or specified by the `--benchmark_min_time` flag. |
| 101 | +class FleetbenchReporter : public benchmark::BenchmarkReporter { |
| 102 | + public: |
| 103 | + explicit FleetbenchReporter(benchmark::BenchmarkReporter* reporter) |
| 104 | + : reporter_(reporter) {} |
| 105 | + |
| 106 | + void ReportRuns( |
| 107 | + const std::vector<benchmark::BenchmarkReporter::Run>& runs) override { |
| 108 | + std::vector<benchmark::BenchmarkReporter::Run> runs_copy(runs); |
| 109 | + for (auto& run : runs_copy) { |
| 110 | + run.run_name.iterations = ""; |
| 111 | + } |
| 112 | + reporter_->ReportRuns(runs_copy); |
| 113 | + } |
| 114 | + |
| 115 | + void ReportRunsConfig(double min_time, bool has_explicit_iters, |
| 116 | + benchmark::IterationCount iters) override { |
| 117 | + reporter_->ReportRunsConfig(min_time, has_explicit_iters, iters); |
| 118 | + } |
| 119 | + |
| 120 | + bool ReportContext( |
| 121 | + const benchmark::BenchmarkReporter::Context& context) override { |
| 122 | + reporter_->SetOutputStream(&this->GetOutputStream()); |
| 123 | + reporter_->SetErrorStream(&this->GetErrorStream()); |
| 124 | + return reporter_->ReportContext(context); |
| 125 | + } |
| 126 | + |
| 127 | + void Finalize() override { reporter_->Finalize(); } |
| 128 | + |
| 129 | + private: |
| 130 | + benchmark::BenchmarkReporter* const reporter_; |
| 131 | +}; |
| 132 | + |
| 133 | +// Returns a file reporter that corresponds to the reporter that |
| 134 | +// benchmark::RunSpecifiedBenchmarks creates when the file_reporter parameter |
| 135 | +// is not set. |
| 136 | +std::unique_ptr<benchmark::BenchmarkReporter> CreateDefaultFileReporter(); |
| 137 | + |
| 138 | +// Check if any command line argument starts with `s`. |
| 139 | +bool FindInCommandLine(absl::string_view s, bool exact_match = false); |
| 140 | + |
| 141 | +// Returns whether explicit iteration counts can be used when registering |
| 142 | +// benchmarks. This is the case if the `--benchmark_min_time` flag and the |
| 143 | +// `--benchmark_list_tests` flag are not set. `--benchmark_list_tests` is |
| 144 | +// excluded here because it does not use our custom `FleetbenchReporter` (see |
| 145 | +// `benchmark_main.cc`), and thus, the printed benchmark names would contain |
| 146 | +// `/iterations:x` when explicit iteration counts are used. |
| 147 | +bool UseExplicitIterationCounts(); |
| 148 | + |
93 | 149 | } // namespace fleetbench |
94 | 150 | #endif // THIRD_PARTY_FLEETBENCH_COMMON_COMMON_H_ |
0 commit comments