metrics_logger.h (4066B)
1 /* 2 * Copyright (c) 2022 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 11 #ifndef API_TEST_METRICS_METRICS_LOGGER_H_ 12 #define API_TEST_METRICS_METRICS_LOGGER_H_ 13 14 #include <map> 15 #include <string> 16 #include <vector> 17 18 #include "absl/strings/string_view.h" 19 #include "api/numerics/samples_stats_counter.h" 20 #include "api/test/metrics/metric.h" 21 #include "api/test/metrics/metrics_accumulator.h" 22 #include "api/units/timestamp.h" 23 #include "rtc_base/synchronization/mutex.h" 24 #include "rtc_base/thread_annotations.h" 25 #include "system_wrappers/include/clock.h" 26 27 namespace webrtc { 28 namespace test { 29 30 // Provides API to log and collect performance metrics. 31 class MetricsLogger { 32 public: 33 virtual ~MetricsLogger() = default; 34 35 // Adds a metric with a single value. 36 // `metadata` - metric's level metadata to add. 37 virtual void LogSingleValueMetric( 38 absl::string_view name, 39 absl::string_view test_case_name, 40 double value, 41 Unit unit, 42 ImprovementDirection improvement_direction, 43 std::map<std::string, std::string> metadata = {}) = 0; 44 45 // Adds metrics with a time series created based on the provided `values`. 46 // `metadata` - metric's level metadata to add. 47 virtual void LogMetric(absl::string_view name, 48 absl::string_view test_case_name, 49 const SamplesStatsCounter& values, 50 Unit unit, 51 ImprovementDirection improvement_direction, 52 std::map<std::string, std::string> metadata = {}) = 0; 53 54 // Adds metric with a time series with only stats object and without actual 55 // collected values. 56 // `metadata` - metric's level metadata to add. 57 virtual void LogMetric(absl::string_view name, 58 absl::string_view test_case_name, 59 const Metric::Stats& metric_stats, 60 Unit unit, 61 ImprovementDirection improvement_direction, 62 std::map<std::string, std::string> metadata = {}) = 0; 63 64 // Returns all metrics collected by this logger. 65 virtual std::vector<Metric> GetCollectedMetrics() const = 0; 66 }; 67 68 class DefaultMetricsLogger : public MetricsLogger { 69 public: 70 explicit DefaultMetricsLogger(webrtc::Clock* clock) : clock_(clock) {} 71 ~DefaultMetricsLogger() override = default; 72 73 void LogSingleValueMetric( 74 absl::string_view name, 75 absl::string_view test_case_name, 76 double value, 77 Unit unit, 78 ImprovementDirection improvement_direction, 79 std::map<std::string, std::string> metadata = {}) override; 80 81 void LogMetric(absl::string_view name, 82 absl::string_view test_case_name, 83 const SamplesStatsCounter& values, 84 Unit unit, 85 ImprovementDirection improvement_direction, 86 std::map<std::string, std::string> metadata = {}) override; 87 88 void LogMetric(absl::string_view name, 89 absl::string_view test_case_name, 90 const Metric::Stats& metric_stats, 91 Unit unit, 92 ImprovementDirection improvement_direction, 93 std::map<std::string, std::string> metadata = {}) override; 94 95 // Returns all metrics collected by this logger and its `MetricsAccumulator`. 96 std::vector<Metric> GetCollectedMetrics() const override; 97 98 MetricsAccumulator* GetMetricsAccumulator() { return &metrics_accumulator_; } 99 100 private: 101 webrtc::Timestamp Now(); 102 103 webrtc::Clock* const clock_; 104 MetricsAccumulator metrics_accumulator_; 105 106 mutable Mutex mutex_; 107 std::vector<Metric> metrics_ RTC_GUARDED_BY(mutex_); 108 }; 109 110 } // namespace test 111 } // namespace webrtc 112 113 #endif // API_TEST_METRICS_METRICS_LOGGER_H_