chromium/components/performance_manager/resource_attribution/cpu_measurement_monitor_unittest.cc

// Copyright 2023 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/resource_attribution/cpu_measurement_monitor.h"

#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/process/kill.h"
#include "base/process/process.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_timeouts.h"
#include "base/test/test_waitable_event.h"
#include "base/time/time.h"
#include "components/performance_manager/embedder/graph_features.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/graph/worker_node_impl.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/performance_manager.h"
#include "components/performance_manager/public/resource_attribution/cpu_measurement_delegate.h"
#include "components/performance_manager/public/resource_attribution/cpu_proportion_tracker.h"
#include "components/performance_manager/public/resource_attribution/origin_in_browsing_instance_context.h"
#include "components/performance_manager/public/resource_attribution/query_results.h"
#include "components/performance_manager/public/resource_attribution/resource_contexts.h"
#include "components/performance_manager/resource_attribution/performance_manager_aliases.h"
#include "components/performance_manager/resource_attribution/query_params.h"
#include "components/performance_manager/test_support/graph_test_harness.h"
#include "components/performance_manager/test_support/mock_graphs.h"
#include "components/performance_manager/test_support/performance_manager_test_harness.h"
#include "components/performance_manager/test_support/resource_attribution/gtest_util.h"
#include "components/performance_manager/test_support/resource_attribution/measurement_delegates.h"
#include "components/performance_manager/test_support/run_in_graph.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/process_type.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/navigation_simulator.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace resource_attribution {

namespace {

AllOf;
Conditional;
Contains;
Field;
IsEmpty;
Not;
Pair;

kBrowsingInstanceForOtherPage;
kBrowsingInstanceForPage;
TestNodeWrapper;
kResourceAttributionIncludeOrigins;
ProcessCPUUsageError;

constexpr base::TimeDelta kTimeBetweenMeasurements =;

// Creates a stub WorkerNode hosted in the given `process_node`, with the given
// `origin`, and adds it to `graph`.
TestNodeWrapper<WorkerNodeImpl> CreateWorkerNodeWithOrigin(
    GraphImpl* graph,
    ProcessNodeImpl* process_node,
    const url::Origin& origin) {}

// Like MockMultiplePagesAndWorkersWithMultipleProcessesGraph (see
// mock_graphs.h), but assigns a fixed origin to each WorkerNode.
struct MockMultiplePagesAndWorkersWithKnownOriginsGraph
    : public performance_manager::MockMultiplePagesWithMultipleProcessesGraph {};

constexpr internal::QueryId kQueryId =;
constexpr internal::QueryId kOtherQueryId =;

}  // namespace

// A test that creates mock processes to simulate exact CPU usage.
class ResourceAttrCPUMonitorTest
    : public performance_manager::GraphTestHarness {};

// Tests that renderers created at various points around CPU measurement
// snapshots are handled correctly.
TEST_F(ResourceAttrCPUMonitorTest, CreateTiming) {}

// Tests that renderers exiting at various points around CPU measurement
// snapshots are handled correctly.
TEST_F(ResourceAttrCPUMonitorTest, ExitTiming) {}

// Tests that varying CPU usage between measurement snapshots is reported
// correctly.
TEST_F(ResourceAttrCPUMonitorTest, VaryingMeasurements) {}

// Tests that CPU usage of non-renderers is measured.
TEST_F(ResourceAttrCPUMonitorTest, AllProcessTypes) {}

// Tests that CPU usage of processes is correctly distributed between frames and
// workers in those processes, and correctly aggregated to pages containing
// frames and workers from multiple processes.
TEST_F(ResourceAttrCPUMonitorTest, CPUDistribution) {}

// Tests that CPU usage of processes is correctly distributed between FrameNodes
// and WorkerNodes that are added and removed between measurements.
TEST_F(ResourceAttrCPUMonitorTest, AddRemoveNodes) {}

// Tests that WorkerNode CPU usage is correctly distributed to pages as clients
// are added and removed.
TEST_F(ResourceAttrCPUMonitorTest, AddRemoveWorkerClients) {}

// Tests that CPU usage of processes is correctly distributed between
// OriginInBrowsingInstanceContexts when a frame origin changes between
// measurements.
TEST_F(ResourceAttrCPUMonitorTest, NavigateChangesOrigin) {}

// Tests that `cumulative_background_cpu` is correctly maintained, including
// when process priority changes during a measurement interval.
TEST_F(ResourceAttrCPUMonitorTest, BackgroundCPU) {}

// Test that CPU time is accumulated correctly when an
// `OriginInBrowsingInstanceContext` dies and is revived, even when there are
// concurrent queries.
TEST_F(ResourceAttrCPUMonitorTest, OriginInBrowsingInstanceContextLifetime) {}

// Tests that errors returned from ProcessMetrics are correctly ignored.
TEST_F(ResourceAttrCPUMonitorTest, MeasurementError) {}

// Tests the CPUProportionTracker helper class.
TEST_F(ResourceAttrCPUMonitorTest, CPUProportionTracker) {}

namespace {

resource_attribution::CPUTimeResult CreateCPUTimeResult(
    base::TimeDelta cumulative_cpu,
    base::TimeDelta cumulative_background_cpu) {}

}  // namespace

// Tests the CPUProportionTracker helper class when configured to use cumulative
// background CPU instead of cumulative CPU.
TEST_F(ResourceAttrCPUMonitorTest, CPUProportionTrackerBackground) {}

// Tests that multiple CPUProportionTrackers with different schedules are
// independent. Also tests trackers with and without a context filter.
TEST_F(ResourceAttrCPUMonitorTest, MultipleCPUProportionTrackers) {}

// A test that creates real processes, to verify that measurement works with the
// timing of real node creation.
class ResourceAttrCPUMonitorTimingTest
    : public performance_manager::PerformanceManagerTestHarness {};

TEST_F(ResourceAttrCPUMonitorTimingTest, ProcessLifetime) {}

}  // namespace resource_attribution