Commit 173b48d4 authored by Aaryaman Sagar's avatar Aaryaman Sagar Committed by Facebook Github Bot

Print benchmark results as they complete

Summary:
Print benchmark results as and when they complete instead of outputting the
whole thing once at the end

Reviewed By: djwatson, ot

Differential Revision: D9177038

fbshipit-source-id: 54898b303771f214c21163cd87b11f8e2083cdf4
parent ab24ce0c
...@@ -244,33 +244,25 @@ static string metricReadable(double n, unsigned int decimals) { ...@@ -244,33 +244,25 @@ static string metricReadable(double n, unsigned int decimals) {
return humanReadable(n, decimals, kMetricSuffixes); return humanReadable(n, decimals, kMetricSuffixes);
} }
static void printBenchmarkResultsAsTable( namespace {
const vector<detail::BenchmarkResult>& data) { class BenchmarkResultsPrinter {
// Width available public:
static const unsigned int columns = 76; static constexpr unsigned int columns{76};
double baselineNsPerIter{numeric_limits<double>::max()};
// Compute the longest benchmark name string lastFile;
size_t longestName = 0;
for (auto& bm : benchmarks()) {
longestName = max(longestName, bm.name.size());
}
// Print a horizontal rule void separator(char pad) {
auto separator = [&](char pad) {
puts(string(columns, pad).c_str()); puts(string(columns, pad).c_str());
}; }
// Print header for a file void header(const string& file) {
auto header = [&](const string& file) {
separator('='); separator('=');
printf("%-*srelative time/iter iters/s\n", printf("%-*srelative time/iter iters/s\n",
columns - 28, file.c_str()); columns - 28, file.c_str());
separator('='); separator('=');
}; }
double baselineNsPerIter = numeric_limits<double>::max();
string lastFile;
void print(const vector<detail::BenchmarkResult>& data) {
for (auto& datum : data) { for (auto& datum : data) {
auto file = datum.file; auto file = datum.file;
if (file != lastFile) { if (file != lastFile) {
...@@ -300,22 +292,27 @@ static void printBenchmarkResultsAsTable( ...@@ -300,22 +292,27 @@ static void printBenchmarkResultsAsTable(
: (1 / secPerIter); : (1 / secPerIter);
if (!useBaseline) { if (!useBaseline) {
// Print without baseline // Print without baseline
printf("%*s %9s %7s\n", printf(
static_cast<int>(s.size()), s.c_str(), "%*s %9s %7s\n",
static_cast<int>(s.size()),
s.c_str(),
readableTime(secPerIter, 2).c_str(), readableTime(secPerIter, 2).c_str(),
metricReadable(itersPerSec, 2).c_str()); metricReadable(itersPerSec, 2).c_str());
} else { } else {
// Print with baseline // Print with baseline
auto rel = baselineNsPerIter / nsPerIter * 100.0; auto rel = baselineNsPerIter / nsPerIter * 100.0;
printf("%*s %7.2f%% %9s %7s\n", printf(
static_cast<int>(s.size()), s.c_str(), "%*s %7.2f%% %9s %7s\n",
static_cast<int>(s.size()),
s.c_str(),
rel, rel,
readableTime(secPerIter, 2).c_str(), readableTime(secPerIter, 2).c_str(),
metricReadable(itersPerSec, 2).c_str()); metricReadable(itersPerSec, 2).c_str());
} }
} }
separator('='); }
} };
} // namespace
static void printBenchmarkResultsAsJson( static void printBenchmarkResultsAsJson(
const vector<detail::BenchmarkResult>& data) { const vector<detail::BenchmarkResult>& data) {
...@@ -337,11 +334,13 @@ static void printBenchmarkResultsAsVerboseJson( ...@@ -337,11 +334,13 @@ static void printBenchmarkResultsAsVerboseJson(
static void printBenchmarkResults(const vector<detail::BenchmarkResult>& data) { static void printBenchmarkResults(const vector<detail::BenchmarkResult>& data) {
if (FLAGS_json_verbose) { if (FLAGS_json_verbose) {
printBenchmarkResultsAsVerboseJson(data); printBenchmarkResultsAsVerboseJson(data);
return;
} else if (FLAGS_json) { } else if (FLAGS_json) {
printBenchmarkResultsAsJson(data); printBenchmarkResultsAsJson(data);
} else { return;
printBenchmarkResultsAsTable(data);
} }
CHECK(FLAGS_json_verbose || FLAGS_json) << "Cannot print benchmark results";
} }
void benchmarkResultsToDynamic( void benchmarkResultsToDynamic(
...@@ -461,6 +460,7 @@ void runBenchmarks() { ...@@ -461,6 +460,7 @@ void runBenchmarks() {
auto const globalBaseline = auto const globalBaseline =
runBenchmarkGetNSPerIteration(benchmarks()[baselineIndex].func, 0); runBenchmarkGetNSPerIteration(benchmarks()[baselineIndex].func, 0);
auto printer = BenchmarkResultsPrinter{};
FOR_EACH_RANGE (i, 0, benchmarks().size()) { FOR_EACH_RANGE (i, 0, benchmarks().size()) {
if (i == baselineIndex) { if (i == baselineIndex) {
continue; continue;
...@@ -473,12 +473,21 @@ void runBenchmarks() { ...@@ -473,12 +473,21 @@ void runBenchmarks() {
} }
elapsed = runBenchmarkGetNSPerIteration(bm.func, globalBaseline); elapsed = runBenchmarkGetNSPerIteration(bm.func, globalBaseline);
} }
if (!FLAGS_json_verbose && !FLAGS_json) {
printer.print({{bm.file, bm.name, elapsed}});
} else {
results.push_back({bm.file, bm.name, elapsed}); results.push_back({bm.file, bm.name, elapsed});
} }
}
// PLEASE MAKE NOISE. MEASUREMENTS DONE. // PLEASE MAKE NOISE. MEASUREMENTS DONE.
if (FLAGS_json_verbose || FLAGS_json) {
printBenchmarkResults(results); printBenchmarkResults(results);
} else {
printer.separator('=');
}
} }
} // namespace folly } // namespace folly
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