metrics_logger.cc (4122B)
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 #include "api/test/metrics/metrics_logger.h" 11 12 #include <map> 13 #include <optional> 14 #include <string> 15 #include <utility> 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/units/timestamp.h" 22 #include "rtc_base/synchronization/mutex.h" 23 24 namespace webrtc { 25 namespace test { 26 namespace { 27 28 Metric::Stats ToStats(const SamplesStatsCounter& values) { 29 if (values.IsEmpty()) { 30 return Metric::Stats(); 31 } 32 return Metric::Stats{.mean = values.GetAverage(), 33 .stddev = values.GetStandardDeviation(), 34 .min = values.GetMin(), 35 .max = values.GetMax()}; 36 } 37 38 } // namespace 39 40 void DefaultMetricsLogger::LogSingleValueMetric( 41 absl::string_view name, 42 absl::string_view test_case_name, 43 double value, 44 Unit unit, 45 ImprovementDirection improvement_direction, 46 std::map<std::string, std::string> metadata) { 47 MutexLock lock(&mutex_); 48 metrics_.push_back(Metric{ 49 .name = std::string(name), 50 .unit = unit, 51 .improvement_direction = improvement_direction, 52 .test_case = std::string(test_case_name), 53 .metric_metadata = std::move(metadata), 54 .time_series = 55 Metric::TimeSeries{.samples = std::vector{Metric::TimeSeries::Sample{ 56 .timestamp = Now(), .value = value}}}, 57 .stats = Metric::Stats{ 58 .mean = value, .stddev = std::nullopt, .min = value, .max = value}}); 59 } 60 61 void DefaultMetricsLogger::LogMetric( 62 absl::string_view name, 63 absl::string_view test_case_name, 64 const SamplesStatsCounter& values, 65 Unit unit, 66 ImprovementDirection improvement_direction, 67 std::map<std::string, std::string> metadata) { 68 MutexLock lock(&mutex_); 69 Metric::TimeSeries time_series; 70 for (const SamplesStatsCounter::StatsSample& sample : 71 values.GetTimedSamples()) { 72 time_series.samples.push_back( 73 Metric::TimeSeries::Sample{.timestamp = sample.time, 74 .value = sample.value, 75 .sample_metadata = sample.metadata}); 76 } 77 78 metrics_.push_back(Metric{.name = std::string(name), 79 .unit = unit, 80 .improvement_direction = improvement_direction, 81 .test_case = std::string(test_case_name), 82 .metric_metadata = std::move(metadata), 83 .time_series = std::move(time_series), 84 .stats = ToStats(values)}); 85 } 86 87 void DefaultMetricsLogger::LogMetric( 88 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) { 94 MutexLock lock(&mutex_); 95 metrics_.push_back(Metric{.name = std::string(name), 96 .unit = unit, 97 .improvement_direction = improvement_direction, 98 .test_case = std::string(test_case_name), 99 .metric_metadata = std::move(metadata), 100 .time_series = Metric::TimeSeries{.samples = {}}, 101 .stats = std::move(metric_stats)}); 102 } 103 104 std::vector<Metric> DefaultMetricsLogger::GetCollectedMetrics() const { 105 std::vector<Metric> out = metrics_accumulator_.GetCollectedMetrics(); 106 MutexLock lock(&mutex_); 107 out.insert(out.end(), metrics_.begin(), metrics_.end()); 108 return out; 109 } 110 111 Timestamp DefaultMetricsLogger::Now() { 112 return clock_->CurrentTime(); 113 } 114 115 } // namespace test 116 } // namespace webrtc