forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_timings_recorder_unittests.cc
301 lines (240 loc) · 12.3 KB
/
frame_timings_recorder_unittests.cc
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/flow/frame_timings.h"
#include "flutter/flow/testing/layer_test.h"
#include "flutter/flow/testing/mock_layer.h"
#include "flutter/flow/testing/mock_raster_cache.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include <thread>
#include "flutter/fml/time/time_delta.h"
#include "flutter/fml/time/time_point.h"
#include "gtest/gtest.h"
namespace flutter {
namespace testing {
TEST(FrameTimingsRecorderTest, RecordVsync) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto st = fml::TimePoint::Now();
const auto en = st + fml::TimeDelta::FromMillisecondsF(16);
recorder->RecordVsync(st, en);
ASSERT_EQ(st, recorder->GetVsyncStartTime());
ASSERT_EQ(en, recorder->GetVsyncTargetTime());
}
TEST(FrameTimingsRecorderTest, RecordBuildTimes) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto st = fml::TimePoint::Now();
const auto en = st + fml::TimeDelta::FromMillisecondsF(16);
recorder->RecordVsync(st, en);
const auto build_start = fml::TimePoint::Now();
const auto build_end = build_start + fml::TimeDelta::FromMillisecondsF(16);
recorder->RecordBuildStart(build_start);
recorder->RecordBuildEnd(build_end);
ASSERT_EQ(build_start, recorder->GetBuildStartTime());
ASSERT_EQ(build_end, recorder->GetBuildEndTime());
}
TEST(FrameTimingsRecorderTest, RecordRasterTimes) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto st = fml::TimePoint::Now();
const auto en = st + fml::TimeDelta::FromMillisecondsF(16);
recorder->RecordVsync(st, en);
const auto build_start = fml::TimePoint::Now();
const auto build_end = build_start + fml::TimeDelta::FromMillisecondsF(16);
recorder->RecordBuildStart(build_start);
recorder->RecordBuildEnd(build_end);
using namespace std::chrono_literals;
const auto raster_start = fml::TimePoint::Now();
recorder->RecordRasterStart(raster_start);
const auto before_raster_end_wall_time = fml::TimePoint::CurrentWallTime();
std::this_thread::sleep_for(1ms);
const auto timing = recorder->RecordRasterEnd();
std::this_thread::sleep_for(1ms);
const auto after_raster_end_wall_time = fml::TimePoint::CurrentWallTime();
ASSERT_EQ(raster_start, recorder->GetRasterStartTime());
ASSERT_GT(recorder->GetRasterEndWallTime(), before_raster_end_wall_time);
ASSERT_LT(recorder->GetRasterEndWallTime(), after_raster_end_wall_time);
ASSERT_EQ(recorder->GetFrameNumber(), timing.GetFrameNumber());
ASSERT_EQ(recorder->GetLayerCacheCount(), 0u);
ASSERT_EQ(recorder->GetLayerCacheBytes(), 0u);
ASSERT_EQ(recorder->GetPictureCacheCount(), 0u);
ASSERT_EQ(recorder->GetPictureCacheBytes(), 0u);
}
TEST(FrameTimingsRecorderTest, RecordRasterTimesWithCache) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto st = fml::TimePoint::Now();
const auto en = st + fml::TimeDelta::FromMillisecondsF(16);
recorder->RecordVsync(st, en);
const auto build_start = fml::TimePoint::Now();
const auto build_end = build_start + fml::TimeDelta::FromMillisecondsF(16);
recorder->RecordBuildStart(build_start);
recorder->RecordBuildEnd(build_end);
using namespace std::chrono_literals;
MockRasterCache cache(1, 10);
cache.BeginFrame();
const auto raster_start = fml::TimePoint::Now();
recorder->RecordRasterStart(raster_start);
cache.AddMockLayer(100, 100);
size_t layer_bytes = cache.EstimateLayerCacheByteSize();
EXPECT_GT(layer_bytes, 0u);
cache.AddMockPicture(100, 100);
size_t picture_bytes = cache.EstimatePictureCacheByteSize();
EXPECT_GT(picture_bytes, 0u);
cache.EvictUnusedCacheEntries();
cache.EndFrame();
const auto before_raster_end_wall_time = fml::TimePoint::CurrentWallTime();
std::this_thread::sleep_for(1ms);
const auto timing = recorder->RecordRasterEnd(&cache);
std::this_thread::sleep_for(1ms);
const auto after_raster_end_wall_time = fml::TimePoint::CurrentWallTime();
ASSERT_EQ(raster_start, recorder->GetRasterStartTime());
ASSERT_GT(recorder->GetRasterEndWallTime(), before_raster_end_wall_time);
ASSERT_LT(recorder->GetRasterEndWallTime(), after_raster_end_wall_time);
ASSERT_EQ(recorder->GetFrameNumber(), timing.GetFrameNumber());
ASSERT_EQ(recorder->GetLayerCacheCount(), 1u);
ASSERT_EQ(recorder->GetLayerCacheBytes(), layer_bytes);
ASSERT_EQ(recorder->GetPictureCacheCount(), 1u);
ASSERT_EQ(recorder->GetPictureCacheBytes(), picture_bytes);
}
// Windows and Fuchsia don't allow testing with killed by signal.
#if !defined(OS_FUCHSIA) && !defined(FML_OS_WIN) && \
(FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG)
TEST(FrameTimingsRecorderTest, ThrowWhenRecordBuildBeforeVsync) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto build_start = fml::TimePoint::Now();
EXPECT_EXIT(recorder->RecordBuildStart(build_start),
::testing::KilledBySignal(SIGABRT),
"Check failed: state_ == State::kVsync.");
}
TEST(FrameTimingsRecorderTest, ThrowWhenRecordRasterBeforeBuildEnd) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto st = fml::TimePoint::Now();
const auto en = st + fml::TimeDelta::FromMillisecondsF(16);
recorder->RecordVsync(st, en);
const auto raster_start = fml::TimePoint::Now();
EXPECT_EXIT(recorder->RecordRasterStart(raster_start),
::testing::KilledBySignal(SIGABRT),
"Check failed: state_ == State::kBuildEnd.");
}
#endif
TEST(FrameTimingsRecorderTest, RecordersHaveUniqueFrameNumbers) {
auto recorder1 = std::make_unique<FrameTimingsRecorder>();
auto recorder2 = std::make_unique<FrameTimingsRecorder>();
ASSERT_TRUE(recorder2->GetFrameNumber() > recorder1->GetFrameNumber());
}
TEST(FrameTimingsRecorderTest, ClonedHasSameFrameNumber) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
auto cloned =
recorder->CloneUntil(FrameTimingsRecorder::State::kUninitialized);
ASSERT_EQ(recorder->GetFrameNumber(), cloned->GetFrameNumber());
}
TEST(FrameTimingsRecorderTest, ClonedHasSameVsyncStartAndTarget) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto now = fml::TimePoint::Now();
recorder->RecordVsync(now, now + fml::TimeDelta::FromMilliseconds(16));
auto cloned = recorder->CloneUntil(FrameTimingsRecorder::State::kVsync);
ASSERT_EQ(recorder->GetFrameNumber(), cloned->GetFrameNumber());
ASSERT_EQ(recorder->GetVsyncStartTime(), cloned->GetVsyncStartTime());
ASSERT_EQ(recorder->GetVsyncTargetTime(), cloned->GetVsyncTargetTime());
}
TEST(FrameTimingsRecorderTest, ClonedHasSameBuildStart) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto now = fml::TimePoint::Now();
recorder->RecordVsync(now, now + fml::TimeDelta::FromMilliseconds(16));
recorder->RecordBuildStart(fml::TimePoint::Now());
auto cloned = recorder->CloneUntil(FrameTimingsRecorder::State::kBuildStart);
ASSERT_EQ(recorder->GetFrameNumber(), cloned->GetFrameNumber());
ASSERT_EQ(recorder->GetVsyncStartTime(), cloned->GetVsyncStartTime());
ASSERT_EQ(recorder->GetVsyncTargetTime(), cloned->GetVsyncTargetTime());
ASSERT_EQ(recorder->GetBuildStartTime(), cloned->GetBuildStartTime());
}
TEST(FrameTimingsRecorderTest, ClonedHasSameBuildEnd) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto now = fml::TimePoint::Now();
recorder->RecordVsync(now, now + fml::TimeDelta::FromMilliseconds(16));
recorder->RecordBuildStart(fml::TimePoint::Now());
recorder->RecordBuildEnd(fml::TimePoint::Now());
auto cloned = recorder->CloneUntil(FrameTimingsRecorder::State::kBuildEnd);
ASSERT_EQ(recorder->GetFrameNumber(), cloned->GetFrameNumber());
ASSERT_EQ(recorder->GetVsyncStartTime(), cloned->GetVsyncStartTime());
ASSERT_EQ(recorder->GetVsyncTargetTime(), cloned->GetVsyncTargetTime());
ASSERT_EQ(recorder->GetBuildStartTime(), cloned->GetBuildStartTime());
ASSERT_EQ(recorder->GetBuildEndTime(), cloned->GetBuildEndTime());
}
TEST(FrameTimingsRecorderTest, ClonedHasSameRasterStart) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto now = fml::TimePoint::Now();
recorder->RecordVsync(now, now + fml::TimeDelta::FromMilliseconds(16));
recorder->RecordBuildStart(fml::TimePoint::Now());
recorder->RecordBuildEnd(fml::TimePoint::Now());
recorder->RecordRasterStart(fml::TimePoint::Now());
auto cloned = recorder->CloneUntil(FrameTimingsRecorder::State::kRasterStart);
ASSERT_EQ(recorder->GetFrameNumber(), cloned->GetFrameNumber());
ASSERT_EQ(recorder->GetVsyncStartTime(), cloned->GetVsyncStartTime());
ASSERT_EQ(recorder->GetVsyncTargetTime(), cloned->GetVsyncTargetTime());
ASSERT_EQ(recorder->GetBuildStartTime(), cloned->GetBuildStartTime());
ASSERT_EQ(recorder->GetBuildEndTime(), cloned->GetBuildEndTime());
ASSERT_EQ(recorder->GetRasterStartTime(), cloned->GetRasterStartTime());
}
TEST(FrameTimingsRecorderTest, ClonedHasSameRasterEnd) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
const auto now = fml::TimePoint::Now();
recorder->RecordVsync(now, now + fml::TimeDelta::FromMilliseconds(16));
recorder->RecordBuildStart(fml::TimePoint::Now());
recorder->RecordBuildEnd(fml::TimePoint::Now());
recorder->RecordRasterStart(fml::TimePoint::Now());
recorder->RecordRasterEnd();
auto cloned = recorder->CloneUntil(FrameTimingsRecorder::State::kRasterEnd);
ASSERT_EQ(recorder->GetFrameNumber(), cloned->GetFrameNumber());
ASSERT_EQ(recorder->GetVsyncStartTime(), cloned->GetVsyncStartTime());
ASSERT_EQ(recorder->GetVsyncTargetTime(), cloned->GetVsyncTargetTime());
ASSERT_EQ(recorder->GetBuildStartTime(), cloned->GetBuildStartTime());
ASSERT_EQ(recorder->GetBuildEndTime(), cloned->GetBuildEndTime());
ASSERT_EQ(recorder->GetRasterStartTime(), cloned->GetRasterStartTime());
ASSERT_EQ(recorder->GetRasterEndTime(), cloned->GetRasterEndTime());
ASSERT_EQ(recorder->GetRasterEndWallTime(), cloned->GetRasterEndWallTime());
ASSERT_EQ(recorder->GetLayerCacheCount(), cloned->GetLayerCacheCount());
ASSERT_EQ(recorder->GetLayerCacheBytes(), cloned->GetLayerCacheBytes());
ASSERT_EQ(recorder->GetPictureCacheCount(), cloned->GetPictureCacheCount());
ASSERT_EQ(recorder->GetPictureCacheBytes(), cloned->GetPictureCacheBytes());
}
TEST(FrameTimingsRecorderTest, ClonedHasSameRasterEndWithCache) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
MockRasterCache cache(1, 10);
cache.BeginFrame();
const auto now = fml::TimePoint::Now();
recorder->RecordVsync(now, now + fml::TimeDelta::FromMilliseconds(16));
recorder->RecordBuildStart(fml::TimePoint::Now());
recorder->RecordBuildEnd(fml::TimePoint::Now());
recorder->RecordRasterStart(fml::TimePoint::Now());
cache.AddMockLayer(100, 100);
size_t layer_bytes = cache.EstimateLayerCacheByteSize();
EXPECT_GT(layer_bytes, 0u);
cache.AddMockPicture(100, 100);
size_t picture_bytes = cache.EstimatePictureCacheByteSize();
EXPECT_GT(picture_bytes, 0u);
cache.EvictUnusedCacheEntries();
cache.EndFrame();
recorder->RecordRasterEnd(&cache);
auto cloned = recorder->CloneUntil(FrameTimingsRecorder::State::kRasterEnd);
ASSERT_EQ(recorder->GetFrameNumber(), cloned->GetFrameNumber());
ASSERT_EQ(recorder->GetVsyncStartTime(), cloned->GetVsyncStartTime());
ASSERT_EQ(recorder->GetVsyncTargetTime(), cloned->GetVsyncTargetTime());
ASSERT_EQ(recorder->GetBuildStartTime(), cloned->GetBuildStartTime());
ASSERT_EQ(recorder->GetBuildEndTime(), cloned->GetBuildEndTime());
ASSERT_EQ(recorder->GetRasterStartTime(), cloned->GetRasterStartTime());
ASSERT_EQ(recorder->GetRasterEndTime(), cloned->GetRasterEndTime());
ASSERT_EQ(recorder->GetRasterEndWallTime(), cloned->GetRasterEndWallTime());
ASSERT_EQ(recorder->GetLayerCacheCount(), cloned->GetLayerCacheCount());
ASSERT_EQ(recorder->GetLayerCacheBytes(), cloned->GetLayerCacheBytes());
ASSERT_EQ(recorder->GetPictureCacheCount(), cloned->GetPictureCacheCount());
ASSERT_EQ(recorder->GetPictureCacheBytes(), cloned->GetPictureCacheBytes());
}
TEST(FrameTimingsRecorderTest, FrameNumberTraceArgIsValid) {
auto recorder = std::make_unique<FrameTimingsRecorder>();
char buff[50];
sprintf(buff, "%d", static_cast<int>(recorder->GetFrameNumber()));
std::string actual_arg = buff;
std::string expected_arg = recorder->GetFrameNumberTraceArg();
ASSERT_EQ(actual_arg, expected_arg);
}
} // namespace testing
} // namespace flutter