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,78 +244,75 @@ static string metricReadable(double n, unsigned int decimals) { ...@@ -244,78 +244,75 @@ 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;
for (auto& datum : data) { void print(const vector<detail::BenchmarkResult>& data) {
auto file = datum.file; for (auto& datum : data) {
if (file != lastFile) { auto file = datum.file;
// New file starting if (file != lastFile) {
header(file); // New file starting
lastFile = file; header(file);
} lastFile = file;
}
string s = datum.name; string s = datum.name;
if (s == "-") { if (s == "-") {
separator('-'); separator('-');
continue; continue;
} }
bool useBaseline /* = void */; bool useBaseline /* = void */;
if (s[0] == '%') { if (s[0] == '%') {
s.erase(0, 1); s.erase(0, 1);
useBaseline = true; useBaseline = true;
} else { } else {
baselineNsPerIter = datum.timeInNs; baselineNsPerIter = datum.timeInNs;
useBaseline = false; useBaseline = false;
} }
s.resize(columns - 29, ' '); s.resize(columns - 29, ' ');
auto nsPerIter = datum.timeInNs; auto nsPerIter = datum.timeInNs;
auto secPerIter = nsPerIter / 1E9; auto secPerIter = nsPerIter / 1E9;
auto itersPerSec = (secPerIter == 0) auto itersPerSec = (secPerIter == 0)
? std::numeric_limits<double>::infinity() ? std::numeric_limits<double>::infinity()
: (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",
readableTime(secPerIter, 2).c_str(), static_cast<int>(s.size()),
metricReadable(itersPerSec, 2).c_str()); s.c_str(),
} else { readableTime(secPerIter, 2).c_str(),
// Print with baseline metricReadable(itersPerSec, 2).c_str());
auto rel = baselineNsPerIter / nsPerIter * 100.0; } else {
printf("%*s %7.2f%% %9s %7s\n", // Print with baseline
static_cast<int>(s.size()), s.c_str(), auto rel = baselineNsPerIter / nsPerIter * 100.0;
rel, printf(
readableTime(secPerIter, 2).c_str(), "%*s %7.2f%% %9s %7s\n",
metricReadable(itersPerSec, 2).c_str()); static_cast<int>(s.size()),
s.c_str(),
rel,
readableTime(secPerIter, 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);
} }
results.push_back({bm.file, bm.name, elapsed});
if (!FLAGS_json_verbose && !FLAGS_json) {
printer.print({{bm.file, bm.name, elapsed}});
} else {
results.push_back({bm.file, bm.name, elapsed});
}
} }
// PLEASE MAKE NOISE. MEASUREMENTS DONE. // PLEASE MAKE NOISE. MEASUREMENTS DONE.
printBenchmarkResults(results); if (FLAGS_json_verbose || FLAGS_json) {
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