1 // Copyright 2015 The Chromium Authors. All rights reserved.
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/memory_pressure/memory_pressure_stats_collector.h"
7 #include "base/test/histogram_tester.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace memory_pressure
{
16 const char kPressureLevel
[] = "Memory.PressureLevel";
17 const char kPressureLevelChange
[] = "Memory.PressureLevelChange";
21 // Test version of the stats collector with a few extra accessors.
22 class TestMemoryPressureStatsCollector
: public MemoryPressureStatsCollector
{
24 TestMemoryPressureStatsCollector(base::TickClock
* tick_clock
)
25 : MemoryPressureStatsCollector(tick_clock
) {}
28 base::TimeDelta
unreported_cumulative_time(int i
) const {
29 return unreported_cumulative_time_
[i
];
31 MemoryPressureLevel
last_pressure_level() const {
32 return last_pressure_level_
;
34 base::TimeTicks
last_update_time() const { return last_update_time_
; }
38 class MemoryPressureStatsCollectorTest
: public testing::Test
{
40 MemoryPressureStatsCollectorTest() : collector_(&tick_clock_
) {}
43 tick_clock_
.Advance(base::TimeDelta::FromMilliseconds(ms
));
46 // Validates expectations on the amount of accumulated (and unreported)
47 // time (milliseconds) per pressure level.
48 void ExpectAccumulated(int none_ms
, int moderate_ms
, int critical_ms
) {
49 EXPECT_EQ(base::TimeDelta::FromMilliseconds(none_ms
),
50 collector_
.unreported_cumulative_time(0)); // None.
51 EXPECT_EQ(base::TimeDelta::FromMilliseconds(moderate_ms
),
52 collector_
.unreported_cumulative_time(1)); // Moderate.
53 EXPECT_EQ(base::TimeDelta::FromMilliseconds(critical_ms
),
54 collector_
.unreported_cumulative_time(2)); // Critical.
57 // Validates expectations on the amount of reported time (seconds) per
59 void ExpectReported(int none_s
, int moderate_s
, int critical_s
) {
60 int total_s
= none_s
+ moderate_s
+ critical_s
;
62 // If the histogram should be empty then simply confirm that it doesn't
65 EXPECT_TRUE(histograms_
.GetTotalCountsForPrefix(kPressureLevel
).empty());
69 histograms_
.ExpectBucketCount(kPressureLevel
, 0, none_s
); // None.
70 histograms_
.ExpectBucketCount(kPressureLevel
, 1, moderate_s
); // Moderate.
71 histograms_
.ExpectBucketCount(kPressureLevel
, 2, critical_s
); // Critical.
72 histograms_
.ExpectTotalCount(kPressureLevel
, total_s
);
75 base::SimpleTestTickClock tick_clock_
;
76 TestMemoryPressureStatsCollector collector_
;
77 base::HistogramTester histograms_
;
80 TEST_F(MemoryPressureStatsCollectorTest
, EndToEnd
) {
81 // Upon construction no statistics should yet have been reported.
82 ExpectAccumulated(0, 0, 0);
83 ExpectReported(0, 0, 0);
84 histograms_
.ExpectTotalCount(kPressureLevelChange
, 0);
86 // A first call should not invoke any reporting functions, but it should
87 // modify member variables.
89 collector_
.UpdateStatistics(
90 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
);
91 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
,
92 collector_
.last_pressure_level());
93 EXPECT_EQ(tick_clock_
.NowTicks(), collector_
.last_update_time());
94 ExpectAccumulated(0, 0, 0);
95 ExpectReported(0, 0, 0);
96 histograms_
.ExpectTotalCount(kPressureLevelChange
, 0);
98 // A subsequent call with the same pressure level should increment the
99 // cumulative time but not make a report, as less than one second of time
100 // has been accumulated.
102 collector_
.UpdateStatistics(
103 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
);
104 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
,
105 collector_
.last_pressure_level());
106 EXPECT_EQ(tick_clock_
.NowTicks(), collector_
.last_update_time());
107 ExpectAccumulated(500, 0, 0);
108 ExpectReported(0, 0, 0);
109 histograms_
.ExpectTotalCount(kPressureLevelChange
, 0);
111 // Yet another call and this time a report should be made, as one second
112 // of time has been accumulated. 500ms should remain unreported.
114 collector_
.UpdateStatistics(
115 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
);
116 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
,
117 collector_
.last_pressure_level());
118 EXPECT_EQ(tick_clock_
.NowTicks(), collector_
.last_update_time());
119 ExpectAccumulated(500, 0, 0);
120 ExpectReported(1, 0, 0);
121 histograms_
.ExpectTotalCount(kPressureLevelChange
, 0);
123 // A subsequent call with a different pressure level should increment the
124 // cumulative time and make several reports.
126 collector_
.UpdateStatistics(
127 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE
);
128 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE
,
129 collector_
.last_pressure_level());
130 EXPECT_EQ(tick_clock_
.NowTicks(), collector_
.last_update_time());
131 ExpectAccumulated(500, 250, 0);
132 ExpectReported(1, 2, 0);
133 histograms_
.ExpectBucketCount(
134 kPressureLevelChange
, UMA_MEMORY_PRESSURE_LEVEL_CHANGE_NONE_TO_MODERATE
,
136 histograms_
.ExpectTotalCount(kPressureLevelChange
, 1);
139 } // namespace memory_pressure