aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Hutchinson <nshutchinson@gmail.com>2016-09-11 22:36:14 +0100
committerEric <eric@efcs.ca>2016-09-11 15:36:14 -0600
commitb826143ac20a105f7caba6d1d5afe4c5204864cf (patch)
tree3c23a844e894b9fb67a77d2b2d1bf77592664862
parentc6f3f0eb9cd68150371c0c45b84aeb0dc72114c9 (diff)
downloadgoogle-benchmark-b826143ac20a105f7caba6d1d5afe4c5204864cf.tar.gz
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.
-rw-r--r--src/benchmark.cc14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/benchmark.cc b/src/benchmark.cc
index 7bd5f3b..a5073f5 100644
--- a/src/benchmark.cc
+++ b/src/benchmark.cc
@@ -480,17 +480,31 @@ void RunMatchingBenchmarks(const std::vector<Benchmark::Instance>& benchmarks,
// Keep track of runing times of all instances of current benchmark
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)
&& (!file_reporter || file_reporter->ReportContext(context))) {
+ flushStreams(console_reporter);
+ flushStreams(file_reporter);
for (const auto& benchmark : benchmarks) {
std::vector<BenchmarkReporter::Run> reports =
RunBenchmark(benchmark, &complexity_reports);
console_reporter->ReportRuns(reports);
if (file_reporter) file_reporter->ReportRuns(reports);
+ flushStreams(console_reporter);
+ flushStreams(file_reporter);
}
}
console_reporter->Finalize();
if (file_reporter) file_reporter->Finalize();
+ flushStreams(console_reporter);
+ flushStreams(file_reporter);
}
std::unique_ptr<BenchmarkReporter>