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