OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/memory_pressure/memory_pressure_stats_collector.h" | |
6 | |
7 #include "base/test/histogram_tester.h" | |
8 #include "base/test/simple_test_tick_clock.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace memory_pressure { | |
12 | |
13 namespace { | |
14 | |
15 // Histogram names. | |
16 const char kPressureLevel[] = "Memory.PressureLevel"; | |
17 | |
18 } // namespace | |
19 | |
20 // Test version of the stats collector with a few extra accessors. | |
21 class TestMemoryPressureStatsCollector : public MemoryPressureStatsCollector { | |
22 public: | |
23 TestMemoryPressureStatsCollector(base::TickClock* tick_clock) | |
24 : MemoryPressureStatsCollector(tick_clock) {} | |
25 | |
26 // Accessors. | |
27 base::TimeDelta unreported_cumulative_time(int i) const { | |
28 return unreported_cumulative_time_[i]; | |
29 } | |
30 MemoryPressureLevel last_pressure_level() const { | |
31 return last_pressure_level_; | |
32 } | |
33 base::TimeTicks last_update_time() const { return last_update_time_; } | |
34 }; | |
35 | |
36 // Test fixture. | |
37 class MemoryPressureStatsCollectorTest : public testing::Test { | |
38 public: | |
39 MemoryPressureStatsCollectorTest() : collector_(&tick_clock_) {} | |
40 | |
41 void Tick(int ms) { | |
42 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(ms)); | |
43 } | |
44 | |
45 // Validates expectations on the amount of accumulated (and unreported) | |
46 // time (milliseconds) per pressure level. | |
47 void ExpectAccumulated(int none_ms, int moderate_ms, int critical_ms) { | |
48 EXPECT_EQ(base::TimeDelta::FromMilliseconds(none_ms), | |
49 collector_.unreported_cumulative_time(0)); // None. | |
50 EXPECT_EQ(base::TimeDelta::FromMilliseconds(moderate_ms), | |
51 collector_.unreported_cumulative_time(1)); // Moderate. | |
52 EXPECT_EQ(base::TimeDelta::FromMilliseconds(critical_ms), | |
53 collector_.unreported_cumulative_time(2)); // Critical. | |
54 } | |
55 | |
56 // Validates expectations on the amount of reported time (seconds) per | |
57 // pressure level. | |
58 void ExpectReported(int none_s, int moderate_s, int critical_s) { | |
59 int total_s = none_s + moderate_s + critical_s; | |
60 | |
61 // If the histogram should be empty then simply confirm that it doesn't | |
62 // yet exist. | |
63 if (total_s == 0) { | |
64 EXPECT_TRUE(histograms_.GetTotalCountsForPrefix(kPressureLevel).empty()); | |
65 return; | |
66 } | |
67 | |
68 histograms_.ExpectBucketCount(kPressureLevel, 0, none_s); // None. | |
69 histograms_.ExpectBucketCount(kPressureLevel, 1, moderate_s); // Moderate. | |
70 histograms_.ExpectBucketCount(kPressureLevel, 2, critical_s); // Critical. | |
71 histograms_.ExpectTotalCount(kPressureLevel, total_s); | |
72 } | |
73 | |
74 base::SimpleTestTickClock tick_clock_; | |
75 TestMemoryPressureStatsCollector collector_; | |
76 base::HistogramTester histograms_; | |
77 }; | |
78 | |
79 TEST_F(MemoryPressureStatsCollectorTest, EndToEnd) { | |
80 // Upon construction no statistics should yet have been reported. | |
81 ExpectAccumulated(0, 0, 0); | |
82 ExpectReported(0, 0, 0); | |
83 | |
84 // A first call should not invoke any reporting functions, but it should | |
85 // modify member variables. | |
86 Tick(500); | |
87 collector_.UpdateStatistics( | |
88 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); | |
89 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | |
90 collector_.last_pressure_level()); | |
91 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); | |
92 ExpectAccumulated(0, 0, 0); | |
93 ExpectReported(0, 0, 0); | |
94 | |
95 // A subsequent call with the same pressure level should increment the | |
96 // cumulative time but not make a report, as less than one second of time | |
97 // has been accumulated. | |
98 Tick(500); | |
99 collector_.UpdateStatistics( | |
100 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); | |
101 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | |
102 collector_.last_pressure_level()); | |
103 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); | |
104 ExpectAccumulated(500, 0, 0); | |
105 ExpectReported(0, 0, 0); | |
106 | |
107 // Yet another call and this time a report should be made, as one second | |
108 // of time has been accumulated. 500ms should remain unreported. | |
109 Tick(1000); | |
110 collector_.UpdateStatistics( | |
111 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); | |
112 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | |
113 collector_.last_pressure_level()); | |
114 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); | |
115 ExpectAccumulated(500, 0, 0); | |
116 ExpectReported(1, 0, 0); | |
117 | |
118 // A subsequent call with a different pressure level should increment the | |
119 // cumulative time and make several reports. | |
120 Tick(2250); | |
121 collector_.UpdateStatistics( | |
122 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | |
123 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | |
124 collector_.last_pressure_level()); | |
125 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); | |
126 ExpectAccumulated(500, 250, 0); | |
127 ExpectReported(1, 2, 0); | |
128 } | |
129 | |
130 } // namespace memory_pressure | |
OLD | NEW |