summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/api/test/metrics/metrics_set_proto_file_exporter.cc
blob: 86e6f2e1366fbe0f3024fb9831a8e113e59a64fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 *  Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include "api/test/metrics/metrics_set_proto_file_exporter.h"

#include <stdio.h>

#include <string>

#include "api/test/metrics/metric.h"
#include "rtc_base/logging.h"
#include "test/testsupport/file_utils.h"

#if WEBRTC_ENABLE_PROTOBUF
#include "api/test/metrics/proto/metric.pb.h"
#endif

namespace webrtc {
namespace test {
namespace {

#if WEBRTC_ENABLE_PROTOBUF
webrtc::test_metrics::Unit ToProtoUnit(Unit unit) {
  switch (unit) {
    case Unit::kMilliseconds:
      return webrtc::test_metrics::Unit::MILLISECONDS;
    case Unit::kPercent:
      return webrtc::test_metrics::Unit::PERCENT;
    case Unit::kBytes:
      return webrtc::test_metrics::Unit::BYTES;
    case Unit::kKilobitsPerSecond:
      return webrtc::test_metrics::Unit::KILOBITS_PER_SECOND;
    case Unit::kHertz:
      return webrtc::test_metrics::Unit::HERTZ;
    case Unit::kUnitless:
      return webrtc::test_metrics::Unit::UNITLESS;
    case Unit::kCount:
      return webrtc::test_metrics::Unit::COUNT;
  }
}

webrtc::test_metrics::ImprovementDirection ToProtoImprovementDirection(
    ImprovementDirection direction) {
  switch (direction) {
    case ImprovementDirection::kBiggerIsBetter:
      return webrtc::test_metrics::ImprovementDirection::BIGGER_IS_BETTER;
    case ImprovementDirection::kNeitherIsBetter:
      return webrtc::test_metrics::ImprovementDirection::NEITHER_IS_BETTER;
    case ImprovementDirection::kSmallerIsBetter:
      return webrtc::test_metrics::ImprovementDirection::SMALLER_IS_BETTER;
  }
}

void SetTimeSeries(
    const Metric::TimeSeries& time_series,
    webrtc::test_metrics::Metric::TimeSeries* proto_time_series) {
  for (const Metric::TimeSeries::Sample& sample : time_series.samples) {
    webrtc::test_metrics::Metric::TimeSeries::Sample* proto_sample =
        proto_time_series->add_samples();
    proto_sample->set_value(sample.value);
    proto_sample->set_timestamp_us(sample.timestamp.us());
    for (const auto& [key, value] : sample.sample_metadata) {
      proto_sample->mutable_sample_metadata()->insert({key, value});
    }
  }
}

void SetStats(const Metric::Stats& stats,
              webrtc::test_metrics::Metric::Stats* proto_stats) {
  if (stats.mean.has_value()) {
    proto_stats->set_mean(*stats.mean);
  }
  if (stats.stddev.has_value()) {
    proto_stats->set_stddev(*stats.stddev);
  }
  if (stats.min.has_value()) {
    proto_stats->set_min(*stats.min);
  }
  if (stats.max.has_value()) {
    proto_stats->set_max(*stats.max);
  }
}

bool WriteMetricsToFile(const std::string& path,
                        const webrtc::test_metrics::MetricsSet& metrics_set) {
  std::string data;
  bool ok = metrics_set.SerializeToString(&data);
  if (!ok) {
    RTC_LOG(LS_ERROR) << "Failed to serialize histogram set to string";
    return false;
  }

  CreateDir(DirName(path));
  FILE* output = fopen(path.c_str(), "wb");
  if (output == NULL) {
    RTC_LOG(LS_ERROR) << "Failed to write to " << path;
    return false;
  }
  size_t written = fwrite(data.c_str(), sizeof(char), data.size(), output);
  fclose(output);

  if (written != data.size()) {
    size_t expected = data.size();
    RTC_LOG(LS_ERROR) << "Wrote " << written << ", tried to write " << expected;
    return false;
  }
  return true;
}
#endif  // WEBRTC_ENABLE_PROTOBUF

}  // namespace

MetricsSetProtoFileExporter::Options::Options(
    absl::string_view export_file_path)
    : export_file_path(export_file_path) {}
MetricsSetProtoFileExporter::Options::Options(
    absl::string_view export_file_path,
    bool export_whole_time_series)
    : export_file_path(export_file_path),
      export_whole_time_series(export_whole_time_series) {}

bool MetricsSetProtoFileExporter::Export(rtc::ArrayView<const Metric> metrics) {
#if WEBRTC_ENABLE_PROTOBUF
  webrtc::test_metrics::MetricsSet metrics_set;
  for (const Metric& metric : metrics) {
    webrtc::test_metrics::Metric* metric_proto = metrics_set.add_metrics();
    metric_proto->set_name(metric.name);
    metric_proto->set_unit(ToProtoUnit(metric.unit));
    metric_proto->set_improvement_direction(
        ToProtoImprovementDirection(metric.improvement_direction));
    metric_proto->set_test_case(metric.test_case);
    for (const auto& [key, value] : metric.metric_metadata) {
      metric_proto->mutable_metric_metadata()->insert({key, value});
    }

    if (options_.export_whole_time_series) {
      SetTimeSeries(metric.time_series, metric_proto->mutable_time_series());
    }
    SetStats(metric.stats, metric_proto->mutable_stats());
  }

  return WriteMetricsToFile(options_.export_file_path, metrics_set);
#else
  RTC_LOG(LS_ERROR)
      << "Compile with protobuf support to properly use this class";
  return false;
#endif
}

}  // namespace test
}  // namespace webrtc