You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "test/testsupport/perf_test.h"
  11. #include <stdio.h>
  12. #include <cmath>
  13. #include <fstream>
  14. #include <map>
  15. #include <set>
  16. #include <sstream>
  17. #include <vector>
  18. #include "rtc_base/checks.h"
  19. #include "rtc_base/critical_section.h"
  20. namespace {
  21. template <typename Container>
  22. void OutputListToStream(std::ostream* ostream, const Container& values) {
  23. const char* sep = "";
  24. for (const auto& v : values) {
  25. (*ostream) << sep << v;
  26. sep = ",";
  27. }
  28. }
  29. struct PlottableCounter {
  30. std::string graph_name;
  31. std::string trace_name;
  32. webrtc::SamplesStatsCounter counter;
  33. std::string units;
  34. };
  35. class PerfResultsLogger {
  36. public:
  37. PerfResultsLogger() : crit_(), output_(stdout), graphs_() {}
  38. void ClearResults() {
  39. rtc::CritScope lock(&crit_);
  40. graphs_.clear();
  41. }
  42. void SetOutput(FILE* output) {
  43. rtc::CritScope lock(&crit_);
  44. output_ = output;
  45. }
  46. void LogResult(const std::string& graph_name,
  47. const std::string& trace_name,
  48. const webrtc::SamplesStatsCounter& counter,
  49. const std::string& units,
  50. const bool important,
  51. webrtc::test::ImproveDirection improve_direction) {
  52. LogResultMeanAndError(
  53. graph_name, trace_name, counter.IsEmpty() ? 0 : counter.GetAverage(),
  54. counter.IsEmpty() ? 0 : counter.GetStandardDeviation(), units,
  55. important, improve_direction);
  56. rtc::CritScope lock(&crit_);
  57. plottable_counters_.push_back({graph_name, trace_name, counter, units});
  58. }
  59. void LogResult(const std::string& graph_name,
  60. const std::string& trace_name,
  61. const double value,
  62. const std::string& units,
  63. const bool important,
  64. webrtc::test::ImproveDirection improve_direction) {
  65. RTC_CHECK(std::isfinite(value))
  66. << "Expected finite value for graph " << graph_name << ", trace name "
  67. << trace_name << ", units " << units << ", got " << value;
  68. std::ostringstream value_stream;
  69. value_stream.precision(8);
  70. value_stream << value;
  71. LogResultsImpl(graph_name, trace_name, value_stream.str(), units, important,
  72. improve_direction);
  73. std::ostringstream json_stream;
  74. json_stream << '"' << trace_name << R"(":{)";
  75. json_stream << R"("type":"scalar",)";
  76. json_stream << R"("value":)" << value << ',';
  77. json_stream << R"("units":")" << UnitWithDirection(units, improve_direction)
  78. << R"("})";
  79. rtc::CritScope lock(&crit_);
  80. graphs_[graph_name].push_back(json_stream.str());
  81. }
  82. void LogResultMeanAndError(const std::string& graph_name,
  83. const std::string& trace_name,
  84. const double mean,
  85. const double error,
  86. const std::string& units,
  87. const bool important,
  88. webrtc::test::ImproveDirection improve_direction) {
  89. RTC_CHECK(std::isfinite(mean));
  90. RTC_CHECK(std::isfinite(error));
  91. std::ostringstream value_stream;
  92. value_stream.precision(8);
  93. value_stream << '{' << mean << ',' << error << '}';
  94. LogResultsImpl(graph_name, trace_name, value_stream.str(), units, important,
  95. improve_direction);
  96. std::ostringstream json_stream;
  97. json_stream << '"' << trace_name << R"(":{)";
  98. json_stream << R"("type":"list_of_scalar_values",)";
  99. json_stream << R"("values":[)" << mean << "],";
  100. json_stream << R"("std":)" << error << ',';
  101. json_stream << R"("units":")" << UnitWithDirection(units, improve_direction)
  102. << R"("})";
  103. rtc::CritScope lock(&crit_);
  104. graphs_[graph_name].push_back(json_stream.str());
  105. }
  106. void LogResultList(const std::string& graph_name,
  107. const std::string& trace_name,
  108. const rtc::ArrayView<const double> values,
  109. const std::string& units,
  110. const bool important,
  111. webrtc::test::ImproveDirection improve_direction) {
  112. for (double v : values) {
  113. RTC_CHECK(std::isfinite(v));
  114. }
  115. std::ostringstream value_stream;
  116. value_stream.precision(8);
  117. value_stream << '[';
  118. OutputListToStream(&value_stream, values);
  119. value_stream << ']';
  120. LogResultsImpl(graph_name, trace_name, value_stream.str(), units, important,
  121. improve_direction);
  122. std::ostringstream json_stream;
  123. json_stream << '"' << trace_name << R"(":{)";
  124. json_stream << R"("type":"list_of_scalar_values",)";
  125. json_stream << R"("values":)" << value_stream.str() << ',';
  126. json_stream << R"("units":")" << UnitWithDirection(units, improve_direction)
  127. << R"("})";
  128. rtc::CritScope lock(&crit_);
  129. graphs_[graph_name].push_back(json_stream.str());
  130. }
  131. std::string ToJSON() const;
  132. void PrintPlottableCounters(
  133. const std::vector<std::string>& desired_graphs_raw) const {
  134. std::set<std::string> desired_graphs(desired_graphs_raw.begin(),
  135. desired_graphs_raw.end());
  136. rtc::CritScope lock(&crit_);
  137. for (auto& counter : plottable_counters_) {
  138. if (!desired_graphs.empty()) {
  139. auto it = desired_graphs.find(counter.graph_name);
  140. if (it == desired_graphs.end()) {
  141. continue;
  142. }
  143. }
  144. std::ostringstream value_stream;
  145. value_stream.precision(8);
  146. value_stream << R"({"graph_name":")" << counter.graph_name << R"(",)";
  147. value_stream << R"("trace_name":")" << counter.trace_name << R"(",)";
  148. value_stream << R"("units":")" << counter.units << R"(",)";
  149. if (!counter.counter.IsEmpty()) {
  150. value_stream << R"("mean":)" << counter.counter.GetAverage() << ',';
  151. value_stream << R"("std":)" << counter.counter.GetStandardDeviation()
  152. << ',';
  153. }
  154. value_stream << R"("samples":[)";
  155. const char* sep = "";
  156. for (const auto& sample : counter.counter.GetTimedSamples()) {
  157. value_stream << sep << R"({"time":)" << sample.time.us() << ','
  158. << R"("value":)" << sample.value << '}';
  159. sep = ",";
  160. }
  161. value_stream << "]}";
  162. fprintf(output_, "PLOTTABLE_DATA: %s\n", value_stream.str().c_str());
  163. }
  164. }
  165. private:
  166. void LogResultsImpl(const std::string& graph_name,
  167. const std::string& trace,
  168. const std::string& values,
  169. const std::string& units,
  170. bool important,
  171. webrtc::test::ImproveDirection improve_direction) {
  172. // <*>RESULT <graph_name>: <trace_name>= <value> <units>
  173. // <*>RESULT <graph_name>: <trace_name>= {<mean>, <std deviation>} <units>
  174. // <*>RESULT <graph_name>: <trace_name>= [<value>,value,value,...,] <units>
  175. rtc::CritScope lock(&crit_);
  176. if (important) {
  177. fprintf(output_, "*");
  178. }
  179. fprintf(output_, "RESULT %s: %s= %s %s\n", graph_name.c_str(),
  180. trace.c_str(), values.c_str(),
  181. UnitWithDirection(units, improve_direction).c_str());
  182. }
  183. std::string UnitWithDirection(
  184. const std::string& units,
  185. webrtc::test::ImproveDirection improve_direction) {
  186. switch (improve_direction) {
  187. case webrtc::test::ImproveDirection::kNone:
  188. return units;
  189. case webrtc::test::ImproveDirection::kSmallerIsBetter:
  190. return units + "_smallerIsBetter";
  191. case webrtc::test::ImproveDirection::kBiggerIsBetter:
  192. return units + "_biggerIsBetter";
  193. }
  194. }
  195. rtc::CriticalSection crit_;
  196. FILE* output_ RTC_GUARDED_BY(&crit_);
  197. std::map<std::string, std::vector<std::string>> graphs_
  198. RTC_GUARDED_BY(&crit_);
  199. std::vector<PlottableCounter> plottable_counters_ RTC_GUARDED_BY(&crit_);
  200. };
  201. std::string PerfResultsLogger::ToJSON() const {
  202. std::ostringstream json_stream;
  203. json_stream << R"({"format_version":"1.0",)";
  204. json_stream << R"("charts":{)";
  205. rtc::CritScope lock(&crit_);
  206. for (auto graphs_it = graphs_.begin(); graphs_it != graphs_.end();
  207. ++graphs_it) {
  208. if (graphs_it != graphs_.begin())
  209. json_stream << ',';
  210. json_stream << '"' << graphs_it->first << "\":";
  211. json_stream << '{';
  212. OutputListToStream(&json_stream, graphs_it->second);
  213. json_stream << '}';
  214. }
  215. json_stream << "}}";
  216. return json_stream.str();
  217. }
  218. PerfResultsLogger& GetPerfResultsLogger() {
  219. static PerfResultsLogger* const logger_ = new PerfResultsLogger();
  220. return *logger_;
  221. }
  222. } // namespace
  223. namespace webrtc {
  224. namespace test {
  225. void ClearPerfResults() {
  226. GetPerfResultsLogger().ClearResults();
  227. }
  228. void SetPerfResultsOutput(FILE* output) {
  229. GetPerfResultsLogger().SetOutput(output);
  230. }
  231. std::string GetPerfResultsJSON() {
  232. return GetPerfResultsLogger().ToJSON();
  233. }
  234. void PrintPlottableResults(const std::vector<std::string>& desired_graphs) {
  235. GetPerfResultsLogger().PrintPlottableCounters(desired_graphs);
  236. }
  237. void WritePerfResults(const std::string& output_path) {
  238. std::string json_results = GetPerfResultsJSON();
  239. std::fstream json_file(output_path, std::fstream::out);
  240. json_file << json_results;
  241. json_file.close();
  242. }
  243. void PrintResult(const std::string& measurement,
  244. const std::string& modifier,
  245. const std::string& trace,
  246. const double value,
  247. const std::string& units,
  248. bool important,
  249. ImproveDirection improve_direction) {
  250. GetPerfResultsLogger().LogResult(measurement + modifier, trace, value, units,
  251. important, improve_direction);
  252. }
  253. void PrintResult(const std::string& measurement,
  254. const std::string& modifier,
  255. const std::string& trace,
  256. const SamplesStatsCounter& counter,
  257. const std::string& units,
  258. const bool important,
  259. ImproveDirection improve_direction) {
  260. GetPerfResultsLogger().LogResult(measurement + modifier, trace, counter,
  261. units, important, improve_direction);
  262. }
  263. void PrintResultMeanAndError(const std::string& measurement,
  264. const std::string& modifier,
  265. const std::string& trace,
  266. const double mean,
  267. const double error,
  268. const std::string& units,
  269. bool important,
  270. ImproveDirection improve_direction) {
  271. GetPerfResultsLogger().LogResultMeanAndError(measurement + modifier, trace,
  272. mean, error, units, important,
  273. improve_direction);
  274. }
  275. void PrintResultList(const std::string& measurement,
  276. const std::string& modifier,
  277. const std::string& trace,
  278. const rtc::ArrayView<const double> values,
  279. const std::string& units,
  280. bool important,
  281. ImproveDirection improve_direction) {
  282. GetPerfResultsLogger().LogResultList(measurement + modifier, trace, values,
  283. units, important, improve_direction);
  284. }
  285. } // namespace test
  286. } // namespace webrtc