Skip to content

Commit c0e4136

Browse files
andreas-abelcopybara-github
authored andcommitted
Exclude the iteration count from the benchmark name
PiperOrigin-RevId: 728718349 Change-Id: I45986b511e0c7a63f7ddfee45100541aca61296c
1 parent 6070d43 commit c0e4136

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

fleetbench/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ cc_library(
107107
deps = [
108108
":dynamic_registrar",
109109
":maybe_llvmlibc",
110+
"//fleetbench/common",
110111
"@com_google_absl//absl/flags:parse",
111112
"@com_google_absl//absl/log",
112113
"@com_google_absl//absl/log:check",

fleetbench/benchmark_main.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "absl/log/check.h"
2525
#include "absl/log/log.h"
2626
#include "benchmark/benchmark.h"
27+
#include "fleetbench/common/common.h"
2728
#include "fleetbench/dynamic_registrar.h"
2829
#include "numa.h"
2930
#include "tcmalloc/malloc_extension.h"
@@ -84,6 +85,15 @@ int main(int argc, char* argv[]) {
8485
}
8586
fleetbench::DynamicRegistrar::Get()->Run();
8687

87-
benchmark::RunSpecifiedBenchmarks();
88+
fleetbench::FleetbenchReporter display_reporter(
89+
benchmark::CreateDefaultDisplayReporter());
90+
if (!fleetbench::FindInCommandLine("--benchmark_out=")) {
91+
benchmark::RunSpecifiedBenchmarks(&display_reporter);
92+
} else {
93+
std::unique_ptr<benchmark::BenchmarkReporter> default_file_reporter =
94+
fleetbench::CreateDefaultFileReporter();
95+
fleetbench::FleetbenchReporter file_reporter(default_file_reporter.get());
96+
benchmark::RunSpecifiedBenchmarks(&display_reporter, &file_reporter);
97+
}
8898
return 0;
8999
}

fleetbench/common/common.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <filesystem> // NOLINT
1919
#include <fstream>
2020
#include <iostream>
21+
#include <memory>
2122
#include <optional>
2223
#include <random>
2324
#include <string>
@@ -227,4 +228,44 @@ int GetCacheSize(int cache_level, absl::string_view cache_type) {
227228
"the cache sizes (in bytes) manually.";
228229
}
229230

231+
std::unique_ptr<benchmark::BenchmarkReporter> CreateDefaultFileReporter() {
232+
if (fleetbench::FindInCommandLine("--benchmark_out_format=console")) {
233+
benchmark::ConsoleReporter::OutputOptions output_opts =
234+
benchmark::ConsoleReporter::OutputOptions::OO_None;
235+
if (fleetbench::FindInCommandLine("--benchmark_counters_tabular", true) ||
236+
fleetbench::FindInCommandLine("--benchmark_counters_tabular=1") ||
237+
fleetbench::FindInCommandLine("--benchmark_counters_tabular=true")) {
238+
output_opts = benchmark::ConsoleReporter::OutputOptions::OO_Tabular;
239+
}
240+
return std::make_unique<benchmark::ConsoleReporter>(output_opts);
241+
}
242+
if (fleetbench::FindInCommandLine("--benchmark_out_format=csv")) {
243+
return std::make_unique<benchmark::CSVReporter>();
244+
}
245+
return std::make_unique<benchmark::JSONReporter>();
246+
}
247+
248+
bool FindInCommandLine(absl::string_view s, bool exact_match) {
249+
std::ifstream f;
250+
f.open("/proc/self/cmdline");
251+
std::string command_line;
252+
f >> command_line;
253+
f.close();
254+
255+
std::vector<absl::string_view> argv = absl::StrSplit(command_line, '\0');
256+
for (const auto& arg : argv) {
257+
if (absl::StartsWith(arg, s)) {
258+
if (!exact_match || arg == s) {
259+
return true;
260+
}
261+
}
262+
}
263+
return false;
264+
}
265+
266+
bool UseExplicitIterationCounts() {
267+
return !FindInCommandLine("--benchmark_min_time") &&
268+
!FindInCommandLine("--benchmark_list_tests");
269+
}
270+
230271
} // namespace fleetbench

fleetbench/common/common.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define THIRD_PARTY_FLEETBENCH_COMMON_COMMON_H_
1616

1717
#include <filesystem> // NOLINT
18+
#include <memory>
1819
#include <optional>
1920
#include <random>
2021
#include <string>
@@ -23,6 +24,7 @@
2324
#include "absl/container/btree_map.h"
2425
#include "absl/flags/declare.h"
2526
#include "absl/strings/string_view.h"
27+
#include "benchmark/benchmark.h"
2628

2729
// Exposed for testing only.
2830
ABSL_DECLARE_FLAG(bool, fixed_seed);
@@ -90,5 +92,59 @@ std::string GetFleetbenchRuntimePath(absl::string_view path);
9092

9193
int GetCacheSize(int cache_level, absl::string_view cache_type = "");
9294

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+
93149
} // namespace fleetbench
94150
#endif // THIRD_PARTY_FLEETBENCH_COMMON_COMMON_H_

0 commit comments

Comments
 (0)