File: highest_pmf_reporter_test.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (294 lines) | stat: -rw-r--r-- 10,749 bytes parent folder | download | duplicates (5)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif

#include "third_party/blink/renderer/controller/highest_pmf_reporter.h"

#include <memory>

#include "base/memory/ptr_util.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/core/loader/empty_clients.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/platform/scheduler/public/main_thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/main_thread_scheduler.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"

namespace blink {

class MockHighestPmfReporter : public HighestPmfReporter {
 public:
  MockHighestPmfReporter(
      scoped_refptr<base::TestMockTimeTaskRunner> task_runner_for_testing,
      const base::TickClock* clock)
      : HighestPmfReporter(task_runner_for_testing, clock) {}
  ~MockHighestPmfReporter() override = default;

  void NotifyNavigationStart() { first_navigation_started_ = true; }

  const std::vector<double>& GetReportedHighestPmf() const {
    return reported_highest_pmf_;
  }

  const std::vector<double>& GetReportedPeakRss() const {
    return reported_peak_rss_;
  }

  const std::vector<unsigned>& GetReportedWebpageCount() const {
    return reported_webpage_count_;
  }

  int GetReportCount() const { return report_count_; }

 private:
  void ReportMetrics() override {
    reported_highest_pmf_.push_back(current_highest_pmf_);
    reported_peak_rss_.push_back(peak_resident_bytes_at_current_highest_pmf_);
    reported_webpage_count_.push_back(webpage_counts_at_current_highest_pmf_);
  }

  bool FirstNavigationStarted() override {
    if (!first_navigation_started_)
      return false;

    first_navigation_started_ = false;
    return true;
  }

  std::vector<double> reported_highest_pmf_;
  std::vector<double> reported_peak_rss_;
  std::vector<unsigned> reported_webpage_count_;
  bool first_navigation_started_ = false;
};

namespace peak_memory_reporter_test {

using testing::_;

// Mock that allows setting mock memory usage.
class MockMemoryUsageMonitor : public MemoryUsageMonitor {
 public:
  MockMemoryUsageMonitor(
      scoped_refptr<base::TestMockTimeTaskRunner> task_runner_for_testing,
      const base::TickClock* clock)
      : MemoryUsageMonitor(task_runner_for_testing, clock),
        agent_group_scheduler_(Thread::MainThread()
                                   ->Scheduler()
                                   ->ToMainThreadScheduler()
                                   ->CreateAgentGroupScheduler()) {
    memset(&mock_memory_usage_, 0, sizeof(mock_memory_usage_));
  }
  ~MockMemoryUsageMonitor() override = default;

  MemoryUsage GetCurrentMemoryUsage() override { return mock_memory_usage_; }

  void SetPrivateFootprintBytes(double private_footprint_bytes) {
    mock_memory_usage_.private_footprint_bytes = private_footprint_bytes;
  }

  void SetPeakResidentBytes(double peak_resident_bytes) {
    mock_memory_usage_.peak_resident_bytes = peak_resident_bytes;
  }

  // Insert fake NonOrdinaryPage into Page::OrdinaryPages().
  void SetOrdinaryPageCount(unsigned page_count) {
    DCHECK_GT(page_count, 0U);
    while (dummy_pages_.size() < page_count) {
      Page* page = CreateDummyPage();
      DCHECK(page);
      dummy_pages_.push_back(page);
    }
    if (Page::OrdinaryPages().size() > 1U) {
      for (unsigned i = 0; i < dummy_pages_.size(); i++) {
        if (Page::OrdinaryPages().Contains(dummy_pages_.at(i).Get()))
          Page::OrdinaryPages().erase(dummy_pages_.at(i).Get());
      }
    }
    DCHECK_EQ(Page::OrdinaryPages().size(), 1U);

    std::vector<Persistent<Page>>::iterator it = dummy_pages_.begin();
    while (Page::OrdinaryPages().size() < page_count) {
      CHECK(it != dummy_pages_.end());
      Page::OrdinaryPages().insert(it->Get());
      it++;
    }
  }

 private:
  MockMemoryUsageMonitor() = delete;

