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