File: process_performance_scenarios_unittest.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 (272 lines) | stat: -rw-r--r-- 12,055 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
// Copyright 2025 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/performance_manager/public/scenarios/process_performance_scenarios.h"

#include <optional>

#include "base/containers/span.h"
#include "base/memory/platform_shared_memory_handle.h"
#include "base/memory/shared_memory_mapper.h"
#include "base/scoped_observation.h"
#include "components/performance_manager/embedder/scoped_global_scenario_memory.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/scenario_api/performance_scenario_observer.h"
#include "components/performance_manager/scenario_api/performance_scenarios.h"
#include "components/performance_manager/scenarios/browser_performance_scenarios.h"
#include "components/performance_manager/scenarios/performance_scenario_data.h"
#include "components/performance_manager/test_support/graph_test_harness.h"
#include "components/performance_manager/test_support/mock_graphs.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace performance_manager {

namespace {

using performance_scenarios::InputScenario;
using performance_scenarios::LoadingScenario;
using performance_scenarios::MatchingScenarioObserver;
using performance_scenarios::PerformanceScenarioObserver;
using performance_scenarios::ScenarioScope;

class LenientMockPerformanceScenarioObserver
    : public PerformanceScenarioObserver {
 public:
  MOCK_METHOD(void,
              OnLoadingScenarioChanged,
              (ScenarioScope scope,
               LoadingScenario old_scenario,
               LoadingScenario new_scenario),
              (override));
  MOCK_METHOD(void,
              OnInputScenarioChanged,
              (ScenarioScope scope,
               InputScenario old_scenario,
               InputScenario new_scenario),
              (override));
};

using MockPerformanceScenarioObserver =
    ::testing::StrictMock<LenientMockPerformanceScenarioObserver>;

class LenientMockMatchingScenarioObserver : public MatchingScenarioObserver {
 public:
  explicit LenientMockMatchingScenarioObserver(
      performance_scenarios::ScenarioPattern pattern)
      : MatchingScenarioObserver(pattern) {}

  MOCK_METHOD(void,
              OnScenarioMatchChanged,
              (ScenarioScope scope, bool matches_pattern),
              (override));
};

using MockMatchingScenarioObserver =
    ::testing::StrictMock<LenientMockMatchingScenarioObserver>;

// A fake SharedMemoryMapper that fails to map in memory.
class FailingSharedMemoryMapper final : public base::SharedMemoryMapper {
 public:
  FailingSharedMemoryMapper() = default;
  ~FailingSharedMemoryMapper() = default;

  std::optional<base::span<uint8_t>> Map(
      base::subtle::PlatformSharedMemoryHandle handle,
      bool write_allowed,
      uint64_t offset,
      size_t size) final {
    return std::nullopt;
  }

  void Unmap(base::span<uint8_t> mapping) final {}
};

class ProcessPerformanceScenariosTest : public GraphTestHarness {
 private:
  // Always map global memory. Per-process memory will be mapped automatically
  // as process nodes are created.
  ScopedGlobalScenarioMemory scoped_global_scenario_memory_;
};

}  // namespace

