1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19
20 #include <JankTracker.h>
21 #include <utils/TimeUtils.h>
22
23 using namespace android;
24 using namespace android::uirenderer;
25
26 class TestFrameMetricsObserver : public FrameMetricsObserver {
27 public:
notify(const int64_t *)28 void notify(const int64_t*) {}
29 };
30
TEST(JankTracker,noJank)31 TEST(JankTracker, noJank) {
32 std::mutex mutex;
33 ProfileDataContainer container(mutex);
34 JankTracker jankTracker(&container);
35 std::unique_ptr<FrameMetricsReporter> reporter = std::make_unique<FrameMetricsReporter>();
36
37 uint64_t frameNumber = 0;
38 uint32_t surfaceId = 0;
39
40 FrameInfo* info = jankTracker.startFrame();
41 info->set(FrameInfoIndex::IntendedVsync) = 100_ms;
42 info->set(FrameInfoIndex::Vsync) = 101_ms;
43 info->set(FrameInfoIndex::SwapBuffersCompleted) = 115_ms;
44 info->set(FrameInfoIndex::GpuCompleted) = 115_ms;
45 info->set(FrameInfoIndex::FrameCompleted) = 115_ms;
46 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
47 info->set(FrameInfoIndex::FrameDeadline) = 120_ms;
48 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
49
50 info = jankTracker.startFrame();
51 info->set(FrameInfoIndex::IntendedVsync) = 116_ms;
52 info->set(FrameInfoIndex::Vsync) = 117_ms;
53 info->set(FrameInfoIndex::SwapBuffersCompleted) = 129_ms;
54 info->set(FrameInfoIndex::GpuCompleted) = 131_ms;
55 info->set(FrameInfoIndex::FrameCompleted) = 131_ms;
56 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
57 info->set(FrameInfoIndex::FrameDeadline) = 136_ms;
58 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
59
60 ASSERT_EQ(2, container.get()->totalFrameCount());
61 ASSERT_EQ(0, container.get()->jankFrameCount());
62 }
63
64
TEST(JankTracker,jank)65 TEST(JankTracker, jank) {
66 std::mutex mutex;
67 ProfileDataContainer container(mutex);
68 JankTracker jankTracker(&container);
69 std::unique_ptr<FrameMetricsReporter> reporter = std::make_unique<FrameMetricsReporter>();
70
71 uint64_t frameNumber = 0;
72 uint32_t surfaceId = 0;
73
74 FrameInfo* info = jankTracker.startFrame();
75 info->set(FrameInfoIndex::IntendedVsync) = 100_ms;
76 info->set(FrameInfoIndex::Vsync) = 101_ms;
77 info->set(FrameInfoIndex::SwapBuffersCompleted) = 115_ms;
78 info->set(FrameInfoIndex::GpuCompleted) = 121_ms;
79 info->set(FrameInfoIndex::FrameCompleted) = 121_ms;
80 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
81 info->set(FrameInfoIndex::FrameDeadline) = 120_ms;
82 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
83
84 ASSERT_EQ(1, container.get()->totalFrameCount());
85 ASSERT_EQ(1, container.get()->jankFrameCount());
86 }
87
TEST(JankTracker,legacyJankButNoRealJank)88 TEST(JankTracker, legacyJankButNoRealJank) {
89 std::mutex mutex;
90 ProfileDataContainer container(mutex);
91 JankTracker jankTracker(&container);
92 std::unique_ptr<FrameMetricsReporter> reporter = std::make_unique<FrameMetricsReporter>();
93
94 uint64_t frameNumber = 0;
95 uint32_t surfaceId = 0;
96
97 FrameInfo* info = jankTracker.startFrame();
98 info->set(FrameInfoIndex::IntendedVsync) = 100_ms;
99 info->set(FrameInfoIndex::Vsync) = 101_ms;
100 info->set(FrameInfoIndex::SwapBuffersCompleted) = 117_ms;
101 info->set(FrameInfoIndex::GpuCompleted) = 118_ms;
102 info->set(FrameInfoIndex::FrameCompleted) = 118_ms;
103 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
104 info->set(FrameInfoIndex::FrameDeadline) = 120_ms;
105 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
106
107 ASSERT_EQ(1, container.get()->totalFrameCount());
108 ASSERT_EQ(0, container.get()->jankFrameCount());
109 ASSERT_EQ(1, container.get()->jankLegacyFrameCount());
110 }
111
TEST(JankTracker,doubleStuffed)112 TEST(JankTracker, doubleStuffed) {
113 std::mutex mutex;
114 ProfileDataContainer container(mutex);
115 JankTracker jankTracker(&container);
116 std::unique_ptr<FrameMetricsReporter> reporter = std::make_unique<FrameMetricsReporter>();
117
118 uint64_t frameNumber = 0;
119 uint32_t surfaceId = 0;
120
121 // First frame janks
122 FrameInfo* info = jankTracker.startFrame();
123 info->set(FrameInfoIndex::IntendedVsync) = 100_ms;
124 info->set(FrameInfoIndex::Vsync) = 101_ms;
125 info->set(FrameInfoIndex::SwapBuffersCompleted) = 115_ms;
126 info->set(FrameInfoIndex::GpuCompleted) = 121_ms;
127 info->set(FrameInfoIndex::FrameCompleted) = 121_ms;
128 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
129 info->set(FrameInfoIndex::FrameDeadline) = 120_ms;
130 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
131
132 ASSERT_EQ(1, container.get()->jankFrameCount());
133
134 // Second frame is long, but doesn't jank because double-stuffed.
135 info = jankTracker.startFrame();
136 info->set(FrameInfoIndex::IntendedVsync) = 116_ms;
137 info->set(FrameInfoIndex::Vsync) = 122_ms;
138 info->set(FrameInfoIndex::SwapBuffersCompleted) = 129_ms;
139 info->set(FrameInfoIndex::GpuCompleted) = 137_ms;
140 info->set(FrameInfoIndex::FrameCompleted) = 137_ms;
141 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
142 info->set(FrameInfoIndex::FrameDeadline) = 136_ms;
143 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
144
145 ASSERT_EQ(2, container.get()->totalFrameCount());
146 ASSERT_EQ(1, container.get()->jankFrameCount());
147 }
148
TEST(JankTracker,doubleStuffedThenPauseThenJank)149 TEST(JankTracker, doubleStuffedThenPauseThenJank) {
150 std::mutex mutex;
151 ProfileDataContainer container(mutex);
152 JankTracker jankTracker(&container);
153 std::unique_ptr<FrameMetricsReporter> reporter = std::make_unique<FrameMetricsReporter>();
154
155 uint64_t frameNumber = 0;
156 uint32_t surfaceId = 0;
157
158 // First frame janks
159 FrameInfo* info = jankTracker.startFrame();
160 info->set(FrameInfoIndex::IntendedVsync) = 100_ms;
161 info->set(FrameInfoIndex::Vsync) = 101_ms;
162 info->set(FrameInfoIndex::SwapBuffersCompleted) = 115_ms;
163 info->set(FrameInfoIndex::GpuCompleted) = 121_ms;
164 info->set(FrameInfoIndex::FrameCompleted) = 121_ms;
165 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
166 info->set(FrameInfoIndex::FrameDeadline) = 120_ms;
167 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
168
169 ASSERT_EQ(1, container.get()->jankFrameCount());
170
171 // Second frame is long, but doesn't jank because double-stuffed.
172 info = jankTracker.startFrame();
173 info->set(FrameInfoIndex::IntendedVsync) = 116_ms;
174 info->set(FrameInfoIndex::Vsync) = 122_ms;
175 info->set(FrameInfoIndex::SwapBuffersCompleted) = 129_ms;
176 info->set(FrameInfoIndex::GpuCompleted) = 137_ms;
177 info->set(FrameInfoIndex::FrameCompleted) = 137_ms;
178 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
179 info->set(FrameInfoIndex::FrameDeadline) = 136_ms;
180 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
181
182 ASSERT_EQ(1, container.get()->jankFrameCount());
183
184 // Thirdframe is long and skips one frame some double stuffed logic gets reset
185 info = jankTracker.startFrame();
186 info->set(FrameInfoIndex::IntendedVsync) = 148_ms;
187 info->set(FrameInfoIndex::Vsync) = 148_ms;
188 info->set(FrameInfoIndex::SwapBuffersCompleted) = 160_ms;
189 info->set(FrameInfoIndex::GpuCompleted) = 169_ms;
190 info->set(FrameInfoIndex::FrameCompleted) = 169_ms;
191 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
192 info->set(FrameInfoIndex::FrameDeadline) = 168_ms;
193 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
194
195 ASSERT_EQ(3, container.get()->totalFrameCount());
196 ASSERT_EQ(2, container.get()->jankFrameCount());
197 }
198
TEST(JankTracker,doubleStuffedTwoIntervalBehind)199 TEST(JankTracker, doubleStuffedTwoIntervalBehind) {
200 std::mutex mutex;
201 ProfileDataContainer container(mutex);
202 JankTracker jankTracker(&container);
203 std::unique_ptr<FrameMetricsReporter> reporter = std::make_unique<FrameMetricsReporter>();
204
205 uint64_t frameNumber = 0;
206 uint32_t surfaceId = 0;
207
208 // First frame janks
209 FrameInfo* info = jankTracker.startFrame();
210 info->set(FrameInfoIndex::IntendedVsync) = 100_ms;
211 info->set(FrameInfoIndex::Vsync) = 101_ms;
212 info->set(FrameInfoIndex::SwapBuffersCompleted) = 107_ms;
213 info->set(FrameInfoIndex::GpuCompleted) = 117_ms;
214 info->set(FrameInfoIndex::FrameCompleted) = 117_ms;
215 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
216 info->set(FrameInfoIndex::FrameDeadline) = 116_ms;
217 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
218
219 ASSERT_EQ(1, container.get()->jankFrameCount());
220
221 // Second frame is long, but doesn't jank because double-stuffed.
222 // Second frame duration is between 1*interval ~ 2*interval
223 info = jankTracker.startFrame();
224 info->set(FrameInfoIndex::IntendedVsync) = 116_ms;
225 info->set(FrameInfoIndex::Vsync) = 116_ms;
226 info->set(FrameInfoIndex::SwapBuffersCompleted) = 129_ms;
227 info->set(FrameInfoIndex::GpuCompleted) = 133_ms;
228 info->set(FrameInfoIndex::FrameCompleted) = 133_ms;
229 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
230 info->set(FrameInfoIndex::FrameDeadline) = 132_ms;
231 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
232
233 ASSERT_EQ(1, container.get()->jankFrameCount());
234
235 // Third frame is even longer, cause a jank
236 // Third frame duration is between 2*interval ~ 3*interval
237 info = jankTracker.startFrame();
238 info->set(FrameInfoIndex::IntendedVsync) = 132_ms;
239 info->set(FrameInfoIndex::Vsync) = 132_ms;
240 info->set(FrameInfoIndex::SwapBuffersCompleted) = 160_ms;
241 info->set(FrameInfoIndex::GpuCompleted) = 165_ms;
242 info->set(FrameInfoIndex::FrameCompleted) = 165_ms;
243 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
244 info->set(FrameInfoIndex::FrameDeadline) = 148_ms;
245 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
246
247 ASSERT_EQ(2, container.get()->jankFrameCount());
248
249 // 4th frame is double-stuffed with a 2 * interval latency
250 // 4th frame duration is between 2*interval ~ 3*interval
251 info = jankTracker.startFrame();
252 info->set(FrameInfoIndex::IntendedVsync) = 148_ms;
253 info->set(FrameInfoIndex::Vsync) = 148_ms;
254 info->set(FrameInfoIndex::SwapBuffersCompleted) = 170_ms;
255 info->set(FrameInfoIndex::GpuCompleted) = 181_ms;
256 info->set(FrameInfoIndex::FrameCompleted) = 181_ms;
257 info->set(FrameInfoIndex::FrameInterval) = 16_ms;
258 info->set(FrameInfoIndex::FrameDeadline) = 164_ms;
259 jankTracker.finishFrame(*info, reporter, frameNumber, surfaceId);
260
261 ASSERT_EQ(2, container.get()->jankFrameCount());
262 }
263