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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
// Copyright 2024 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/scenario_api/performance_scenarios.h"
#include <atomic>
#include <ostream>
#include <utility>
#include "base/containers/enum_set.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/structured_shared_memory.h"
#include "components/performance_manager/scenario_api/performance_scenario_memory.h"
#include "components/performance_manager/scenario_api/performance_scenario_test_support.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace performance_scenarios {
// Test printers.
std::ostream& operator<<(std::ostream& os, LoadingScenario loading_scenario) {
switch (loading_scenario) {
case LoadingScenario::kNoPageLoading:
return os << "NoPageLoading";
case LoadingScenario::kFocusedPageLoading:
return os << "FocusedPageLoading";
case LoadingScenario::kVisiblePageLoading:
return os << "VisiblePageLoading";
case LoadingScenario::kBackgroundPageLoading:
return os << "BackgroundPageLoading";
}
}
std::ostream& operator<<(std::ostream& os, InputScenario input_scenario) {
switch (input_scenario) {
case InputScenario::kNoInput:
return os << "NoInput";
case InputScenario::kTyping:
return os << "TypingInput";
case InputScenario::kTap:
return os << "Tap";
case InputScenario::kScroll:
return os << "Scroll";
}
}
namespace {
// Tests that are repeated for all scenarios of a given type.
using PerformanceScenariosAllLoadingScenariosTest =
::testing::TestWithParam<LoadingScenario>;
using PerformanceScenariosAllInputScenariosTest =
::testing::TestWithParam<InputScenario>;
INSTANTIATE_TEST_SUITE_P(All,
PerformanceScenariosAllLoadingScenariosTest,
::testing::ValuesIn(LoadingScenarios::All()));
INSTANTIATE_TEST_SUITE_P(All,
PerformanceScenariosAllInputScenariosTest,
::testing::ValuesIn(InputScenarios::All()));
TEST(PerformanceScenariosTest, MappedScenarioState) {
auto test_helper = PerformanceScenarioTestHelper::CreateWithoutMapping();
ASSERT_TRUE(test_helper);
// Before the shared memory is mapped in, GetLoadingScenario should return
// default values.
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kCurrentProcess)
->load(std::memory_order_relaxed),
LoadingScenario::kNoPageLoading);
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kGlobal)
->load(std::memory_order_relaxed),
LoadingScenario::kNoPageLoading);
{
// Map the shared memory as the global state.
ScopedReadOnlyScenarioMemory mapped_global_memory(
ScenarioScope::kGlobal,
test_helper->GetReadOnlyScenarioRegion(ScenarioScope::kGlobal));
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kGlobal)
->load(std::memory_order_relaxed),
LoadingScenario::kNoPageLoading);
// Updates should be visible in the global state only.
test_helper->SetLoadingScenario(ScenarioScope::kGlobal,
LoadingScenario::kFocusedPageLoading);
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kGlobal)
->load(std::memory_order_relaxed),
LoadingScenario::kFocusedPageLoading);
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kCurrentProcess)
->load(std::memory_order_relaxed),
LoadingScenario::kNoPageLoading);
// Map the same shared memory as the per-process state.
ScopedReadOnlyScenarioMemory mapped_current_memory(
ScenarioScope::kCurrentProcess,
test_helper->GetReadOnlyScenarioRegion(ScenarioScope::kGlobal));
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kCurrentProcess)
->load(std::memory_order_relaxed),
LoadingScenario::kFocusedPageLoading);
// Updates should be visible in both mappings.
test_helper->SetLoadingScenario(ScenarioScope::kGlobal,
LoadingScenario::kVisiblePageLoading);
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kGlobal)
->load(std::memory_order_relaxed),
LoadingScenario::kVisiblePageLoading);
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kCurrentProcess)
->load(std::memory_order_relaxed),
LoadingScenario::kVisiblePageLoading);
}
// After going out of scope, the memory is unmapped and GetLoadingScenario
// should see default values again.
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kCurrentProcess)
->load(std::memory_order_relaxed),
LoadingScenario::kNoPageLoading);
EXPECT_EQ(GetLoadingScenario(ScenarioScope::kGlobal)
->load(std::memory_order_relaxed),
LoadingScenario::kNoPageLoading);
}
TEST(PerformanceScenariosTest, SharedAtomicRef) {
// Create and map shared memory.
auto test_helper = PerformanceScenarioTestHelper::CreateWithoutMapping();
ASSERT_TRUE(test_helper);
auto read_only_mapping =
base::StructuredSharedMemory<ScenarioState>::MapReadOnlyRegion(
test_helper->GetReadOnlyScenarioRegion(ScenarioScope::kGlobal));
ASSERT_TRUE(read_only_mapping.has_value());
// Store pointers to the atomics in the shared memory for later comparison.
const std::atomic<LoadingScenario>* loading_ptr =
&(read_only_mapping->ReadOnlyRef().loading);
const std::atomic<InputScenario>* input_ptr =
&(read_only_mapping->ReadOnlyRef().input);
// Transfer ownership of the mapping to a scoped_refptr.
auto mapping_ptr = base::MakeRefCounted<RefCountedScenarioMapping>(
std::move(read_only_mapping.value()));
SharedAtomicRef<LoadingScenario> loading_ref(
mapping_ptr, mapping_ptr->data.ReadOnlyRef().loading);
SharedAtomicRef<InputScenario> input_ref(
mapping_ptr, mapping_ptr->data.ReadOnlyRef().input);
// The SharedAtomicRef's should keep the mapping alive.
mapping_ptr.reset();
test_helper->SetLoadingScenario(ScenarioScope::kGlobal,
LoadingScenario::kBackgroundPageLoading);
test_helper->SetInputScenario(ScenarioScope::kGlobal, InputScenario::kTyping);
// get()
EXPECT_EQ(loading_ref.get(), loading_ptr);
EXPECT_EQ(input_ref.get(), input_ptr);
// operator*
EXPECT_EQ(*loading_ref, *loading_ptr);
EXPECT_EQ(*input_ref, *input_ptr);
// operator->
EXPECT_EQ(loading_ref->load(std::memory_order_relaxed),
LoadingScenario::kBackgroundPageLoading);
EXPECT_EQ(input_ref->load(std::memory_order_relaxed), InputScenario::kTyping);
}
TEST_P(PerformanceScenariosAllLoadingScenariosTest, EmptyScenarioPattern) {
// Nothing matches the pattern.
static ScenarioPattern kEmptyScenarioPattern{};
EXPECT_TRUE(ScenariosMatch(GetParam(), InputScenario::kNoInput,
kEmptyScenarioPattern));
EXPECT_TRUE(ScenariosMatch(GetParam(), InputScenario::kTyping,
kEmptyScenarioPattern));
EXPECT_TRUE(
ScenariosMatch(GetParam(), InputScenario::kTap, kEmptyScenarioPattern));
EXPECT_TRUE(ScenariosMatch(GetParam(), InputScenario::kScroll,
kEmptyScenarioPattern));
}
TEST_P(PerformanceScenariosAllLoadingScenariosTest, NoInputScenarioPattern) {
// kNoInput matches the pattern, loading scenarios are ignored.
static ScenarioPattern kInputScenarioPattern{
.input = {InputScenario::kNoInput},
};
EXPECT_TRUE(ScenariosMatch(GetParam(), InputScenario::kNoInput,
kInputScenarioPattern));
EXPECT_FALSE(ScenariosMatch(GetParam(), InputScenario::kTyping,
kInputScenarioPattern));
EXPECT_FALSE(
ScenariosMatch(GetParam(), InputScenario::kTap, kInputScenarioPattern));
EXPECT_FALSE(ScenariosMatch(GetParam(), InputScenario::kScroll,
kInputScenarioPattern));
}
TEST_P(PerformanceScenariosAllLoadingScenariosTest, InputScenarioPattern) {
// All except kNoInput matches the pattern, loading scenarios are ignored.
static ScenarioPattern kInputScenarioPattern{
.input = {InputScenario::kTyping, InputScenario::kTap,
InputScenario::kScroll},
};
EXPECT_FALSE(ScenariosMatch(GetParam(), InputScenario::kNoInput,
kInputScenarioPattern));
EXPECT_TRUE(ScenariosMatch(GetParam(), InputScenario::kTyping,
kInputScenarioPattern));
EXPECT_TRUE(
ScenariosMatch(GetParam(), InputScenario::kTap, kInputScenarioPattern));
EXPECT_TRUE(ScenariosMatch(GetParam(), InputScenario::kScroll,
kInputScenarioPattern));
}
TEST_P(PerformanceScenariosAllInputScenariosTest, LoadingScenarioPattern) {
// Only kNoPageLoading matches the pattern, input scenarios are ignored.
static ScenarioPattern kNotLoadingScenarioPattern{
.loading = {LoadingScenario::kNoPageLoading},
};
EXPECT_TRUE(ScenariosMatch(LoadingScenario::kNoPageLoading, GetParam(),
kNotLoadingScenarioPattern));
EXPECT_FALSE(ScenariosMatch(LoadingScenario::kBackgroundPageLoading,
GetParam(), kNotLoadingScenarioPattern));
EXPECT_FALSE(ScenariosMatch(LoadingScenario::kVisiblePageLoading, GetParam(),
kNotLoadingScenarioPattern));
EXPECT_FALSE(ScenariosMatch(LoadingScenario::kFocusedPageLoading, GetParam(),
kNotLoadingScenarioPattern));
// Loading background pages match the pattern, input scenarios are ignored.
static ScenarioPattern kBackgroundLoadingScenarioPattern{
.loading = {LoadingScenario::kNoPageLoading,
LoadingScenario::kBackgroundPageLoading},
};
EXPECT_TRUE(ScenariosMatch(LoadingScenario::kNoPageLoading, GetParam(),
kBackgroundLoadingScenarioPattern));
EXPECT_TRUE(ScenariosMatch(LoadingScenario::kBackgroundPageLoading,
GetParam(), kBackgroundLoadingScenarioPattern));
EXPECT_FALSE(ScenariosMatch(LoadingScenario::kVisiblePageLoading, GetParam(),
kBackgroundLoadingScenarioPattern));
EXPECT_FALSE(ScenariosMatch(LoadingScenario::kFocusedPageLoading, GetParam(),
kBackgroundLoadingScenarioPattern));
// Loading non-focused pages match the pattern, input scenarios are ignored.
static ScenarioPattern kNonFocusedLoadingScenarioPattern{
.loading = {LoadingScenario::kNoPageLoading,
LoadingScenario::kBackgroundPageLoading,
LoadingScenario::kVisiblePageLoading},
};
EXPECT_TRUE(ScenariosMatch(LoadingScenario::kNoPageLoading, GetParam(),
kNonFocusedLoadingScenarioPattern));
EXPECT_TRUE(ScenariosMatch(LoadingScenario::kBackgroundPageLoading,
GetParam(), kNonFocusedLoadingScenarioPattern));
EXPECT_TRUE(ScenariosMatch(LoadingScenario::kVisiblePageLoading, GetParam(),
kNonFocusedLoadingScenarioPattern));
EXPECT_FALSE(ScenariosMatch(LoadingScenario::kFocusedPageLoading, GetParam(),
kNonFocusedLoadingScenarioPattern));
}
// By default, scenarios are only idle with no foreground loading and no input.
// If one of these tests fails, check if the definition of kDefaultIdleScenarios
// has changed.
TEST(PerformanceScenariosTest, DefaultIdleScenariosNoInput) {
EXPECT_TRUE(ScenariosMatch(LoadingScenario::kNoPageLoading,
InputScenario::kNoInput, kDefaultIdleScenarios));
EXPECT_TRUE(ScenariosMatch(LoadingScenario::kBackgroundPageLoading,
InputScenario::kNoInput, kDefaultIdleScenarios));
EXPECT_FALSE(ScenariosMatch(LoadingScenario::kVisiblePageLoading,
InputScenario::kNoInput, kDefaultIdleScenarios));
EXPECT_FALSE(ScenariosMatch(LoadingScenario::kFocusedPageLoading,
InputScenario::kNoInput, kDefaultIdleScenarios));
}
TEST_P(PerformanceScenariosAllLoadingScenariosTest,
DefaultIdleScenariosWithInput) {
EXPECT_FALSE(ScenariosMatch(GetParam(), InputScenario::kTyping,
kDefaultIdleScenarios));
}
// Ensure that all values of LoadingScenario are ordered correctly.
TEST_P(PerformanceScenariosAllLoadingScenariosTest, LoadingScenarioOrdering) {
// When adding a new scenario to this switch, make sure that everything
// comparing less than the new scenario is less performance-sensitive.
switch (GetParam()) {
case LoadingScenario::kFocusedPageLoading:
EXPECT_LT(LoadingScenario::kVisiblePageLoading, GetParam());
[[fallthrough]];
case LoadingScenario::kVisiblePageLoading:
EXPECT_LT(LoadingScenario::kBackgroundPageLoading, GetParam());
[[fallthrough]];
case LoadingScenario::kBackgroundPageLoading:
EXPECT_LT(LoadingScenario::kNoPageLoading, GetParam());
[[fallthrough]];
case LoadingScenario::kNoPageLoading:
// This is the lowest priority scenario.
break;
}
}
// Ensure that all values of InputScenario are ordered correctly.
TEST_P(PerformanceScenariosAllInputScenariosTest, InputScenarioOrdering) {
// When adding a new scenario to this switch, make sure that everything
// comparing less than the new scenario is less performance-sensitive.
switch (GetParam()) {
case InputScenario::kScroll:
// Scrolling is the most performance-sensitive because scroll jank is
// very obvious to the user.
EXPECT_LT(InputScenario::kTap, GetParam());
[[fallthrough]];
case InputScenario::kTap:
// Tap is more performance-sensitive than typing because there's a tactile
// link: users expect immediate screen feedback when touching the screen.
EXPECT_LT(InputScenario::kTyping, GetParam());
[[fallthrough]];
case InputScenario::kTyping:
EXPECT_LT(InputScenario::kNoInput, GetParam());
[[fallthrough]];
case InputScenario::kNoInput:
// This is the lowest priority scenario.
break;
}
}
} // namespace
} // namespace performance_scenarios
|