1 /*
2 * Copyright (c) 2020 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 #include "test/testsupport/perf_test_histogram_writer.h"
12
13 #include <memory>
14 #include <string>
15
16 #include "test/gtest.h"
17 #include "third_party/catapult/tracing/tracing/value/histogram.h"
18
19 namespace webrtc {
20 namespace test {
21
22 namespace proto = catapult::tracing::tracing::proto;
23
TEST(PerfHistogramWriterUnittest,TestSimpleHistogram)24 TEST(PerfHistogramWriterUnittest, TestSimpleHistogram) {
25 std::unique_ptr<PerfTestResultWriter> writer =
26 std::unique_ptr<PerfTestResultWriter>(CreateHistogramWriter());
27
28 writer->LogResult("-", "-", 0, "ms", false, ImproveDirection::kNone);
29
30 proto::HistogramSet histogram_set;
31 EXPECT_TRUE(histogram_set.ParseFromString(writer->Serialize()))
32 << "Expected valid histogram set";
33
34 ASSERT_EQ(histogram_set.histograms_size(), 1);
35 }
36
TEST(PerfHistogramWriterUnittest,WritesSamplesAndUserStory)37 TEST(PerfHistogramWriterUnittest, WritesSamplesAndUserStory) {
38 std::unique_ptr<PerfTestResultWriter> writer =
39 std::unique_ptr<PerfTestResultWriter>(CreateHistogramWriter());
40
41 writer->LogResult("measurement", "user_story", 15e7, "Hz", false,
42 ImproveDirection::kBiggerIsBetter);
43
44 proto::HistogramSet histogram_set;
45 histogram_set.ParseFromString(writer->Serialize());
46 const proto::Histogram& hist1 = histogram_set.histograms(0);
47
48 EXPECT_EQ(hist1.name(), "measurement");
49
50 EXPECT_EQ(hist1.unit().unit(), proto::HERTZ);
51 EXPECT_EQ(hist1.unit().improvement_direction(), proto::BIGGER_IS_BETTER);
52
53 EXPECT_EQ(hist1.sample_values_size(), 1);
54 EXPECT_EQ(hist1.sample_values(0), 15e7);
55
56 EXPECT_EQ(hist1.diagnostics().diagnostic_map().count("stories"), 1u);
57 const proto::Diagnostic& stories =
58 hist1.diagnostics().diagnostic_map().at("stories");
59 ASSERT_EQ(stories.generic_set().values_size(), 1);
60 EXPECT_EQ(stories.generic_set().values(0), "\"user_story\"");
61 }
62
TEST(PerfHistogramWriterUnittest,WritesOneHistogramPerMeasurementAndStory)63 TEST(PerfHistogramWriterUnittest, WritesOneHistogramPerMeasurementAndStory) {
64 std::unique_ptr<PerfTestResultWriter> writer =
65 std::unique_ptr<PerfTestResultWriter>(CreateHistogramWriter());
66
67 writer->LogResult("measurement", "story1", 1, "ms", false,
68 ImproveDirection::kNone);
69 writer->LogResult("measurement", "story1", 2, "ms", false,
70 ImproveDirection::kNone);
71 writer->LogResult("measurement", "story2", 2, "ms", false,
72 ImproveDirection::kNone);
73
74 proto::HistogramSet histogram_set;
75 histogram_set.ParseFromString(writer->Serialize());
76 ASSERT_EQ(histogram_set.histograms_size(), 2);
77
78 const proto::Histogram& hist1 = histogram_set.histograms(0);
79 const proto::Histogram& hist2 = histogram_set.histograms(1);
80
81 EXPECT_EQ(hist1.name(), "measurement");
82 EXPECT_EQ(hist2.name(), "measurement");
83
84 const proto::Diagnostic& stories1 =
85 hist1.diagnostics().diagnostic_map().at("stories");
86 EXPECT_EQ(stories1.generic_set().values(0), "\"story1\"");
87 EXPECT_EQ(hist1.sample_values_size(), 2);
88
89 const proto::Diagnostic& stories2 =
90 hist2.diagnostics().diagnostic_map().at("stories");
91 EXPECT_EQ(stories2.generic_set().values(0), "\"story2\"");
92 EXPECT_EQ(hist2.sample_values_size(), 1);
93 }
94
TEST(PerfHistogramWriterUnittest,IgnoresError)95 TEST(PerfHistogramWriterUnittest, IgnoresError) {
96 std::unique_ptr<PerfTestResultWriter> writer =
97 std::unique_ptr<PerfTestResultWriter>(CreateHistogramWriter());
98
99 writer->LogResultMeanAndError("-", "-", 17, 12345, "ms", false,
100 ImproveDirection::kNone);
101
102 proto::HistogramSet histogram_set;
103 histogram_set.ParseFromString(writer->Serialize());
104 const proto::Histogram& hist1 = histogram_set.histograms(0);
105
106 EXPECT_EQ(hist1.running().mean(), 17);
107 EXPECT_EQ(hist1.running().variance(), 0) << "The error should be ignored.";
108 }
109
TEST(PerfHistogramWriterUnittest,WritesDecibelIntoMeasurementName)110 TEST(PerfHistogramWriterUnittest, WritesDecibelIntoMeasurementName) {
111 std::unique_ptr<PerfTestResultWriter> writer =
112 std::unique_ptr<PerfTestResultWriter>(CreateHistogramWriter());
113
114 writer->LogResult("measurement", "-", 0, "dB", false,
115 ImproveDirection::kNone);
116
117 proto::HistogramSet histogram_set;
118 histogram_set.ParseFromString(writer->Serialize());
119 const proto::Histogram& hist1 = histogram_set.histograms(0);
120
121 EXPECT_EQ(hist1.unit().unit(), proto::UNITLESS)
122 << "dB should map to unitless";
123 EXPECT_EQ(hist1.name(), "measurement_dB") << "measurement should be renamed";
124 }
125
TEST(PerfHistogramWriterUnittest,WritesFpsIntoMeasurementName)126 TEST(PerfHistogramWriterUnittest, WritesFpsIntoMeasurementName) {
127 std::unique_ptr<PerfTestResultWriter> writer =
128 std::unique_ptr<PerfTestResultWriter>(CreateHistogramWriter());
129
130 writer->LogResult("measurement", "-", 0, "fps", false,
131 ImproveDirection::kNone);
132
133 proto::HistogramSet histogram_set;
134 histogram_set.ParseFromString(writer->Serialize());
135 const proto::Histogram& hist1 = histogram_set.histograms(0);
136
137 EXPECT_EQ(hist1.unit().unit(), proto::HERTZ) << "fps should map to hertz";
138 EXPECT_EQ(hist1.name(), "measurement_fps") << "measurement should be renamed";
139 }
140
TEST(PerfHistogramWriterUnittest,WritesPercentIntoMeasurementName)141 TEST(PerfHistogramWriterUnittest, WritesPercentIntoMeasurementName) {
142 std::unique_ptr<PerfTestResultWriter> writer =
143 std::unique_ptr<PerfTestResultWriter>(CreateHistogramWriter());
144
145 writer->LogResult("measurement", "-", 0, "%", false, ImproveDirection::kNone);
146
147 proto::HistogramSet histogram_set;
148 histogram_set.ParseFromString(writer->Serialize());
149 const proto::Histogram& hist1 = histogram_set.histograms(0);
150
151 EXPECT_EQ(hist1.unit().unit(), proto::UNITLESS)
152 << "percent should map to hertz";
153 EXPECT_EQ(hist1.name(), "measurement_%") << "measurement should be renamed";
154 }
155
TEST(PerfHistogramWriterUnittest,BitsPerSecondIsConvertedToBytes)156 TEST(PerfHistogramWriterUnittest, BitsPerSecondIsConvertedToBytes) {
157 std::unique_ptr<PerfTestResultWriter> writer =
158 std::unique_ptr<PerfTestResultWriter>(CreateHistogramWriter());
159
160 writer->LogResult("-", "-", 1024, "bps", false, ImproveDirection::kNone);
161
162 proto::HistogramSet histogram_set;
163 histogram_set.ParseFromString(writer->Serialize());
164 const proto::Histogram& hist1 = histogram_set.histograms(0);
165
166 EXPECT_EQ(hist1.sample_values(0), 128) << "1024 bits = 128 bytes";
167 }
168
TEST(PerfHistogramWriterUnittest,ParsesDirection)169 TEST(PerfHistogramWriterUnittest, ParsesDirection) {
170 std::unique_ptr<PerfTestResultWriter> writer =
171 std::unique_ptr<PerfTestResultWriter>(CreateHistogramWriter());
172
173 writer->LogResult("measurement1", "-", 0, "bps", false,
174 ImproveDirection::kBiggerIsBetter);
175 writer->LogResult("measurement2", "-", 0, "frames", false,
176 ImproveDirection::kSmallerIsBetter);
177 writer->LogResult("measurement3", "-", 0, "sigma", false,
178 ImproveDirection::kNone);
179
180 proto::HistogramSet histogram_set;
181 histogram_set.ParseFromString(writer->Serialize());
182 const proto::Histogram& hist1 = histogram_set.histograms(0);
183 const proto::Histogram& hist2 = histogram_set.histograms(1);
184 const proto::Histogram& hist3 = histogram_set.histograms(2);
185
186 EXPECT_EQ(hist1.unit().unit(), proto::BYTES_PER_SECOND);
187 EXPECT_EQ(hist1.unit().improvement_direction(), proto::BIGGER_IS_BETTER);
188
189 EXPECT_EQ(hist2.unit().unit(), proto::COUNT);
190 EXPECT_EQ(hist2.unit().improvement_direction(), proto::SMALLER_IS_BETTER);
191
192 EXPECT_EQ(hist3.unit().unit(), proto::SIGMA);
193 EXPECT_EQ(hist3.unit().improvement_direction(), proto::NOT_SPECIFIED);
194 }
195
196 } // namespace test
197 } // namespace webrtc
198