TEST_F(ProcessPerformanceScenariosTest, LoadingScenario) {
  MockSinglePageWithMultipleProcessesGraph mock_graph(graph());

  EXPECT_EQ(GetProcessLoadingScenario(mock_graph.process.get()),
            LoadingScenario::kNoPageLoading);
  EXPECT_EQ(GetProcessLoadingScenario(mock_graph.other_process.get()),
            LoadingScenario::kNoPageLoading);

  MockPerformanceScenarioObserver mock_observer;
  base::ScopedObservation<ProcessNode, PerformanceScenarioObserver> observation(
      &mock_observer);
  observation.Observe(mock_graph.process.get());

  MockMatchingScenarioObserver mock_matching_observer(
      performance_scenarios::kDefaultIdleScenarios);
  base::ScopedObservation<ProcessNode, MatchingScenarioObserver>
      matching_observation(&mock_matching_observer);
  matching_observation.Observe(mock_graph.process.get());

  // Changing to kBackgroundPageLoading should invoke the
  // PerformanceScenarioObserver but not the MatchingScenarioObserver, since it
  // still matches kDefaultIdleScenarios.
  EXPECT_CALL(mock_observer, OnLoadingScenarioChanged(
                                 ScenarioScope::kCurrentProcess,
                                 LoadingScenario::kNoPageLoading,
                                 LoadingScenario::kBackgroundPageLoading));

  SetLoadingScenarioForProcessNode(LoadingScenario::kBackgroundPageLoading,
                                   mock_graph.process.get());
  EXPECT_EQ(GetProcessLoadingScenario(mock_graph.process.get()),
            LoadingScenario::kBackgroundPageLoading);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
  ::testing::Mock::VerifyAndClearExpectations(&mock_matching_observer);

  // Observers shouldn't be notified if the scenario doesn't change.
  SetLoadingScenarioForProcessNode(LoadingScenario::kBackgroundPageLoading,
                                   mock_graph.process.get());
  EXPECT_EQ(GetProcessLoadingScenario(mock_graph.process.get()),
            LoadingScenario::kBackgroundPageLoading);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
  ::testing::Mock::VerifyAndClearExpectations(&mock_matching_observer);

  // Observer shouldn't be notified for changes to a different process, or for
  // global changes.
  SetLoadingScenarioForProcessNode(LoadingScenario::kVisiblePageLoading,
                                   mock_graph.other_process.get());
  SetGlobalLoadingScenario(LoadingScenario::kFocusedPageLoading);
  EXPECT_EQ(GetProcessLoadingScenario(mock_graph.other_process.get()),
            LoadingScenario::kVisiblePageLoading);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
  ::testing::Mock::VerifyAndClearExpectations(&mock_matching_observer);

  // Changing to kVisiblePageLoading should notify both observers since the
  // scenario no longer matches kDefaultIdleScenarios.
  EXPECT_CALL(mock_observer,
              OnLoadingScenarioChanged(ScenarioScope::kCurrentProcess,
                                       LoadingScenario::kBackgroundPageLoading,
                                       LoadingScenario::kVisiblePageLoading));
  EXPECT_CALL(mock_matching_observer,
              OnScenarioMatchChanged(ScenarioScope::kCurrentProcess, false));

  SetLoadingScenarioForProcessNode(LoadingScenario::kVisiblePageLoading,
                                   mock_graph.process.get());
  EXPECT_EQ(GetProcessLoadingScenario(mock_graph.process.get()),
            LoadingScenario::kVisiblePageLoading);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
  ::testing::Mock::VerifyAndClearExpectations(&mock_matching_observer);
}

