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