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 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 namespace perf_test 12 { 13 14 struct MetricInfo 15 { 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 { 39 public: 40 PerfResultReporter(const std::string &metric_basename, const std::string &story_name); 41 ~PerfResultReporter(); 42 43 void RegisterFyiMetric(const std::string &metric_suffix, const std::string &units); 44 void RegisterImportantMetric(const std::string &metric_suffix, const std::string &units); 45 void AddResult(const std::string &metric_suffix, size_t value); 46 void AddResult(const std::string &metric_suffix, double value); 47 void AddResult(const std::string &metric_suffix, const std::string &value); 48 49 // Returns true and fills the pointer if the metric is registered, otherwise 50 // returns false. 51 bool GetMetricInfo(const std::string &metric_suffix, MetricInfo *out); 52 53 private: 54 void RegisterMetric(const std::string &metric_suffix, const std::string &units, bool important); 55 56 std::string metric_basename_; 57 std::string story_name_; 58 std::unordered_map<std::string, MetricInfo> metric_map_; 59 }; 60 61 } // namespace perf_test 62 63 #endif // TESTING_PERF_PERF_RESULT_REPORTER_H_ 64