memory_pressure_level_reporter_unittest.cc 7.2 KB

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