blob: 509882db08d617baf503e0eb14f338279a6bed5d [file] [log] [blame]
andrew@webrtc.org2009f6b2012-11-20 00:20:201/*
2 * Copyright (c) 2012 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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "test/testsupport/perf_test.h"
andrew@webrtc.org2009f6b2012-11-20 00:20:2012
Yves Gerey3e707812018-11-28 15:47:4913#include <algorithm>
Oleh Prypind136b282018-10-03 11:53:4414#include <limits>
andrew@webrtc.org2009f6b2012-11-20 00:20:2015#include <string>
16
Mirko Bonadeid2637a32021-12-24 10:27:5517#include "test/gmock.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "test/gtest.h"
Artem Titov9dc209a2019-11-28 16:09:3019#include "test/testsupport/rtc_expect_death.h"
andrew@webrtc.org2009f6b2012-11-20 00:20:2020
Patrik Höglund7f585b32020-01-31 10:22:4221#if WEBRTC_ENABLE_PROTOBUF
22#include "third_party/catapult/tracing/tracing/value/histogram.h"
23namespace proto = catapult::tracing::tracing::proto;
24#endif
25
andrew@webrtc.org2009f6b2012-11-20 00:20:2026namespace webrtc {
27namespace test {
28
Edward Lemur936dfb12017-11-30 10:43:4229class PerfTest : public ::testing::Test {
30 protected:
Yves Gerey665174f2018-06-19 13:03:0531 void TearDown() override { ClearPerfResults(); }
Edward Lemur936dfb12017-11-30 10:43:4232};
33
Edward Lemurf49a56b2017-11-29 19:29:3034#if defined(WEBRTC_IOS)
Edward Lemur936dfb12017-11-30 10:43:4235#define MAYBE_TestPrintResult DISABLED_TestPrintResult
Edward Lemurf49a56b2017-11-29 19:29:3036#else
Edward Lemur936dfb12017-11-30 10:43:4237#define MAYBE_TestPrintResult TestPrintResult
Edward Lemurf49a56b2017-11-29 19:29:3038#endif
Edward Lemur936dfb12017-11-30 10:43:4239TEST_F(PerfTest, MAYBE_TestPrintResult) {
Mirko Bonadei6a489f22019-04-09 13:11:1240 ::testing::internal::CaptureStdout();
Edward Lemur936dfb12017-11-30 10:43:4241 std::string expected;
42
43 expected += "RESULT measurementmodifier: trace= 42 units\n";
Edward Lemurf7ff3e82017-11-22 15:32:0144 PrintResult("measurement", "modifier", "trace", 42, "units", false);
andrew@webrtc.org2009f6b2012-11-20 00:20:2045
Edward Lemurcb666f52017-12-04 12:21:0146 expected += "*RESULT foobar: baz_v= 1423730 widgets\n";
47 PrintResult("foo", "bar", "baz_v", 1423730, "widgets", true);
Edward Lemur2f061682017-11-24 12:40:0148
Edward Lemur936dfb12017-11-30 10:43:4249 expected += "RESULT foobar: baz_me= {1,2} lemurs\n";
50 PrintResultMeanAndError("foo", "bar", "baz_me", 1, 2, "lemurs", false);
Edward Lemur2f061682017-11-24 12:40:0151
Edward Lemur936dfb12017-11-30 10:43:4252 const double kListOfScalars[] = {1, 2, 3};
53 expected += "RESULT foobar: baz_vl= [1,2,3] units\n";
54 PrintResultList("foo", "bar", "baz_vl", kListOfScalars, "units", false);
Edward Lemurf7ff3e82017-11-22 15:32:0155
Mirko Bonadei6a489f22019-04-09 13:11:1256 EXPECT_EQ(expected, ::testing::internal::GetCapturedStdout());
Edward Lemur936dfb12017-11-30 10:43:4257}
58
Edward Lemur936dfb12017-11-30 10:43:4259TEST_F(PerfTest, TestClearPerfResults) {
60 PrintResult("measurement", "modifier", "trace", 42, "units", false);
61 ClearPerfResults();
Patrik Höglund36b35d52020-03-27 13:05:0862 EXPECT_EQ("", GetPerfResults());
andrew@webrtc.org2009f6b2012-11-20 00:20:2063}
64
Patrik Höglund7f585b32020-01-31 10:22:4265#if WEBRTC_ENABLE_PROTOBUF
66
67TEST_F(PerfTest, TestGetPerfResultsHistograms) {
Mirko Bonadei9ff23ba2020-12-14 11:01:5968 ClearPerfResults();
Patrik Höglund7f585b32020-01-31 10:22:4269 PrintResult("measurement", "_modifier", "story_1", 42, "ms", false);
70 PrintResult("foo", "bar", "story_1", 7, "sigma", true);
71 // Note: the error will be ignored, not supported by histograms.
72 PrintResultMeanAndError("foo", "bar", "story_1", 1, 2000, "sigma", false);
73 const double kListOfScalars[] = {1, 2, 3};
74 PrintResultList("foo", "bar", "story_1", kListOfScalars, "sigma", false);
75
76 proto::HistogramSet histogram_set;
77 EXPECT_TRUE(histogram_set.ParseFromString(GetPerfResults()))
78 << "Expected valid histogram set";
79
80 ASSERT_EQ(histogram_set.histograms_size(), 2)
81 << "Should be two histograms: foobar and measurement_modifier";
82 const proto::Histogram& hist1 = histogram_set.histograms(0);
83 const proto::Histogram& hist2 = histogram_set.histograms(1);
84
85 EXPECT_EQ(hist1.name(), "foobar");
86
87 // Spot check some things in here (there's a more thorough test on the
88 // histogram writer itself).
89 EXPECT_EQ(hist1.unit().unit(), proto::SIGMA);
90 EXPECT_EQ(hist1.sample_values_size(), 5);
91 EXPECT_EQ(hist1.sample_values(0), 7);
92 EXPECT_EQ(hist1.sample_values(1), 1);
93 EXPECT_EQ(hist1.sample_values(2), 1);
94 EXPECT_EQ(hist1.sample_values(3), 2);
95 EXPECT_EQ(hist1.sample_values(4), 3);
96
97 EXPECT_EQ(hist1.diagnostics().diagnostic_map().count("stories"), 1u);
98 const proto::Diagnostic& stories =
99 hist1.diagnostics().diagnostic_map().at("stories");
100 ASSERT_EQ(stories.generic_set().values_size(), 1);
101 EXPECT_EQ(stories.generic_set().values(0), "\"story_1\"");
102
103 EXPECT_EQ(hist2.name(), "measurement_modifier");
104 EXPECT_EQ(hist2.unit().unit(), proto::MS_BEST_FIT_FORMAT);
Patrik Höglund7f585b32020-01-31 10:22:42105}
106
Artem Titoveecc4f52021-03-20 11:27:43107TEST_F(PerfTest, TestGetPerfResultsHistogramsWithEmptyCounter) {
108 ClearPerfResults();
109 ::testing::internal::CaptureStdout();
110
111 SamplesStatsCounter empty_counter;
112 PrintResult("measurement", "_modifier", "story", empty_counter, "ms", false);
113
114 proto::HistogramSet histogram_set;
115 EXPECT_TRUE(histogram_set.ParseFromString(GetPerfResults()))
116 << "Expected valid histogram set";
117
118 ASSERT_EQ(histogram_set.histograms_size(), 1)
119 << "Should be one histogram: measurement_modifier";
120 const proto::Histogram& hist = histogram_set.histograms(0);
121
122 EXPECT_EQ(hist.name(), "measurement_modifier");
123
124 // Spot check some things in here (there's a more thorough test on the
125 // histogram writer itself).
126 EXPECT_EQ(hist.unit().unit(), proto::MS_BEST_FIT_FORMAT);
127 EXPECT_EQ(hist.sample_values_size(), 1);
128 EXPECT_EQ(hist.sample_values(0), 0);
129
130 EXPECT_EQ(hist.diagnostics().diagnostic_map().count("stories"), 1u);
131 const proto::Diagnostic& stories =
132 hist.diagnostics().diagnostic_map().at("stories");
133 ASSERT_EQ(stories.generic_set().values_size(), 1);
134 EXPECT_EQ(stories.generic_set().values(0), "\"story\"");
135
136 std::string expected = "RESULT measurement_modifier: story= {0,0} ms\n";
137 EXPECT_EQ(expected, ::testing::internal::GetCapturedStdout());
138}
139
140TEST_F(PerfTest, TestGetPerfResultsHistogramsWithStatsCounter) {
141 ClearPerfResults();
142 ::testing::internal::CaptureStdout();
143
144 SamplesStatsCounter counter;
145 counter.AddSample(1);
146 counter.AddSample(2);
147 counter.AddSample(3);
148 counter.AddSample(4);
149 counter.AddSample(5);
150 PrintResult("measurement", "_modifier", "story", counter, "ms", false);
151
152 proto::HistogramSet histogram_set;
153 EXPECT_TRUE(histogram_set.ParseFromString(GetPerfResults()))
154 << "Expected valid histogram set";
155
156 ASSERT_EQ(histogram_set.histograms_size(), 1)
157 << "Should be one histogram: measurement_modifier";
158 const proto::Histogram& hist = histogram_set.histograms(0);
159
160 EXPECT_EQ(hist.name(), "measurement_modifier");
161
162 // Spot check some things in here (there's a more thorough test on the
163 // histogram writer itself).
164 EXPECT_EQ(hist.unit().unit(), proto::MS_BEST_FIT_FORMAT);
165 EXPECT_EQ(hist.sample_values_size(), 5);
Mirko Bonadeid2637a32021-12-24 10:27:55166 EXPECT_THAT(hist.sample_values(), testing::ElementsAre(1, 2, 3, 4, 5));
Artem Titoveecc4f52021-03-20 11:27:43167
168 EXPECT_EQ(hist.diagnostics().diagnostic_map().count("stories"), 1u);
169 const proto::Diagnostic& stories =
170 hist.diagnostics().diagnostic_map().at("stories");
171 ASSERT_EQ(stories.generic_set().values_size(), 1);
172 EXPECT_EQ(stories.generic_set().values(0), "\"story\"");
173
174 // mean = 3; std = sqrt(2)
175 std::string expected =
176 "RESULT measurement_modifier: story= {3,1.4142136} ms\n";
177 EXPECT_EQ(expected, ::testing::internal::GetCapturedStdout());
178}
179
Patrik Höglund7f585b32020-01-31 10:22:42180#endif // WEBRTC_ENABLE_PROTOBUF
181
Oleh Prypind136b282018-10-03 11:53:44182#if GTEST_HAS_DEATH_TEST
183using PerfDeathTest = PerfTest;
184
185TEST_F(PerfDeathTest, TestFiniteResultError) {
186 const double kNan = std::numeric_limits<double>::quiet_NaN();
187 const double kInf = std::numeric_limits<double>::infinity();
188
Artem Titov9dc209a2019-11-28 16:09:30189 RTC_EXPECT_DEATH(PrintResult("a", "b", "c", kNan, "d", false), "finit");
190 RTC_EXPECT_DEATH(PrintResult("a", "b", "c", kInf, "d", false), "finit");
Oleh Prypind136b282018-10-03 11:53:44191
Artem Titov9dc209a2019-11-28 16:09:30192 RTC_EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", kNan, 1, "d", false),
193 "");
194 RTC_EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", 1, kInf, "d", false),
195 "");
Oleh Prypind136b282018-10-03 11:53:44196
197 const double kNanList[] = {kNan, kNan};
Artem Titov9dc209a2019-11-28 16:09:30198 RTC_EXPECT_DEATH(PrintResultList("a", "b", "c", kNanList, "d", false), "");
Oleh Prypind136b282018-10-03 11:53:44199 const double kInfList[] = {0, kInf};
Artem Titov9dc209a2019-11-28 16:09:30200 RTC_EXPECT_DEATH(PrintResultList("a", "b", "c", kInfList, "d", false), "");
Oleh Prypind136b282018-10-03 11:53:44201}
202#endif
203
andrew@webrtc.org2009f6b2012-11-20 00:20:20204} // namespace test
205} // namespace webrtc