perf_result_reporter.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2020 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. #ifndef TEST_TESTSUPPORT_PERF_RESULT_REPORTER_H_
  11. #define TEST_TESTSUPPORT_PERF_RESULT_REPORTER_H_
  12. #include <string>
  13. #include <unordered_map>
  14. #include "absl/types/optional.h"
  15. #include "api/array_view.h"
  16. #include "test/testsupport/perf_test.h"
  17. namespace webrtc {
  18. namespace test {
  19. // These match the units in histogram.proto (in third_party/catapult).
  20. enum class Unit {
  21. kMs,
  22. kMsBestFitFormat,
  23. kMsTs,
  24. kNPercent,
  25. kSizeInBytes,
  26. kBytesPerSecond,
  27. kHertz,
  28. kUnitless,
  29. kCount,
  30. kSigma,
  31. };
  32. struct MetricInfo {
  33. Unit unit;
  34. ImproveDirection improve_direction;
  35. };
  36. // A helper class for using the perf test printing functions safely, as
  37. // otherwise it's easy to accidentally mix up arguments to produce usable but
  38. // malformed perf data. See https://crbug.com/923564.
  39. //
  40. // Sample usage:
  41. // auto reporter = PerfResultReporter("ramp_up_time", "bwe_15s");
  42. // reporter.RegisterImportantMetric(
  43. // "_turn_over_tcp", Unit::kMs, ImproveDirection::kBiggerIsBetter);
  44. // reporter.RegisterImportantMetric("_cpu_time", Unit::kMs);
  45. // ...
  46. // reporter.AddResult("turn_over_tcp", GetTurnOverTcpTime());
  47. // reporter.AddResult("turn_over_udp", GetTurnOverUdpTime());
  48. //
  49. // This will show in the dashboard as
  50. // (test binary name) > (bot) > ramp_up_time_turn_over_tcp > bwe_15s.
  51. // (test binary name) > (bot) > ramp_up_time_turn_over_udp > bwe_15s.
  52. //
  53. // If you add more reporters that cover other user stories, they will show up
  54. // as separate subtests (e.g. next to bwe_15s).
  55. class PerfResultReporter {
  56. public:
  57. PerfResultReporter(const std::string& metric_basename,
  58. const std::string& story_name);
  59. ~PerfResultReporter();
  60. void RegisterMetric(const std::string& metric_suffix, Unit unit);
  61. void RegisterMetric(const std::string& metric_suffix,
  62. Unit unit,
  63. ImproveDirection improve_direction);
  64. void AddResult(const std::string& metric_suffix, size_t value) const;
  65. void AddResult(const std::string& metric_suffix, double value) const;
  66. void AddResultList(const std::string& metric_suffix,
  67. rtc::ArrayView<const double> values) const;
  68. // Users should prefer AddResultList if possible, as otherwise the min/max
  69. // values reported on the perf dashboard aren't useful.
  70. // |mean_and_error| should be a comma-separated string of mean then
  71. // error/stddev, e.g. "2.4,0.5".
  72. void AddResultMeanAndError(const std::string& metric_suffix,
  73. const double mean,
  74. const double error);
  75. // Returns the metric info if it has been registered.
  76. absl::optional<MetricInfo> GetMetricInfo(
  77. const std::string& metric_suffix) const;
  78. private:
  79. MetricInfo GetMetricInfoOrFail(const std::string& metric_suffix) const;
  80. std::string metric_basename_;
  81. std::string story_name_;
  82. std::unordered_map<std::string, MetricInfo> metric_map_;
  83. };
  84. } // namespace test
  85. } // namespace webrtc
  86. #endif // TEST_TESTSUPPORT_PERF_RESULT_REPORTER_H_