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
content / common / android / cpu_time_metrics_unittest.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/android/cpu_time_metrics_internal.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "content/common/process_visibility_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace internal {
namespace {
void WorkForOneCpuSec(base::WaitableEvent* event) {
auto initial_ticks = base::ThreadTicks::Now();
while (!event->IsSignaled()) {
if (base::ThreadTicks::Now() > initial_ticks + base::Seconds(1)) {
event->Signal();
}
}
}
TEST(CpuTimeMetricsTest, RecordsMetricsForeground) {
// Ensure the visibility tracker is created on the test runner thread.
ProcessVisibilityTracker::GetInstance();
base::test::TaskEnvironment task_environment;
base::HistogramTester histograms;
base::Thread thread1("StackSamplingProfiler");
thread1.StartAndWaitForTesting();
ASSERT_TRUE(thread1.IsRunning());
base::WaitableEvent event;
// Create the ProcessCpuTimeMetrics instance and register it as the process
// visibility observer.
ProcessCpuTimeMetrics::SetIgnoreHistogramAllocatorForTesting(true);
std::unique_ptr<ProcessCpuTimeMetrics> metrics =
ProcessCpuTimeMetrics::CreateForTesting();
metrics->WaitForCollectionForTesting();
// Start out in the foreground and spend one CPU second there.
ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged(true);
metrics->WaitForCollectionForTesting();
thread1.task_runner()->PostTask(
FROM_HERE, BindOnce(&WorkForOneCpuSec, base::Unretained(&event)));
// Wait until the thread has consumed one second of CPU time.
event.Wait();
// Update the state to background to trigger the collection of high level
// metrics.
ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged(false);
metrics->WaitForCollectionForTesting();
// The test process has no process-type command line flag, so is recognized as
// the browser process. The thread created above is named like a sampling
// profiler thread.
static constexpr int kBrowserProcessBucket = 2;
static constexpr int kSamplingProfilerThreadBucket = 24;
// Expect that the CPU second spent by the thread above is represented in the
// metrics.
int browser_cpu_seconds = histograms.GetBucketCount(
"Power.CpuTimeSecondsPerProcessType", kBrowserProcessBucket);
EXPECT_GE(browser_cpu_seconds, 1);
int browser_cpu_seconds_foreground = histograms.GetBucketCount(
"Power.CpuTimeSecondsPerProcessType.Foreground", kBrowserProcessBucket);
EXPECT_GE(browser_cpu_seconds_foreground, 1);
// Thread breakdown requires periodic collection.
int thread_cpu_seconds =
histograms.GetBucketCount("Power.CpuTimeSecondsPerThreadType.Browser",
kSamplingProfilerThreadBucket);
EXPECT_EQ(thread_cpu_seconds, 0);
metrics->PerformFullCollectionForTesting();
metrics->WaitForCollectionForTesting();
thread_cpu_seconds =
histograms.GetBucketCount("Power.CpuTimeSecondsPerThreadType.Browser",
kSamplingProfilerThreadBucket);
EXPECT_GE(thread_cpu_seconds, 1);
thread1.Stop();
ProcessCpuTimeMetrics::SetIgnoreHistogramAllocatorForTesting(false);
}
TEST(CpuTimeMetricsTest, RecordsMetricsBackground) {
// Ensure the visibility tracker is created on the test runner thread.
ProcessVisibilityTracker::GetInstance();
base::test::TaskEnvironment task_environment;
base::HistogramTester histograms;
base::Thread thread1("StackSamplingProfiler");
thread1.StartAndWaitForTesting();
ASSERT_TRUE(thread1.IsRunning());
base::WaitableEvent event;
// Create the ProcessCpuTimeMetrics instance and register it as the process
// visibility observer.
ProcessCpuTimeMetrics::SetIgnoreHistogramAllocatorForTesting(true);
std::unique_ptr<ProcessCpuTimeMetrics> metrics =
ProcessCpuTimeMetrics::CreateForTesting();
metrics->WaitForCollectionForTesting();
// Start out in the background and spend one CPU second there.
ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged(false);
metrics->WaitForCollectionForTesting();
thread1.task_runner()->PostTask(
FROM_HERE, BindOnce(&WorkForOneCpuSec, base::Unretained(&event)));
// Wait until the thread has consumed one second of CPU time.
event.Wait();
// Update the state to foreground to trigger the collection of high level
// metrics.
ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged(true);
metrics->WaitForCollectionForTesting();
// The test process has no process-type command line flag, so is recognized as
// the browser process. The thread created above is named like a sampling
// profiler thread.
static constexpr int kBrowserProcessBucket = 2;
static constexpr int kSamplingProfilerThreadBucket = 24;
// Expect that the CPU second spent by the thread above is represented in the
// metrics.
int browser_cpu_seconds = histograms.GetBucketCount(
"Power.CpuTimeSecondsPerProcessType", kBrowserProcessBucket);
EXPECT_GE(browser_cpu_seconds, 1);
int browser_cpu_seconds_background = histograms.GetBucketCount(
"Power.CpuTimeSecondsPerProcessType.Background", kBrowserProcessBucket);
EXPECT_GE(browser_cpu_seconds_background, 1);
// Thread breakdown requires periodic collection.
int thread_cpu_seconds =
histograms.GetBucketCount("Power.CpuTimeSecondsPerThreadType.Browser",
kSamplingProfilerThreadBucket);
EXPECT_EQ(thread_cpu_seconds, 0);
metrics->PerformFullCollectionForTesting();
metrics->WaitForCollectionForTesting();
thread_cpu_seconds =
histograms.GetBucketCount("Power.CpuTimeSecondsPerThreadType.Browser",
kSamplingProfilerThreadBucket);
EXPECT_GE(thread_cpu_seconds, 1);
thread1.Stop();
ProcessCpuTimeMetrics::SetIgnoreHistogramAllocatorForTesting(false);
}
} // namespace
} // namespace internal
} // namespace content