perf_result_reporter.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TESTING_PERF_PERF_RESULT_REPORTER_H_
  5. #define TESTING_PERF_PERF_RESULT_REPORTER_H_
  6. #include <string>
  7. #include <unordered_map>
  8. #include "base/time/time.h"
  9. namespace perf_test {
  10. struct MetricInfo {
  11. std::string units;
  12. bool important;
  13. };
  14. // A helper class for using the perf test printing functions safely, as
  15. // otherwise it's easy to accidentally mix up arguments to produce usable but
  16. // malformed perf data. See https://crbug.com/923564.
  17. // Sample usage:
  18. // auto reporter = PerfResultReporter("TextRendering", "100_chars");
  19. // reporter.RegisterImportantMetric(".wall_time", "ms");
  20. // reporter.RegisterImportantMetric(".cpu_time", "ms");
  21. // ...
  22. // reporter.AddResult(".wall_time", GetWallTime());
  23. // reporter.AddResult(".cpu_time", GetCpuTime());
  24. // This would end up reporting "TextRendering.wall_time" and
  25. // "TextRendering.cpu_time" metrics on the dashboard, made up of results from
  26. // a single "100_chars" story. If an additional story run is added, e.g.
  27. // "200_chars", then the metrics will be averaged over both runs with the
  28. // ability to drill down into results for specific stories.
  29. class PerfResultReporter {
  30. public:
  31. PerfResultReporter(const std::string& metric_basename,
  32. const std::string& story_name);
  33. ~PerfResultReporter();
  34. void RegisterFyiMetric(const std::string& metric_suffix,
  35. const std::string& units);
  36. void RegisterImportantMetric(const std::string& metric_suffix,
  37. const std::string& units);
  38. void AddResult(const std::string& metric_suffix, size_t value) const;
  39. void AddResult(const std::string& metric_suffix, double value) const;
  40. void AddResult(const std::string& metric_suffix,
  41. const std::string& value) const;
  42. // A special version of AddResult that will automatically convert the given
  43. // TimeDelta into a double with the correct units for the registered metric.
  44. void AddResult(const std::string& metric_suffix, base::TimeDelta value) const;
  45. void AddResultList(const std::string& metric_suffix,
  46. const std::string& values) const;
  47. // Users should prefer AddResultList if possible, as otherwise the min/max
  48. // values reported on the perf dashboard aren't useful.
  49. // |mean_and_error| should be a comma-separated string of mean then
  50. // error/stddev, e.g. "2.4,0.5".
  51. void AddResultMeanAndError(const std::string& metric_suffix,
  52. const std::string& mean_and_error);
  53. // Returns true and fills the pointer if the metric is registered, otherwise
  54. // returns false.
  55. bool GetMetricInfo(const std::string& metric_suffix, MetricInfo* out) const;
  56. private:
  57. void RegisterMetric(const std::string& metric_suffix,
  58. const std::string& units,
  59. bool important);
  60. MetricInfo GetMetricInfoOrFail(const std::string& metric_suffix) const;
  61. std::string metric_basename_;
  62. std::string story_name_;
  63. std::unordered_map<std::string, MetricInfo> metric_map_;
  64. };
  65. } // namespace perf_test
  66. #endif // TESTING_PERF_PERF_RESULT_REPORTER_H_