Commit b826143a by Nicholas Hutchinson Committed by Eric

Flush reporters' output streams after writing a benchmark run (#288)

If a reporter's output stream isn't line-buffered (e.g. it's not writing to a terminal) then it can be some time before a write to it becomes visible. This is problematic if, say, you're wanting to use tail -f to view the file written to via --benchmark_out. Or if the application crashes, leaving you with no results. Addressed by flushing the reporters' output streams whenever we invoke methods that may write to them.
parent c6f3f0eb
...@@ -480,17 +480,31 @@ void RunMatchingBenchmarks(const std::vector<Benchmark::Instance>& benchmarks, ...@@ -480,17 +480,31 @@ void RunMatchingBenchmarks(const std::vector<Benchmark::Instance>& benchmarks,
// Keep track of runing times of all instances of current benchmark // Keep track of runing times of all instances of current benchmark
std::vector<BenchmarkReporter::Run> complexity_reports; std::vector<BenchmarkReporter::Run> complexity_reports;
// We flush streams after invoking reporter methods that write to them. This
// ensures users get timely updates even when streams are not line-buffered.
auto flushStreams = [](BenchmarkReporter* reporter) {
if (!reporter) return;
std::flush(reporter->GetOutputStream());
std::flush(reporter->GetErrorStream());
};
if (console_reporter->ReportContext(context) if (console_reporter->ReportContext(context)
&& (!file_reporter || file_reporter->ReportContext(context))) { && (!file_reporter || file_reporter->ReportContext(context))) {
flushStreams(console_reporter);
flushStreams(file_reporter);
for (const auto& benchmark : benchmarks) { for (const auto& benchmark : benchmarks) {
std::vector<BenchmarkReporter::Run> reports = std::vector<BenchmarkReporter::Run> reports =
RunBenchmark(benchmark, &complexity_reports); RunBenchmark(benchmark, &complexity_reports);
console_reporter->ReportRuns(reports); console_reporter->ReportRuns(reports);
if (file_reporter) file_reporter->ReportRuns(reports); if (file_reporter) file_reporter->ReportRuns(reports);
flushStreams(console_reporter);
flushStreams(file_reporter);
} }
} }
console_reporter->Finalize(); console_reporter->Finalize();
if (file_reporter) file_reporter->Finalize(); if (file_reporter) file_reporter->Finalize();
flushStreams(console_reporter);
flushStreams(file_reporter);
} }
std::unique_ptr<BenchmarkReporter> std::unique_ptr<BenchmarkReporter>
......
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