blob: ecc42585945ea940f1949a3ecbf4cf810eaa060c [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <algorithm>
Oleh Prypind136b282018-10-03 13:53:44 +020014#include <limits>
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +000015#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "test/gtest.h"
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +000018
Edward Lemur936dfb12017-11-30 11:43:42 +010019namespace {
20
21const char* kJsonExpected = R"({
22 "format_version":"1.0",
23 "charts":{
24 "foobar":{
25 "baz_v":{
26 "type":"scalar",
27 "value":7,
28 "units":"widgets"
29 },
30 "baz_me":{
Edward Lemurc492bf12018-01-05 15:06:59 +010031 "type":"list_of_scalar_values",
Edward Lemur936dfb12017-11-30 11:43:42 +010032 "values":[1],
33 "std":2,
34 "units":"lemurs"
35 },
36 "baz_vl":{
Edward Lemurc492bf12018-01-05 15:06:59 +010037 "type":"list_of_scalar_values",
Edward Lemur936dfb12017-11-30 11:43:42 +010038 "values":[1,2,3],
39 "units":"units"
40 }
41 },
42 "measurementmodifier":{
43 "trace":{
44 "type":"scalar",
45 "value":42,
46 "units":"units"
47 }
48 }
49 }
50})";
51
52std::string RemoveSpaces(std::string s) {
53 s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
54 s.erase(std::remove(s.begin(), s.end(), '\n'), s.end());
55 return s;
56}
57
58} // namespace
59
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +000060namespace webrtc {
61namespace test {
62
Edward Lemur936dfb12017-11-30 11:43:42 +010063class PerfTest : public ::testing::Test {
64 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020065 void TearDown() override { ClearPerfResults(); }
Edward Lemur936dfb12017-11-30 11:43:42 +010066};
67
Edward Lemurf49a56b2017-11-29 20:29:30 +010068#if defined(WEBRTC_IOS)
Edward Lemur936dfb12017-11-30 11:43:42 +010069#define MAYBE_TestPrintResult DISABLED_TestPrintResult
Edward Lemurf49a56b2017-11-29 20:29:30 +010070#else
Edward Lemur936dfb12017-11-30 11:43:42 +010071#define MAYBE_TestPrintResult TestPrintResult
Edward Lemurf49a56b2017-11-29 20:29:30 +010072#endif
Edward Lemur936dfb12017-11-30 11:43:42 +010073TEST_F(PerfTest, MAYBE_TestPrintResult) {
Mirko Bonadei6a489f22019-04-09 15:11:12 +020074 ::testing::internal::CaptureStdout();
Edward Lemur936dfb12017-11-30 11:43:42 +010075 std::string expected;
76
77 expected += "RESULT measurementmodifier: trace= 42 units\n";
Edward Lemurf7ff3e82017-11-22 16:32:01 +010078 PrintResult("measurement", "modifier", "trace", 42, "units", false);
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +000079
Edward Lemurcb666f52017-12-04 13:21:01 +010080 expected += "*RESULT foobar: baz_v= 1423730 widgets\n";
81 PrintResult("foo", "bar", "baz_v", 1423730, "widgets", true);
Edward Lemur2f061682017-11-24 13:40:01 +010082
Edward Lemur936dfb12017-11-30 11:43:42 +010083 expected += "RESULT foobar: baz_me= {1,2} lemurs\n";
84 PrintResultMeanAndError("foo", "bar", "baz_me", 1, 2, "lemurs", false);
Edward Lemur2f061682017-11-24 13:40:01 +010085
Edward Lemur936dfb12017-11-30 11:43:42 +010086 const double kListOfScalars[] = {1, 2, 3};
87 expected += "RESULT foobar: baz_vl= [1,2,3] units\n";
88 PrintResultList("foo", "bar", "baz_vl", kListOfScalars, "units", false);
Edward Lemurf7ff3e82017-11-22 16:32:01 +010089
Mirko Bonadei6a489f22019-04-09 15:11:12 +020090 EXPECT_EQ(expected, ::testing::internal::GetCapturedStdout());
Edward Lemur936dfb12017-11-30 11:43:42 +010091}
92
93TEST_F(PerfTest, TestGetPerfResultsJSON) {
94 PrintResult("measurement", "modifier", "trace", 42, "units", false);
95 PrintResult("foo", "bar", "baz_v", 7, "widgets", true);
96 PrintResultMeanAndError("foo", "bar", "baz_me", 1, 2, "lemurs", false);
97 const double kListOfScalars[] = {1, 2, 3};
98 PrintResultList("foo", "bar", "baz_vl", kListOfScalars, "units", false);
99
100 EXPECT_EQ(RemoveSpaces(kJsonExpected), GetPerfResultsJSON());
101}
102
103TEST_F(PerfTest, TestClearPerfResults) {
104 PrintResult("measurement", "modifier", "trace", 42, "units", false);
105 ClearPerfResults();
106 EXPECT_EQ(R"({"format_version":"1.0","charts":{}})", GetPerfResultsJSON());
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +0000107}
108
Oleh Prypind136b282018-10-03 13:53:44 +0200109#if GTEST_HAS_DEATH_TEST
110using PerfDeathTest = PerfTest;
111
112TEST_F(PerfDeathTest, TestFiniteResultError) {
113 const double kNan = std::numeric_limits<double>::quiet_NaN();
114 const double kInf = std::numeric_limits<double>::infinity();
115
116 EXPECT_DEATH(PrintResult("a", "b", "c", kNan, "d", false), "finit");
117 EXPECT_DEATH(PrintResult("a", "b", "c", kInf, "d", false), "finit");
118
119 EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", kNan, 1, "d", false), "");
120 EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", 1, kInf, "d", false), "");
121
122 const double kNanList[] = {kNan, kNan};
123 EXPECT_DEATH(PrintResultList("a", "b", "c", kNanList, "d", false), "");
124 const double kInfList[] = {0, kInf};
125 EXPECT_DEATH(PrintResultList("a", "b", "c", kInfList, "d", false), "");
126}
127#endif
128
andrew@webrtc.org2009f6b2012-11-20 00:20:20 +0000129} // namespace test
130} // namespace webrtc