Add an option to print perf results to a file.

video_quality_analysis unittests need to print perf results to a file [1].
Add an option to make this possible.

[1] https://webrtc.googlesource.com/src/+/master/rtc_tools/frame_analyzer/video_quality_analysis_unittest.cc#72

R=kwiberg@webrtc.org, oprypin@webrtc.org
TBR=phoglund@webrtc.org

Bug: chromium:755660
Change-Id: Ife83c4f026cc5a65dd0a430ddc9ff12eb27ae77c
Reviewed-on: https://webrtc-review.googlesource.com/43460
Commit-Queue: Edward Lemur <ehmaldonado@webrtc.org>
Reviewed-by: Oleh Prypin <oprypin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21763}
diff --git a/test/testsupport/perf_test.cc b/test/testsupport/perf_test.cc
index 015e140..b79c706 100644
--- a/test/testsupport/perf_test.cc
+++ b/test/testsupport/perf_test.cc
@@ -19,22 +19,6 @@
 
 namespace {
 
-void PrintResultsImpl(const std::string& graph_name,
-                      const std::string& trace,
-                      const std::string& values,
-                      const std::string& units,
-                      bool important) {
-  // <*>RESULT <graph_name>: <trace_name>= <value> <units>
-  // <*>RESULT <graph_name>: <trace_name>= {<mean>, <std deviation>} <units>
-  // <*>RESULT <graph_name>: <trace_name>= [<value>,value,value,...,] <units>
-
-  if (important) {
-    printf("*");
-  }
-  printf("RESULT %s: %s= %s %s\n", graph_name.c_str(), trace.c_str(),
-         values.c_str(), units.c_str());
-}
-
 template <typename Container>
 void OutputListToStream(std::ostream* ostream, const Container& values) {
   const char* sep = "";
@@ -46,10 +30,15 @@
 
 class PerfResultsLogger {
  public:
+  PerfResultsLogger() : crit_(), output_(stdout), graphs_() {}
   void ClearResults() {
     rtc::CritScope lock(&crit_);
     graphs_.clear();
   }
+  void SetOutput(FILE* output) {
+    rtc::CritScope lock(&crit_);
+    output_ = output;
+  }
   void LogResult(const std::string& graph_name,
                  const std::string& trace_name,
                  const double value,
@@ -58,8 +47,8 @@
     std::ostringstream value_stream;
     value_stream.precision(8);
     value_stream << value;
-    PrintResultsImpl(graph_name, trace_name, value_stream.str(), units,
-                     important);
+    LogResultsImpl(graph_name, trace_name, value_stream.str(), units,
+                   important);
 
     std::ostringstream json_stream;
     json_stream << '"' << trace_name << R"(":{)";
@@ -78,8 +67,8 @@
     std::ostringstream value_stream;
     value_stream.precision(8);
     value_stream << '{' << mean << ',' << error << '}';
-    PrintResultsImpl(graph_name, trace_name, value_stream.str(), units,
-                     important);
+    LogResultsImpl(graph_name, trace_name, value_stream.str(), units,
+                   important);
 
     std::ostringstream json_stream;
     json_stream << '"' << trace_name << R"(":{)";
@@ -100,8 +89,8 @@
     value_stream << '[';
     OutputListToStream(&value_stream, values);
     value_stream << ']';
-    PrintResultsImpl(graph_name, trace_name, value_stream.str(), units,
-                     important);
+    LogResultsImpl(graph_name, trace_name, value_stream.str(), units,
+                   important);
 
     std::ostringstream json_stream;
     json_stream << '"' << trace_name << R"(":{)";
@@ -114,7 +103,26 @@
   std::string ToJSON() const;
 
  private:
+  void LogResultsImpl(const std::string& graph_name,
+                      const std::string& trace,
+                      const std::string& values,
+                      const std::string& units,
+                      bool important) {
+    // <*>RESULT <graph_name>: <trace_name>= <value> <units>
+    // <*>RESULT <graph_name>: <trace_name>= {<mean>, <std deviation>} <units>
+    // <*>RESULT <graph_name>: <trace_name>= [<value>,value,value,...,] <units>
+    rtc::CritScope lock(&crit_);
+
+    if (important) {
+      fprintf(output_, "*");
+    }
+    fprintf(output_, "RESULT %s: %s= %s %s\n", graph_name.c_str(),
+            trace.c_str(), values.c_str(), units.c_str());
+  }
+
   rtc::CriticalSection crit_;
+  FILE* output_
+      RTC_GUARDED_BY(&crit_);
   std::map<std::string, std::vector<std::string>> graphs_
       RTC_GUARDED_BY(&crit_);
 };
@@ -151,6 +159,10 @@
   GetPerfResultsLogger().ClearResults();
 }
 
+void SetPerfResultsOutput(FILE* output) {
+  GetPerfResultsLogger().SetOutput(output);
+}
+
 std::string GetPerfResultsJSON() {
   return GetPerfResultsLogger().ToJSON();
 }
diff --git a/test/testsupport/perf_test.h b/test/testsupport/perf_test.h
index f28dd2b2ac..fbc1ef9 100644
--- a/test/testsupport/perf_test.h
+++ b/test/testsupport/perf_test.h
@@ -61,11 +61,17 @@
                      const std::string& units,
                      bool important);
 
-// Write  all perf results to date to a JSON file formatted as described in
+// Returns all perf results to date in a JSON string formatted as described in
 // https://github.com/catapult-project/catapult/blob/master/dashboard/docs/data-format.md
+std::string GetPerfResultsJSON();
+
+// Writes the JSON representation of the perf results returned by
+// GetPerfResultsJSON() to the file in output_path.
 void WritePerfResults(const std::string& output_path);
 
-std::string GetPerfResultsJSON();
+// By default, perf results are printed to stdout. Set the FILE* to where they
+// should be printing instead.
+void SetPerfResultsOutput(const FILE* output);
 
 // You shouldn't use this function. It's only used to test the functions above.
 void ClearPerfResults();