TEST_F(ProcessPerformanceScenariosTest, InputScenario) {
  MockSinglePageWithMultipleProcessesGraph mock_graph(graph());

  EXPECT_EQ(GetProcessInputScenario(mock_graph.process.get()),
            InputScenario::kNoInput);
  EXPECT_EQ(GetProcessInputScenario(mock_graph.other_process.get()),
            InputScenario::kNoInput);

  MockPerformanceScenarioObserver mock_observer;
  base::ScopedObservation<ProcessNode, PerformanceScenarioObserver> observation(
      &mock_observer);
  observation.Observe(mock_graph.process.get());

  MockMatchingScenarioObserver mock_matching_observer(
      performance_scenarios::kDefaultIdleScenarios);
  base::ScopedObservation<ProcessNode, MatchingScenarioObserver>
      matching_observation(&mock_matching_observer);
  matching_observation.Observe(mock_graph.process.get());

  // Changing to kTyping should notify both observers since the scenario no
  // longer matches kDefaultIdleScenarios.
  EXPECT_CALL(
      mock_observer,
      OnInputScenarioChanged(ScenarioScope::kCurrentProcess,
                             InputScenario::kNoInput, InputScenario::kTyping));
  EXPECT_CALL(mock_matching_observer,
              OnScenarioMatchChanged(ScenarioScope::kCurrentProcess, false));

  SetInputScenarioForProcessNode(InputScenario::kTyping,
                                 mock_graph.process.get());
  EXPECT_EQ(GetProcessInputScenario(mock_graph.process.get()),
            InputScenario::kTyping);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
  ::testing::Mock::VerifyAndClearExpectations(&mock_matching_observer);

  // Observers shouldn't be notified if the scenario doesn't change.
  SetInputScenarioForProcessNode(InputScenario::kTyping,
                                 mock_graph.process.get());
  EXPECT_EQ(GetProcessInputScenario(mock_graph.process.get()),
            InputScenario::kTyping);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
  ::testing::Mock::VerifyAndClearExpectations(&mock_matching_observer);

  // Observer shouldn't be notified for changes to a different process, or for
  // global changes.
  SetInputScenarioForProcessNode(InputScenario::kTap,
                                 mock_graph.other_process.get());
  SetGlobalInputScenario(InputScenario::kScroll);
  EXPECT_EQ(GetProcessInputScenario(mock_graph.other_process.get()),
            InputScenario::kTap);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
  ::testing::Mock::VerifyAndClearExpectations(&mock_matching_observer);

  // Changing to kTap should invoke the PerformanceScenarioObserver but not the
  // MatchingScenarioObserver, since it still doesn't match
  // kDefaultIdleScenarios.
  EXPECT_CALL(mock_observer, OnInputScenarioChanged(
                                 ScenarioScope::kCurrentProcess,
                                 InputScenario::kTyping, InputScenario::kTap));

  SetInputScenarioForProcessNode(InputScenario::kTap, mock_graph.process.get());
  EXPECT_EQ(GetProcessInputScenario(mock_graph.process.get()),
            InputScenario::kTap);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
  ::testing::Mock::VerifyAndClearExpectations(&mock_matching_observer);
}

TEST_F(ProcessPerformanceScenariosTest, NoMemory) {
  MockSinglePageInSingleProcessGraph mock_graph(graph());

  // First access to the PerformanceScenarioData uses a failing mapper.
  FailingSharedMemoryMapper failing_mapper;
  EXPECT_FALSE(PerformanceScenarioData::Exists(mock_graph.process.get()));
  auto& data = PerformanceScenarioData::GetOrCreate(mock_graph.process.get(),
                                                    &failing_mapper);
  EXPECT_FALSE(data.HasSharedState());

  // Reading the process scenario should return default values when memory
  // mapping failed.
  EXPECT_EQ(GetProcessLoadingScenario(mock_graph.process.get()),
            LoadingScenario::kNoPageLoading);
  EXPECT_EQ(GetProcessInputScenario(mock_graph.process.get()),
            InputScenario::kNoInput);

  // Writing to process state should do nothing. Observers should not be
  // notified.
  MockPerformanceScenarioObserver mock_observer;
  base::ScopedObservation<ProcessNode, PerformanceScenarioObserver> observation(
      &mock_observer);
  observation.Observe(mock_graph.process.get());

  MockMatchingScenarioObserver mock_matching_observer(
      performance_scenarios::kDefaultIdleScenarios);
  base::ScopedObservation<ProcessNode, MatchingScenarioObserver>
      matching_observation(&mock_matching_observer);
  matching_observation.Observe(mock_graph.process.get());

  SetLoadingScenarioForProcessNode(LoadingScenario::kVisiblePageLoading,
                                   mock_graph.process.get());
  SetInputScenarioForProcessNode(InputScenario::kTyping,
                                 mock_graph.process.get());
  EXPECT_EQ(GetProcessLoadingScenario(mock_graph.process.get()),
            LoadingScenario::kNoPageLoading);
  EXPECT_EQ(GetProcessInputScenario(mock_graph.process.get()),
            InputScenario::kNoInput);
}

}  // namespace performance_manager