xref: /aosp_15_r20/external/webrtc/test/testsupport/perf_test_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "test/testsupport/perf_test.h"
12 
13 #include <algorithm>
14 #include <limits>
15 #include <string>
16 
17 #include "test/gmock.h"
18 #include "test/gtest.h"
19 #include "test/testsupport/rtc_expect_death.h"
20 
21 #if WEBRTC_ENABLE_PROTOBUF
22 #include "third_party/catapult/tracing/tracing/value/histogram.h"
23 namespace proto = catapult::tracing::tracing::proto;
24 #endif
25 
26 namespace webrtc {
27 namespace test {
28 
29 class PerfTest : public ::testing::Test {
30  protected:
TearDown()31   void TearDown() override { ClearPerfResults(); }
32 };
33 
34 #if defined(WEBRTC_IOS)
35 #define MAYBE_TestPrintResult DISABLED_TestPrintResult
36 #else
37 #define MAYBE_TestPrintResult TestPrintResult
38 #endif
TEST_F(PerfTest,MAYBE_TestPrintResult)39 TEST_F(PerfTest, MAYBE_TestPrintResult) {
40   ::testing::internal::CaptureStdout();
41   std::string expected;
42 
43   expected += "RESULT measurementmodifier: trace= 42 units\n";
44   PrintResult("measurement", "modifier", "trace", 42, "units", false);
45 
46   expected += "*RESULT foobar: baz_v= 1423730 widgets\n";
47   PrintResult("foo", "bar", "baz_v", 1423730, "widgets", true);
48 
49   expected += "RESULT foobar: baz_me= {1,2} lemurs\n";
50   PrintResultMeanAndError("foo", "bar", "baz_me", 1, 2, "lemurs", false);
51 
52   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);
55 
56   EXPECT_EQ(expected, ::testing::internal::GetCapturedStdout());
57 }
58 
TEST_F(PerfTest,TestClearPerfResults)59 TEST_F(PerfTest, TestClearPerfResults) {
60   PrintResult("measurement", "modifier", "trace", 42, "units", false);
61   ClearPerfResults();
62   EXPECT_EQ("", GetPerfResults());
63 }
64 
65 #if WEBRTC_ENABLE_PROTOBUF
66 
TEST_F(PerfTest,TestGetPerfResultsHistograms)67 TEST_F(PerfTest, TestGetPerfResultsHistograms) {
68   ClearPerfResults();
69   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);
105 }
106 
TEST_F(PerfTest,TestGetPerfResultsHistogramsWithEmptyCounter)107 TEST_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 
TEST_F(PerfTest,TestGetPerfResultsHistogramsWithStatsCounter)140 TEST_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);
166   EXPECT_THAT(hist.sample_values(), testing::ElementsAre(1, 2, 3, 4, 5));
167 
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 
180 #endif  // WEBRTC_ENABLE_PROTOBUF
181 
182 #if GTEST_HAS_DEATH_TEST
183 using PerfDeathTest = PerfTest;
184 
TEST_F(PerfDeathTest,TestFiniteResultError)185 TEST_F(PerfDeathTest, TestFiniteResultError) {
186   const double kNan = std::numeric_limits<double>::quiet_NaN();
187   const double kInf = std::numeric_limits<double>::infinity();
188 
189   RTC_EXPECT_DEATH(PrintResult("a", "b", "c", kNan, "d", false), "finit");
190   RTC_EXPECT_DEATH(PrintResult("a", "b", "c", kInf, "d", false), "finit");
191 
192   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                    "");
196 
197   const double kNanList[] = {kNan, kNan};
198   RTC_EXPECT_DEATH(PrintResultList("a", "b", "c", kNanList, "d", false), "");
199   const double kInfList[] = {0, kInf};
200   RTC_EXPECT_DEATH(PrintResultList("a", "b", "c", kInfList, "d", false), "");
201 }
202 #endif
203 
204 }  // namespace test
205 }  // namespace webrtc
206