1 // Copyright 2018 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.
5 #include "components/scheduling_metrics/thread_metrics.h"
7 #include "base/task/sequence_manager/test/fake_task.h"
8 #include "base/test/metrics/histogram_tester.h"
9 #include "base/time/time_override.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using base::sequence_manager::TaskQueue;
15 namespace scheduling_metrics {
19 using base::sequence_manager::FakeTask;
20 using base::sequence_manager::FakeTaskTiming;
22 base::TimeTicks Seconds(int seconds) {
23 return base::TimeTicks() + base::TimeDelta::FromSeconds(seconds);
26 base::ThreadTicks ThreadSeconds(int seconds) {
27 return base::ThreadTicks() + base::TimeDelta::FromSeconds(seconds);
32 TEST(MetricsHelperTest, TaskDurationPerThreadType) {
33 base::HistogramTester histogram_tester;
35 ThreadMetrics main_thread_metrics(ThreadType::kRendererMainThread,
36 false /* has_cpu_timing_for_each_task */);
37 ThreadMetrics compositor_metrics(ThreadType::kRendererCompositorThread,
38 false /* has_cpu_timing_for_each_task */);
39 ThreadMetrics worker_metrics(ThreadType::kRendererOtherBlinkThread,
40 false /* has_cpu_timing_for_each_task */);
42 main_thread_metrics.RecordTaskMetrics(
44 FakeTaskTiming(Seconds(10), Seconds(50), ThreadSeconds(0),
46 compositor_metrics.RecordTaskMetrics(
48 FakeTaskTiming(Seconds(10), Seconds(80), ThreadSeconds(0),
50 compositor_metrics.RecordTaskMetrics(
51 nullptr, FakeTask(), FakeTaskTiming(Seconds(100), Seconds(200)));
52 worker_metrics.RecordTaskMetrics(
54 FakeTaskTiming(Seconds(10), Seconds(125), ThreadSeconds(0),
58 histogram_tester.GetAllSamples(
59 "Scheduler.Experimental.WallTimePerThread"),
60 testing::UnorderedElementsAre(
61 base::Bucket(static_cast<int>(ThreadType::kRendererMainThread), 40),
62 base::Bucket(static_cast<int>(ThreadType::kRendererCompositorThread),
64 base::Bucket(static_cast<int>(ThreadType::kRendererOtherBlinkThread),
68 histogram_tester.GetAllSamples("Scheduler.Experimental.CPUTimePerThread"),
69 testing::UnorderedElementsAre(
70 base::Bucket(static_cast<int>(ThreadType::kRendererMainThread), 15),
71 base::Bucket(static_cast<int>(ThreadType::kRendererCompositorThread),
73 base::Bucket(static_cast<int>(ThreadType::kRendererOtherBlinkThread),
77 TEST(MetricsHelperTest, TrackedCPUTimeMetrics) {
78 base::HistogramTester histogram_tester;
79 base::subtle::ScopedTimeClockOverrides time_override(
80 []() { return base::Time(); }, []() { return Seconds(1); },
81 []() { return ThreadSeconds(1); });
83 ThreadMetrics main_thread_metrics(ThreadType::kRendererMainThread,
84 true /* has_cpu_timing_for_each_task */);
86 main_thread_metrics.RecordTaskMetrics(
88 FakeTaskTiming(Seconds(10), Seconds(50), ThreadSeconds(5),
90 main_thread_metrics.RecordTaskMetrics(
92 FakeTaskTiming(Seconds(10), Seconds(50), ThreadSeconds(20),
95 EXPECT_THAT(histogram_tester.GetAllSamples(
96 "Scheduler.Experimental.CPUTimePerThread.Tracked"),
97 testing::UnorderedElementsAre(base::Bucket(
98 static_cast<int>(ThreadType::kRendererMainThread), 15)));
99 // 9 = 4 seconds before task 1 and 5 seconds between tasks 1 and 2.
100 EXPECT_THAT(histogram_tester.GetAllSamples(
101 "Scheduler.Experimental.CPUTimePerThread.Untracked"),
102 testing::UnorderedElementsAre(base::Bucket(
103 static_cast<int>(ThreadType::kRendererMainThread), 9)));
106 } // namespace scheduling_metrics