| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/memory_pressure/memory_pressure_stats_collector.h" | 5 #include "components/memory_pressure/memory_pressure_stats_collector.h" |
| 6 | 6 |
| 7 #include "base/test/histogram_tester.h" | 7 #include "base/test/histogram_tester.h" |
| 8 #include "base/test/simple_test_tick_clock.h" | 8 #include "base/test/simple_test_tick_clock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace memory_pressure { | 11 namespace memory_pressure { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // Histogram names. | 15 // Histogram names. |
| 16 const char kPressureLevel[] = "Memory.PressureLevel"; | 16 const char kPressureLevel[] = "Memory.PressureLevel"; |
| 17 const char kPressureLevelChange[] = "Memory.PressureLevelChange"; | |
| 18 | 17 |
| 19 } // namespace | 18 } // namespace |
| 20 | 19 |
| 21 // Test version of the stats collector with a few extra accessors. | 20 // Test version of the stats collector with a few extra accessors. |
| 22 class TestMemoryPressureStatsCollector : public MemoryPressureStatsCollector { | 21 class TestMemoryPressureStatsCollector : public MemoryPressureStatsCollector { |
| 23 public: | 22 public: |
| 24 TestMemoryPressureStatsCollector(base::TickClock* tick_clock) | 23 TestMemoryPressureStatsCollector(base::TickClock* tick_clock) |
| 25 : MemoryPressureStatsCollector(tick_clock) {} | 24 : MemoryPressureStatsCollector(tick_clock) {} |
| 26 | 25 |
| 27 // Accessors. | 26 // Accessors. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 73 |
| 75 base::SimpleTestTickClock tick_clock_; | 74 base::SimpleTestTickClock tick_clock_; |
| 76 TestMemoryPressureStatsCollector collector_; | 75 TestMemoryPressureStatsCollector collector_; |
| 77 base::HistogramTester histograms_; | 76 base::HistogramTester histograms_; |
| 78 }; | 77 }; |
| 79 | 78 |
| 80 TEST_F(MemoryPressureStatsCollectorTest, EndToEnd) { | 79 TEST_F(MemoryPressureStatsCollectorTest, EndToEnd) { |
| 81 // Upon construction no statistics should yet have been reported. | 80 // Upon construction no statistics should yet have been reported. |
| 82 ExpectAccumulated(0, 0, 0); | 81 ExpectAccumulated(0, 0, 0); |
| 83 ExpectReported(0, 0, 0); | 82 ExpectReported(0, 0, 0); |
| 84 histograms_.ExpectTotalCount(kPressureLevelChange, 0); | |
| 85 | 83 |
| 86 // A first call should not invoke any reporting functions, but it should | 84 // A first call should not invoke any reporting functions, but it should |
| 87 // modify member variables. | 85 // modify member variables. |
| 88 Tick(500); | 86 Tick(500); |
| 89 collector_.UpdateStatistics( | 87 collector_.UpdateStatistics( |
| 90 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); | 88 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); |
| 91 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | 89 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
| 92 collector_.last_pressure_level()); | 90 collector_.last_pressure_level()); |
| 93 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); | 91 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); |
| 94 ExpectAccumulated(0, 0, 0); | 92 ExpectAccumulated(0, 0, 0); |
| 95 ExpectReported(0, 0, 0); | 93 ExpectReported(0, 0, 0); |
| 96 histograms_.ExpectTotalCount(kPressureLevelChange, 0); | |
| 97 | 94 |
| 98 // A subsequent call with the same pressure level should increment the | 95 // 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 | 96 // cumulative time but not make a report, as less than one second of time |
| 100 // has been accumulated. | 97 // has been accumulated. |
| 101 Tick(500); | 98 Tick(500); |
| 102 collector_.UpdateStatistics( | 99 collector_.UpdateStatistics( |
| 103 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); | 100 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); |
| 104 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | 101 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
| 105 collector_.last_pressure_level()); | 102 collector_.last_pressure_level()); |
| 106 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); | 103 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); |
| 107 ExpectAccumulated(500, 0, 0); | 104 ExpectAccumulated(500, 0, 0); |
| 108 ExpectReported(0, 0, 0); | 105 ExpectReported(0, 0, 0); |
| 109 histograms_.ExpectTotalCount(kPressureLevelChange, 0); | |
| 110 | 106 |
| 111 // Yet another call and this time a report should be made, as one second | 107 // Yet another call and this time a report should be made, as one second |
| 112 // of time has been accumulated. 500ms should remain unreported. | 108 // of time has been accumulated. 500ms should remain unreported. |
| 113 Tick(1000); | 109 Tick(1000); |
| 114 collector_.UpdateStatistics( | 110 collector_.UpdateStatistics( |
| 115 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); | 111 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); |
| 116 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | 112 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
| 117 collector_.last_pressure_level()); | 113 collector_.last_pressure_level()); |
| 118 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); | 114 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); |
| 119 ExpectAccumulated(500, 0, 0); | 115 ExpectAccumulated(500, 0, 0); |
| 120 ExpectReported(1, 0, 0); | 116 ExpectReported(1, 0, 0); |
| 121 histograms_.ExpectTotalCount(kPressureLevelChange, 0); | |
| 122 | 117 |
| 123 // A subsequent call with a different pressure level should increment the | 118 // A subsequent call with a different pressure level should increment the |
| 124 // cumulative time and make several reports. | 119 // cumulative time and make several reports. |
| 125 Tick(2250); | 120 Tick(2250); |
| 126 collector_.UpdateStatistics( | 121 collector_.UpdateStatistics( |
| 127 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | 122 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 128 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 123 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
| 129 collector_.last_pressure_level()); | 124 collector_.last_pressure_level()); |
| 130 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); | 125 EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time()); |
| 131 ExpectAccumulated(500, 250, 0); | 126 ExpectAccumulated(500, 250, 0); |
| 132 ExpectReported(1, 2, 0); | 127 ExpectReported(1, 2, 0); |
| 133 histograms_.ExpectBucketCount( | |
| 134 kPressureLevelChange, UMA_MEMORY_PRESSURE_LEVEL_CHANGE_NONE_TO_MODERATE, | |
| 135 1); | |
| 136 histograms_.ExpectTotalCount(kPressureLevelChange, 1); | |
| 137 } | 128 } |
| 138 | 129 |
| 139 } // namespace memory_pressure | 130 } // namespace memory_pressure |
| OLD | NEW |