From c9e4522656275f0215836fd44d5ae501115211f4 Mon Sep 17 00:00:00 2001 From: Edward Lemur Date: Thu, 25 Jan 2018 15:17:55 +0100 Subject: [PATCH] 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 Reviewed-by: Oleh Prypin Cr-Commit-Position: refs/heads/master@{#21763} --- test/testsupport/perf_test.cc | 56 +++++++++++++++++++++-------------- test/testsupport/perf_test.h | 10 +++++-- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/test/testsupport/perf_test.cc b/test/testsupport/perf_test.cc index 015e1409a2..b79c7061a6 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 : = - // <*>RESULT : = {, } - // <*>RESULT : = [,value,value,...,] - - if (important) { - printf("*"); - } - printf("RESULT %s: %s= %s %s\n", graph_name.c_str(), trace.c_str(), - values.c_str(), units.c_str()); -} - template void OutputListToStream(std::ostream* ostream, const Container& values) { const char* sep = ""; @@ -46,10 +30,15 @@ void OutputListToStream(std::ostream* ostream, const Container& values) { 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 @@ class PerfResultsLogger { 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 @@ class PerfResultsLogger { 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 @@ class PerfResultsLogger { 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 @@ class PerfResultsLogger { 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 : = + // <*>RESULT : = {, } + // <*>RESULT : = [,value,value,...,] + 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> graphs_ RTC_GUARDED_BY(&crit_); }; @@ -151,6 +159,10 @@ void ClearPerfResults() { 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..fbc1ef94c3 100644 --- a/test/testsupport/perf_test.h +++ b/test/testsupport/perf_test.h @@ -61,11 +61,17 @@ void PrintResultList(const std::string& measurement, 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();