1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/memory_pressure/memory_pressure_level_reporter.h"
#include <memory>
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace memory_pressure {
TEST(MemoryPressureLevelReporterTest, PressureWindowDuration) {
base::test::SingleThreadTaskEnvironment task_environment(
base::test::TaskEnvironment::MainThreadType::IO,
base::test::TaskEnvironment::TimeSource::MOCK_TIME);
MemoryPressureLevelReporter reporter(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
base::HistogramTester histogram_tester;
// Moderate -> None.
task_environment.AdvanceClock(base::Seconds(12));
reporter.OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
histogram_tester.ExpectTimeBucketCount(
"Memory.PressureWindowDuration.ModerateToNone", base::Seconds(12), 1);
// Moderate -> Critical.
reporter.OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
task_environment.AdvanceClock(base::Seconds(20));
reporter.OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
histogram_tester.ExpectTimeBucketCount(
"Memory.PressureWindowDuration.ModerateToCritical", base::Seconds(20), 1);
// Critical -> None
task_environment.AdvanceClock(base::Seconds(25));
reporter.OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
histogram_tester.ExpectTimeBucketCount(
"Memory.PressureWindowDuration.CriticalToNone", base::Seconds(25), 1);
// Critical -> Moderate
reporter.OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
task_environment.AdvanceClock(base::Seconds(27));
reporter.OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
histogram_tester.ExpectTimeBucketCount(
"Memory.PressureWindowDuration.CriticalToModerate", base::Seconds(27), 1);
}
TEST(MemoryPressureLevelReporterTest, MemoryPressureHistogram) {
base::test::SingleThreadTaskEnvironment task_environment(
base::test::TaskEnvironment::MainThreadType::IO,
base::test::TaskEnvironment::TimeSource::MOCK_TIME);
std::unique_ptr<MemoryPressureLevelReporter> reporter =
std::make_unique<MemoryPressureLevelReporter>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
base::HistogramTester histogram_tester;
constexpr base::TimeDelta kDelay = base::Seconds(12);
const char* kHistogram = "Memory.PressureLevel2";
// None -> Moderate.
task_environment.AdvanceClock(kDelay);
reporter->OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
// There one report for a |kdelay| MEMORY_PRESSURE_LEVEL_NONE session.
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
kDelay.InSeconds());
task_environment.AdvanceClock(kDelay);
reporter->OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
// There one report for a |kdelay| MEMORY_PRESSURE_LEVEL_MODERATE session.
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE),
kDelay.InSeconds());
task_environment.AdvanceClock(kDelay);
reporter->OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
// There's now two reports for a |kdelay| MEMORY_PRESSURE_LEVEL_NONE session,
// for a total of |2*kdelay|.
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
(2 * kDelay).InSeconds());
task_environment.AdvanceClock(kDelay);
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL),
0);
reporter.reset();
// Releasing the reporter should report the data from the current pressure
// session.
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL),
kDelay.InSeconds());
}
TEST(MemoryPressureLevelReporterTest, MemoryPressureHistogramAccumulatedTime) {
base::test::SingleThreadTaskEnvironment task_environment(
base::test::TaskEnvironment::MainThreadType::IO,
base::test::TaskEnvironment::TimeSource::MOCK_TIME);
MemoryPressureLevelReporter reporter(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
base::HistogramTester histogram_tester;
const char* kHistogram = "Memory.PressureLevel2";
constexpr base::TimeDelta kHalfASecond = base::Milliseconds(500);
task_environment.AdvanceClock(kHalfASecond);
reporter.OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
// The delay is inferior to one second, there should be no data reported.
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
0);
reporter.OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
task_environment.AdvanceClock(kHalfASecond);
reporter.OnMemoryPressureLevelChanged(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
// The delay is inferior to one second, there should be no data reported.
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
1);
}
TEST(MemoryPressureLevelReporterTest,
MemoryPressureHistogramPeriodicReporting) {
base::test::SingleThreadTaskEnvironment task_environment(
base::test::TaskEnvironment::MainThreadType::IO,
base::test::TaskEnvironment::TimeSource::MOCK_TIME);
MemoryPressureLevelReporter reporter(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
base::HistogramTester histogram_tester;
const char* kHistogram = "Memory.PressureLevel2";
// Advancing the clock by a few seconds shouldn't cause any periodic
// reporting.
task_environment.FastForwardBy(base::Seconds(10));
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
0);
// Advancing the clock by a few minutes should cause periodic reporting.
task_environment.FastForwardBy(base::Minutes(5));
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
5 * 60 /* 5 minutes */);
task_environment.FastForwardBy(base::Minutes(5));
histogram_tester.ExpectBucketCount(
kHistogram,
static_cast<int>(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
2 * 5 * 60 /* 2 x 5 minutes */);
}
} // namespace memory_pressure
|