blob: 2c8a93c5d4fc064a909758b45f069f5dc33074da [file] [log] [blame]
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +00001/*
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 06:47:31 +020011#include "test/testsupport/perf_test.h"
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +000012
Oleh Prypind136b282018-10-03 13:53:44 +020013#include <limits>
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +000014#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "test/gtest.h"
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +000017
Edward Lemur936dfb12017-11-30 11:43:42 +010018namespace {
19
20const char* kJsonExpected = R"({
21 "format_version":"1.0",
22 "charts":{
23 "foobar":{
24 "baz_v":{
25 "type":"scalar",
26 "value":7,
27 "units":"widgets"
28 },
29 "baz_me":{
Edward Lemurc492bf12018-01-05 15:06:59 +010030 "type":"list_of_scalar_values",
Edward Lemur936dfb12017-11-30 11:43:42 +010031 "values":[1],
32 "std":2,
33 "units":"lemurs"
34 },
35 "baz_vl":{
Edward Lemurc492bf12018-01-05 15:06:59 +010036 "type":"list_of_scalar_values",
Edward Lemur936dfb12017-11-30 11:43:42 +010037 "values":[1,2,3],
38 "units":"units"
39 }
40 },
41 "measurementmodifier":{
42 "trace":{
43 "type":"scalar",
44 "value":42,
45 "units":"units"
46 }
47 }
48 }
49})";
50
51std::string RemoveSpaces(std::string s) {
52 s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
53 s.erase(std::remove(s.begin(), s.end(), '\n'), s.end());
54 return s;
55}
56
57} // namespace
58
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +000059namespace webrtc {
60namespace test {
61
Edward Lemur936dfb12017-11-30 11:43:42 +010062class PerfTest : public ::testing::Test {
63 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020064 void TearDown() override { ClearPerfResults(); }
Edward Lemur936dfb12017-11-30 11:43:42 +010065};
66
Edward Lemurf49a56b2017-11-29 20:29:30 +010067#if defined(WEBRTC_IOS)
Edward Lemur936dfb12017-11-30 11:43:42 +010068#define MAYBE_TestPrintResult DISABLED_TestPrintResult
Edward Lemurf49a56b2017-11-29 20:29:30 +010069#else
Edward Lemur936dfb12017-11-30 11:43:42 +010070#define MAYBE_TestPrintResult TestPrintResult
Edward Lemurf49a56b2017-11-29 20:29:30 +010071#endif
Edward Lemur936dfb12017-11-30 11:43:42 +010072TEST_F(PerfTest, MAYBE_TestPrintResult) {
Edward Lemurf7ff3e82017-11-22 16:32:01 +010073 testing::internal::CaptureStdout();
Edward Lemur936dfb12017-11-30 11:43:42 +010074 std::string expected;
75
76 expected += "RESULT measurementmodifier: trace= 42 units\n";
Edward Lemurf7ff3e82017-11-22 16:32:01 +010077 PrintResult("measurement", "modifier", "trace", 42, "units", false);
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +000078
Edward Lemurcb666f52017-12-04 13:21:01 +010079 expected += "*RESULT foobar: baz_v= 1423730 widgets\n";
80 PrintResult("foo", "bar", "baz_v", 1423730, "widgets", true);
Edward Lemur2f061682017-11-24 13:40:01 +010081
Edward Lemur936dfb12017-11-30 11:43:42 +010082 expected += "RESULT foobar: baz_me= {1,2} lemurs\n";
83 PrintResultMeanAndError("foo", "bar", "baz_me", 1, 2, "lemurs", false);
Edward Lemur2f061682017-11-24 13:40:01 +010084
Edward Lemur936dfb12017-11-30 11:43:42 +010085 const double kListOfScalars[] = {1, 2, 3};
86 expected += "RESULT foobar: baz_vl= [1,2,3] units\n";
87 PrintResultList("foo", "bar", "baz_vl", kListOfScalars, "units", false);
Edward Lemurf7ff3e82017-11-22 16:32:01 +010088
Edward Lemur936dfb12017-11-30 11:43:42 +010089 EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
90}
91
92TEST_F(PerfTest, TestGetPerfResultsJSON) {
93 PrintResult("measurement", "modifier", "trace", 42, "units", false);
94 PrintResult("foo", "bar", "baz_v", 7, "widgets", true);
95 PrintResultMeanAndError("foo", "bar", "baz_me", 1, 2, "lemurs", false);
96 const double kListOfScalars[] = {1, 2, 3};
97 PrintResultList("foo", "bar", "baz_vl", kListOfScalars, "units", false);
98
99 EXPECT_EQ(RemoveSpaces(kJsonExpected), GetPerfResultsJSON());
100}
101
102TEST_F(PerfTest, TestClearPerfResults) {
103 PrintResult("measurement", "modifier", "trace", 42, "units", false);
104 ClearPerfResults();
105 EXPECT_EQ(R"({"format_version":"1.0","charts":{}})", GetPerfResultsJSON());
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +0000106}
107
Oleh Prypind136b282018-10-03 13:53:44 +0200108#if GTEST_HAS_DEATH_TEST
109using PerfDeathTest = PerfTest;
110
111TEST_F(PerfDeathTest, TestFiniteResultError) {
112 const double kNan = std::numeric_limits<double>::quiet_NaN();
113 const double kInf = std::numeric_limits<double>::infinity();
114
115 EXPECT_DEATH(PrintResult("a", "b", "c", kNan, "d", false), "finit");
116 EXPECT_DEATH(PrintResult("a", "b", "c", kInf, "d", false), "finit");
117
118 EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", kNan, 1, "d", false), "");
119 EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", 1, kInf, "d", false), "");
120
121 const double kNanList[] = {kNan, kNan};
122 EXPECT_DEATH(PrintResultList("a", "b", "c", kNanList, "d", false), "");
123 const double kInfList[] = {0, kInf};
124 EXPECT_DEATH(PrintResultList("a", "b", "c", kInfList, "d", false), "");
125}
126#endif
127
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +0000128} // namespace test
129} // namespace webrtc