  Page* CreateDummyPage() {
    return Page::CreateNonOrdinary(*MakeGarbageCollected<EmptyChromeClient>(),
                                   *agent_group_scheduler_,
                                   /*color_provider_colors=*/nullptr);
  }

  MemoryUsage mock_memory_usage_;
  std::vector<Persistent<Page>> dummy_pages_;
  Persistent<AgentGroupScheduler> agent_group_scheduler_;
};

class HighestPmfReporterTest : public PageTestBase {
 public:
  HighestPmfReporterTest() = default;

  void SetUp() override {
    test_task_runner_ = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
    memory_usage_monitor_ = std::make_unique<MockMemoryUsageMonitor>(
        test_task_runner_, test_task_runner_->GetMockTickClock());
    MemoryUsageMonitor::SetInstanceForTesting(memory_usage_monitor_.get());
    reporter_ = std::make_unique<MockHighestPmfReporter>(
        test_task_runner_, test_task_runner_->GetMockTickClock());
    PageTestBase::SetUp();
  }

  void TearDown() override {
    PageTestBase::TearDown();
    MemoryUsageMonitor::SetInstanceForTesting(nullptr);
    memory_usage_monitor_.reset();
    reporter_.reset();
  }

  void AdvanceClock(base::TimeDelta delta) {
    test_task_runner_->FastForwardBy(delta);
  }

  void AdvanceClockTo(base::TimeTicks time) {
    base::TimeDelta delta = time - NowTicks();
    if (delta.is_zero())
      return;
    AdvanceClock(delta);
  }

  base::TimeTicks NowTicks() const {
    return test_task_runner_->GetMockTickClock()->NowTicks();
  }

