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/gtest.h"
18 #include "test/testsupport/rtc_expect_death.h"
19
20 #if WEBRTC_ENABLE_PROTOBUF
21 #include "third_party/catapult/tracing/tracing/value/histogram.h"
22 namespace proto = catapult::tracing::tracing::proto;
23 #endif
24
25 namespace webrtc {
26 namespace test {
27
28 class PerfTest : public ::testing::Test {
29 protected:
TearDown()30 void TearDown() override { ClearPerfResults(); }
31 };
32
33 #if defined(WEBRTC_IOS)
34 #define MAYBE_TestPrintResult DISABLED_TestPrintResult
35 #else
36 #define MAYBE_TestPrintResult TestPrintResult
37 #endif
TEST_F(PerfTest,MAYBE_TestPrintResult)38 TEST_F(PerfTest, MAYBE_TestPrintResult) {
39 ::testing::internal::CaptureStdout();
40 std::string expected;
41
42 expected += "RESULT measurementmodifier: trace= 42 units\n";
43 PrintResult("measurement", "modifier", "trace", 42, "units", false);
44
45 expected += "*RESULT foobar: baz_v= 1423730 widgets\n";
46 PrintResult("foo", "bar", "baz_v", 1423730, "widgets", true);
47
48 expected += "RESULT foobar: baz_me= {1,2} lemurs\n";
49 PrintResultMeanAndError("foo", "bar", "baz_me", 1, 2, "lemurs", false);
50
51 const double kListOfScalars[] = {1, 2, 3};
52 expected += "RESULT foobar: baz_vl= [1,2,3] units\n";
53 PrintResultList("foo", "bar", "baz_vl", kListOfScalars, "units", false);
54
55 EXPECT_EQ(expected, ::testing::internal::GetCapturedStdout());
56 }
57
TEST_F(PerfTest,TestClearPerfResults)58 TEST_F(PerfTest, TestClearPerfResults) {
59 PrintResult("measurement", "modifier", "trace", 42, "units", false);
60 ClearPerfResults();
61 EXPECT_EQ("", GetPerfResults());
62 }
63
64 #if WEBRTC_ENABLE_PROTOBUF
65
TEST_F(PerfTest,TestGetPerfResultsHistograms)66 TEST_F(PerfTest, TestGetPerfResultsHistograms) {
67 PrintResult("measurement", "_modifier", "story_1", 42, "ms", false);
68 PrintResult("foo", "bar", "story_1", 7, "sigma", true);
69 // Note: the error will be ignored, not supported by histograms.
70 PrintResultMeanAndError("foo", "bar", "story_1", 1, 2000, "sigma", false);
71 const double kListOfScalars[] = {1, 2, 3};
72 PrintResultList("foo", "bar", "story_1", kListOfScalars, "sigma", false);
73
74 proto::HistogramSet histogram_set;
75 EXPECT_TRUE(histogram_set.ParseFromString(GetPerfResults()))
76 << "Expected valid histogram set";
77
78 ASSERT_EQ(histogram_set.histograms_size(), 2)
79 << "Should be two histograms: foobar and measurement_modifier";
80 const proto::Histogram& hist1 = histogram_set.histograms(0);
81 const proto::Histogram& hist2 = histogram_set.histograms(1);
82
83 EXPECT_EQ(hist1.name(), "foobar");
84
85 // Spot check some things in here (there's a more thorough test on the
86 // histogram writer itself).
87 EXPECT_EQ(hist1.unit().unit(), proto::SIGMA);
88 EXPECT_EQ(hist1.sample_values_size(), 5);
89 EXPECT_EQ(hist1.sample_values(0), 7);
90 EXPECT_EQ(hist1.sample_values(1), 1);
91 EXPECT_EQ(hist1.sample_values(2), 1);
92 EXPECT_EQ(hist1.sample_values(3), 2);
93 EXPECT_EQ(hist1.sample_values(4), 3);
94
95 EXPECT_EQ(hist1.diagnostics().diagnostic_map().count("stories"), 1u);
96 const proto::Diagnostic& stories =
97 hist1.diagnostics().diagnostic_map().at("stories");
98 ASSERT_EQ(stories.generic_set().values_size(), 1);
99 EXPECT_EQ(stories.generic_set().values(0), "\"story_1\"");
100
101 EXPECT_EQ(hist2.name(), "measurement_modifier");
102 EXPECT_EQ(hist2.unit().unit(), proto::MS_BEST_FIT_FORMAT);
103 }
104
105 #endif // WEBRTC_ENABLE_PROTOBUF
106
107 #if GTEST_HAS_DEATH_TEST
108 using PerfDeathTest = PerfTest;
109
TEST_F(PerfDeathTest,TestFiniteResultError)110 TEST_F(PerfDeathTest, TestFiniteResultError) {
111 const double kNan = std::numeric_limits<double>::quiet_NaN();
112 const double kInf = std::numeric_limits<double>::infinity();
113
114 RTC_EXPECT_DEATH(PrintResult("a", "b", "c", kNan, "d", false), "finit");
115 RTC_EXPECT_DEATH(PrintResult("a", "b", "c", kInf, "d", false), "finit");
116
117 RTC_EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", kNan, 1, "d", false),
118 "");
119 RTC_EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", 1, kInf, "d", false),
120 "");
121
122 const double kNanList[] = {kNan, kNan};
123 RTC_EXPECT_DEATH(PrintResultList("a", "b", "c", kNanList, "d", false), "");
124 const double kInfList[] = {0, kInf};
125 RTC_EXPECT_DEATH(PrintResultList("a", "b", "c", kInfList, "d", false), "");
126 }
127 #endif
128
129 } // namespace test
130 } // namespace webrtc
131