1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/psi_memory_parser.h"
9 #include "base/files/file_util.h"
10 #include "base/metrics/statistics_recorder.h"
11 #include "base/test/metrics/histogram_tester.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 // Just as the kernel outputs.
19 const char kFileContents1[] =
20 "some avg10=23.10 avg60=5.06 avg300=15.10 total=417963\n"
21 "full avg10=9.00 avg60=19.20 avg300=3.23 total=205933\n";
23 // Number of decimals not consistent, slightly malformed - but acceptable.
24 const char kFileContents2[] =
25 "some avg10=24 avg60=5.06 avg300=15.10 total=417963\n"
26 "full avg10=9.2 avg60=19.20 avg300=3.23 total=205933\n";
30 class PSIMemoryParserTest : public testing::Test {
32 PSIMemoryParserTest() = default;
33 ~PSIMemoryParserTest() override = default;
35 void Init(uint32_t period) {
36 cit_ = std::make_unique<PSIMemoryParser>(period);
39 uint32_t GetPeriod() { return cit_->GetPeriod(); }
40 base::HistogramTester& Histograms() { return histogram_tester_; }
41 std::unique_ptr<PSIMemoryParser>& Cit() { return cit_; }
42 const std::string& GetMetricPrefix() { return cit_->metric_prefix_; }
44 void KillCit() { cit_.reset(); }
47 std::unique_ptr<PSIMemoryParser> cit_;
48 base::HistogramTester histogram_tester_;
51 TEST_F(PSIMemoryParserTest, CustomInterval) {
54 EXPECT_EQ(60u, GetPeriod());
57 TEST_F(PSIMemoryParserTest, InvalidInterval) {
60 EXPECT_EQ(10u, GetPeriod());
63 TEST_F(PSIMemoryParserTest, InternalsA) {
66 std::string testContent1 = "prefix" + GetMetricPrefix() + "9.37 suffix";
67 EXPECT_EQ(10u, GetPeriod());
72 EXPECT_EQ(false, internal::FindMiddleString(testContent1, 0, "nothere",
75 EXPECT_EQ(false, internal::FindMiddleString(testContent1, 0, "prefix",
78 EXPECT_EQ(true, internal::FindMiddleString(testContent1, 0, "prefix",
83 EXPECT_EQ(937, Cit()->GetMetricValue(testContent1, s, e));
85 std::string testContent2 = "extra " + testContent1;
86 EXPECT_EQ(true, internal::FindMiddleString(testContent2, 0, "prefix",
91 EXPECT_EQ(937, Cit()->GetMetricValue(testContent2, s, e));
94 TEST_F(PSIMemoryParserTest, InternalsB) {
99 ParsePSIMemStatus stat;
101 stat = Cit()->ParseMetrics(kFileContents1, &msome, &mfull);
103 EXPECT_EQ(ParsePSIMemStatus::kSuccess, stat);
104 EXPECT_EQ(1510, msome);
105 EXPECT_EQ(323, mfull);
108 TEST_F(PSIMemoryParserTest, InternalsC) {
113 ParsePSIMemStatus stat;
115 stat = Cit()->ParseMetrics(kFileContents1, &msome, &mfull);
117 EXPECT_EQ(ParsePSIMemStatus::kSuccess, stat);
118 EXPECT_EQ(506, msome);
119 EXPECT_EQ(1920, mfull);
122 TEST_F(PSIMemoryParserTest, InternalsD) {
127 ParsePSIMemStatus stat;
129 stat = Cit()->ParseMetrics(kFileContents1, &msome, &mfull);
131 EXPECT_EQ(ParsePSIMemStatus::kSuccess, stat);
132 EXPECT_EQ(2310, msome);
133 EXPECT_EQ(900, mfull);
136 TEST_F(PSIMemoryParserTest, InternalsE) {
141 ParsePSIMemStatus stat;
143 stat = Cit()->ParseMetrics(kFileContents2, &msome, &mfull);
145 EXPECT_EQ(ParsePSIMemStatus::kSuccess, stat);
146 EXPECT_EQ(2400, msome);
147 EXPECT_EQ(920, mfull);
150 TEST_F(PSIMemoryParserTest, ParseResultCounter) {
153 Cit()->LogParseStatus(ParsePSIMemStatus::kSuccess);
154 Cit()->LogParseStatus(ParsePSIMemStatus::kInvalidMetricFormat);
155 Cit()->LogParseStatus(ParsePSIMemStatus::kInvalidMetricFormat);
157 Histograms().ExpectBucketCount("ChromeOS.CWP.ParsePSIMemory",
158 ParsePSIMemStatus::kSuccess, 1);
159 Histograms().ExpectBucketCount("ChromeOS.CWP.ParsePSIMemory",
160 ParsePSIMemStatus::kInvalidMetricFormat, 2);
163 } // namespace metrics