 protected:
  scoped_refptr<base::TestMockTimeTaskRunner> test_task_runner_;
  std::unique_ptr<MockMemoryUsageMonitor> memory_usage_monitor_;
  std::unique_ptr<MockHighestPmfReporter> reporter_;
};

TEST_F(HighestPmfReporterTest, ReportNoMetricBeforeNavigationStart) {
  EXPECT_TRUE(memory_usage_monitor_->TimerIsActive());
  Page::OrdinaryPages().insert(&GetPage());

  memory_usage_monitor_->SetPrivateFootprintBytes(1000.0);
  AdvanceClock(base::Minutes(1));
  EXPECT_EQ(0, reporter_->GetReportCount());
  EXPECT_EQ(0U, reporter_->GetReportedHighestPmf().size());
  EXPECT_EQ(0U, reporter_->GetReportedPeakRss().size());
}

// TODO(https://crbug.com/1408949): This test fails on ASAN bots.
#if defined(ADDRESS_SANITIZER)
#define MAYBE_ReportMetric DISABLED_ReportMetric
#else
#define MAYBE_ReportMetric ReportMetric
#endif
TEST_F(HighestPmfReporterTest, MAYBE_ReportMetric) {
  EXPECT_TRUE(memory_usage_monitor_->TimerIsActive());
  Page::OrdinaryPages().insert(&GetPage());
  AdvanceClock(base::Seconds(1));

  // PMF, PeakRSS and PageCount at specified TimeSinceNavigation.
  static const struct {
    base::TimeDelta time_since_navigation;
    double pmf;
    double peak_rss;
    unsigned page_count;
  } time_pmf_rss_table[] = {
      {base::Minutes(0), 1000.0, 1200.0, 1},
      {base::Minutes(1), 750.0, 900.0, 1},
      {base::Seconds(80), 750.0, 1000.0, 4},   // t=1min 20sec
      {base::Seconds(90), 1100.0, 1500.0, 2},  // t=1min 30sec
      {base::Minutes(2), 900.0, 1000.0, 1},
      {base::Minutes(4), 900.0, 1000.0, 1},
      {base::Minutes(5), 1500.0, 2000.0, 3},
      {base::Minutes(7), 800.0, 900.0, 1},
      {base::Minutes(8), 900.0, 1000.0, 1},
      {base::Minutes(16), 900.0, 1000.0, 1},
  };

  base::TimeTicks navigation_start_time = NowTicks();
  reporter_->NotifyNavigationStart();

  for (const auto& item : time_pmf_rss_table) {
    AdvanceClockTo(navigation_start_time + item.time_since_navigation);
    // PMF, PeakRSS, Webpage count are captured at next OnMemoryPing.
    memory_usage_monitor_->SetPrivateFootprintBytes(item.pmf);
    memory_usage_monitor_->SetPeakResidentBytes(item.peak_rss);
    memory_usage_monitor_->SetOrdinaryPageCount(item.page_count);
  }
  AdvanceClockTo(navigation_start_time + base::Minutes(17));

  EXPECT_EQ(4, reporter_->GetReportCount());
  EXPECT_EQ(4U, reporter_->GetReportedHighestPmf().size());
  EXPECT_NEAR(1100.0, reporter_->GetReportedHighestPmf().at(0), 0.001);
  EXPECT_NEAR(900.0, reporter_->GetReportedHighestPmf().at(1), 0.001);
  EXPECT_NEAR(1500.0, reporter_->GetReportedHighestPmf().at(2), 0.001);
  EXPECT_NEAR(900.0, reporter_->GetReportedHighestPmf().at(3), 0.001);

  EXPECT_EQ(4U, reporter_->GetReportedPeakRss().size());
  EXPECT_NEAR(1500.0, reporter_->GetReportedPeakRss().at(0), 0.001);
  EXPECT_NEAR(1000.0, reporter_->GetReportedPeakRss().at(1), 0.001);
  EXPECT_NEAR(2000.0, reporter_->GetReportedPeakRss().at(2), 0.001);
  EXPECT_NEAR(1000.0, reporter_->GetReportedPeakRss().at(3), 0.001);

  EXPECT_EQ(4U, reporter_->GetReportedWebpageCount().size());
  EXPECT_EQ(2U, reporter_->GetReportedWebpageCount().at(0));
  EXPECT_EQ(1U, reporter_->GetReportedWebpageCount().at(1));
  EXPECT_EQ(3U, reporter_->GetReportedWebpageCount().at(2));
  EXPECT_EQ(1U, reporter_->GetReportedWebpageCount().at(3));
}

TEST_F(HighestPmfReporterTest, TestReportTiming) {
  EXPECT_TRUE(memory_usage_monitor_->TimerIsActive());
  Page::OrdinaryPages().insert(&GetPage());

  memory_usage_monitor_->SetPrivateFootprintBytes(1000.0);

  base::TimeTicks navigation_start_time = NowTicks();
  reporter_->NotifyNavigationStart();
  AdvanceClock(base::Seconds(1));
  // Now ReportMetrics task is posted with 2minutes delay.
  // The task will be executed at "navigation_start_time + 2min + 1sec."

  EXPECT_EQ(0, reporter_->GetReportCount());
  AdvanceClockTo(navigation_start_time + base::Minutes(2));
  EXPECT_EQ(0, reporter_->GetReportCount());
  // ReportMetrics task is executed and next ReportMetrics task is posted.
  AdvanceClock(base::Seconds(1));
  EXPECT_EQ(1, reporter_->GetReportCount());

  AdvanceClockTo(navigation_start_time + base::Minutes(4));
  EXPECT_EQ(1, reporter_->GetReportCount());
  // ReportMetrics task is executed and next ReportMetrics task is posted.
  AdvanceClock(base::Seconds(1));
  EXPECT_EQ(2, reporter_->GetReportCount());

  AdvanceClockTo(navigation_start_time + base::Minutes(8));
  EXPECT_EQ(2, reporter_->GetReportCount());
  // ReportMetrics task is executed and next ReportMetrics task is posted.
  AdvanceClock(base::Seconds(1));
  EXPECT_EQ(3, reporter_->GetReportCount());

  AdvanceClockTo(navigation_start_time + base::Minutes(16));
  EXPECT_EQ(3, reporter_->GetReportCount());
  // ReportMetrics task is executed and next ReportMetrics task is posted.
  AdvanceClock(base::Seconds(1));
  EXPECT_EQ(4, reporter_->GetReportCount());
}

}  // namespace peak_memory_reporter_test
}  // namespace blink