Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: components/memory_pressure/memory_pressure_stats_collector_unittest.cc

Issue 1310043004: Create MemoryPressureStatsCollector. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed rkaplow's comments. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // This is outside of the anonymous namespace as it is a friend to the class
14 // under test.
15 class TestMemoryPressureStatsCollector : public MemoryPressureStatsCollector {
16 public:
17 TestMemoryPressureStatsCollector(base::TickClock* tick_clock)
18 : MemoryPressureStatsCollector(tick_clock) {}
19
20 // Accessors.
21 base::TimeDelta unreported_cumulative_time(int i) const {
22 return unreported_cumulative_time_[i];
23 }
24 MemoryPressureLevel last_pressure_level() const {
25 return last_pressure_level_;
26 }
27 base::TimeTicks last_update_time() const { return last_update_time_; }
28 };
29
30 namespace {
31
32 // Histogram names.
33 static const char kPressureLevel[] = "Memory.PressureLevel";
grt (UTC plus 2) 2015/09/08 17:22:46 nit: no "static" for constants in the unnamed name
chrisha 2015/09/08 18:48:43 Done.
34 static const char kPressureLevelChange[] = "Memory.PressureLevelChange";
35
36 // Test fixture.
37 class MemoryPressureStatsCollectorTest : public testing::Test {
grt (UTC plus 2) 2015/09/08 17:22:47 i don't see an advantage to having this in an unna
chrisha 2015/09/08 18:48:43 No compelling reason, just habit. Done.
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 TestMemoryPressureStatsCollector collector_;
75 base::SimpleTestTickClock tick_clock_;
grt (UTC plus 2) 2015/09/08 17:22:46 since |tick_clock_| is passed to |collector_|'s ct
chrisha 2015/09/08 18:48:43 Done.
76 base::HistogramTester histograms_;
77 };
78
79 } // namespace
80
81 TEST_F(MemoryPressureStatsCollectorTest, EndToEnd) {
82 // Upon construction the last pressure level should be invalid.
grt (UTC plus 2) 2015/09/08 17:22:46 this seems to be testing an implementation detail.
chrisha 2015/09/08 18:48:43 Good point.
83 EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
84 collector_.last_pressure_level());
85 EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
86 collector_.last_pressure_level());
87 EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
88 collector_.last_pressure_level());
89 ExpectAccumulated(0, 0, 0);
90 ExpectReported(0, 0, 0);
91
92 // A first call should not invoke any reporting functions, but it should
93 // modify member variables.
94 Tick(500);
95 collector_.UpdateStatistics(
96 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
97 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
98 collector_.last_pressure_level());
99 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time());
100 ExpectAccumulated(0, 0, 0);
101 ExpectReported(0, 0, 0);
102 histograms_.ExpectTotalCount(kPressureLevelChange, 0);
103
104 // A subsequent call with the same pressure level should increment the
105 // cumulative time but not make a report, as less than one second of time
106 // has been accumulated.
107 Tick(500);
108 collector_.UpdateStatistics(
109 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
110 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
111 collector_.last_pressure_level());
112 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time());
113 ExpectAccumulated(500, 0, 0);
114 ExpectReported(0, 0, 0);
115 histograms_.ExpectTotalCount(kPressureLevelChange, 0);
116
117 // Yet another call and this time a report should be made, as one second
118 // of time has been accumulated. 500ms should remain unreported.
119 Tick(1000);
120 collector_.UpdateStatistics(
121 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
122 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
123 collector_.last_pressure_level());
124 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time());
125 ExpectAccumulated(500, 0, 0);
126 ExpectReported(1, 0, 0);
127 histograms_.ExpectTotalCount(kPressureLevelChange, 0);
128
129 // A subsequent call with a different pressure level should increment the
130 // cumulative time and make several reports.
131 Tick(2250);
132 collector_.UpdateStatistics(
133 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
134 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
135 collector_.last_pressure_level());
136 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time());
137 ExpectAccumulated(500, 250, 0);
138 ExpectReported(1, 2, 0);
139 histograms_.ExpectBucketCount(kPressureLevelChange,
140 UMA_MEMORY_PRESSURE_LEVEL_CHANGE_NONE_TO_MODERATE, 1);
141 histograms_.ExpectTotalCount(kPressureLevelChange, 1);
142 }
143
144 } // namespace memory_pressure
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698