-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
main-thread-tasks-test.js
239 lines (209 loc) · 9.34 KB
/
main-thread-tasks-test.js
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
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
/* eslint-env jest */
const MainThreadTasks = require('../../computed/main-thread-tasks.js');
const taskGroups = require('../../lib/task-groups.js').taskGroups;
const pwaTrace = require('../fixtures/traces/progressive-app.json');
const TracingProcessor = require('../../lib/traces/tracing-processor.js');
const assert = require('assert');
describe('MainResource computed artifact', () => {
const pid = 1;
const tid = 2;
const frameId = 'BLAH';
const args = {data: {}, frame: frameId};
const baseTs = 1241250325;
let boilerplateTrace;
beforeEach(() => {
boilerplateTrace = [
{ph: 'I', name: 'TracingStartedInPage', pid, tid, ts: baseTs, args: {data: {page: frameId}}},
{ph: 'I', name: 'navigationStart', pid, tid, ts: baseTs, args},
{ph: 'R', name: 'firstContentfulPaint', pid, tid, ts: baseTs + 1, args},
];
});
it('should get all main thread tasks from a trace', async () => {
const context = {computedCache: new Map()};
const tasks = await MainThreadTasks.request({traceEvents: pwaTrace}, context);
const toplevelTasks = tasks.filter(task => !task.parent);
assert.equal(tasks.length, 2305);
assert.equal(toplevelTasks.length, 296);
// Sanity check the reachability of tasks and summation of selfTime
const allTasks = [];
const queue = toplevelTasks;
let totalTime = 0;
let totalTopLevelTime = 0;
while (queue.length) {
const task = queue.shift();
totalTime += task.selfTime;
totalTopLevelTime += TracingProcessor.isScheduleableTask(task.event) ? task.duration : 0;
allTasks.push(task);
queue.push(...task.children);
}
assert.equal(allTasks.length, 2305);
assert.equal(Math.round(totalTopLevelTime), 386);
assert.equal(Math.round(totalTime), 396);
});
it('should compute parent/child correctly', async () => {
/*
An artistic rendering of the below trace:
█████████████████████████████TaskA██████████████████████████████████████████████
████████████████TaskB███████████████████
████TaskC██████
*/
const traceEvents = [
...boilerplateTrace,
{ph: 'X', name: 'TaskA', pid, tid, ts: baseTs, dur: 100e3, args},
{ph: 'B', name: 'TaskB', pid, tid, ts: baseTs + 5e3, args},
{ph: 'X', name: 'TaskC', pid, tid, ts: baseTs + 10e3, dur: 30e3, args},
{ph: 'E', name: 'TaskB', pid, tid, ts: baseTs + 55e3, args},
];
traceEvents.forEach(evt => Object.assign(evt, {cat: 'devtools.timeline'}));
const context = {computedCache: new Map()};
const tasks = await MainThreadTasks.request({traceEvents}, context);
assert.equal(tasks.length, 3);
const taskA = tasks.find(task => task.event.name === 'TaskA');
const taskB = tasks.find(task => task.event.name === 'TaskB');
const taskC = tasks.find(task => task.event.name === 'TaskC');
assert.deepStrictEqual(taskA, {
parent: undefined,
attributableURLs: [],
children: [taskB],
event: traceEvents[3],
startTime: 0,
endTime: 100,
duration: 100,
selfTime: 50,
group: taskGroups.other,
});
assert.deepStrictEqual(taskB, {
parent: taskA,
attributableURLs: [],
children: [taskC],
event: traceEvents[4],
startTime: 5,
endTime: 55,
duration: 50,
selfTime: 20,
group: taskGroups.other,
});
});
it('should compute attributableURLs correctly', async () => {
const baseTs = 1241250325;
const url = s => ({args: {data: {url: s}}});
const stackFrames = f => ({args: {data: {stackTrace: f.map(url => ({url}))}}});
/*
An artistic rendering of the below trace:
█████████████████████████████TaskA██████████████████████████████████████████████
████████████████TaskB███████████████████
████EvaluateScript██████
█D█
*/
const traceEvents = [
...boilerplateTrace,
{ph: 'X', name: 'TaskA', pid, tid, ts: baseTs, dur: 100e3, ...url('about:blank')},
{ph: 'B', name: 'TaskB', pid, tid, ts: baseTs + 5e3, ...stackFrames(['urlB.1', 'urlB.2'])},
{ph: 'X', name: 'EvaluateScript', pid, tid, ts: baseTs + 10e3, dur: 30e3, ...url('urlC')},
{ph: 'X', name: 'TaskD', pid, tid, ts: baseTs + 15e3, dur: 5e3, ...stackFrames(['urlD'])},
{ph: 'E', name: 'TaskB', pid, tid, ts: baseTs + 55e3},
];
traceEvents.forEach(evt => {
evt.cat = 'devtools.timeline';
evt.args = evt.args || args;
});
const context = {computedCache: new Map()};
const tasks = await MainThreadTasks.request({traceEvents}, context);
const taskA = tasks.find(task => task.event.name === 'TaskA');
const taskB = tasks.find(task => task.event.name === 'TaskB');
const taskC = tasks.find(task => task.event.name === 'EvaluateScript');
const taskD = tasks.find(task => task.event.name === 'TaskD');
assert.deepStrictEqual(taskA.attributableURLs, []);
assert.deepStrictEqual(taskB.attributableURLs, ['urlB.1', 'urlB.2']);
assert.deepStrictEqual(taskC.attributableURLs, ['urlB.1', 'urlB.2', 'urlC']);
assert.deepStrictEqual(taskD.attributableURLs, ['urlB.1', 'urlB.2', 'urlC', 'urlD']);
});
it('should handle the last trace event not ending', async () => {
/*
An artistic rendering of the below trace:
█████████████████████████████TaskA████████████|
████████████████TaskB███████████████████|
█TaskC|
^ trace abruptly ended
*/
const traceEvents = [
...boilerplateTrace,
// These events would normally be accompanied by an 'E' event
{ph: 'B', name: 'TaskA', pid, tid, ts: baseTs, args},
{ph: 'B', name: 'TaskB', pid, tid, ts: baseTs + 5e3, args},
{ph: 'B', name: 'TaskC', pid, tid, ts: baseTs + 100e3, args},
];
traceEvents.forEach(evt => Object.assign(evt, {cat: 'devtools.timeline'}));
const context = {computedCache: new Map()};
const tasks = await MainThreadTasks.request({traceEvents}, context);
expect(tasks).toHaveLength(3);
const taskA = tasks.find(task => task.event.name === 'TaskA');
const taskB = tasks.find(task => task.event.name === 'TaskB');
const taskC = tasks.find(task => task.event.name === 'TaskC');
expect(taskA).toEqual({
parent: undefined,
attributableURLs: [],
children: [taskB],
event: traceEvents[3],
startTime: 0,
endTime: 100,
duration: 100,
selfTime: 5,
group: taskGroups.other,
});
expect(taskB).toEqual({
parent: taskA,
attributableURLs: [],
children: [taskC],
event: traceEvents[4],
startTime: 5,
endTime: 100,
duration: 95,
selfTime: 95,
group: taskGroups.other,
});
});
const invalidEventSets = [
[
// TaskA overlaps with TaskB, X first
{ph: 'X', name: 'TaskA', pid, tid, ts: baseTs, dur: 100e3, args},
{ph: 'B', name: 'TaskB', pid, tid, ts: baseTs + 5e3, args},
{ph: 'E', name: 'TaskB', pid, tid, ts: baseTs + 115e3, args},
],
[
// TaskA overlaps with TaskB, B first
{ph: 'B', name: 'TaskA', pid, tid, ts: baseTs, args},
{ph: 'X', name: 'TaskB', pid, tid, ts: baseTs + 5e3, dur: 100e3, args},
{ph: 'E', name: 'TaskA', pid, tid, ts: baseTs + 90e3, args},
],
[
// TaskA is missing a B event
{ph: 'E', name: 'TaskA', pid, tid, ts: baseTs, args},
{ph: 'B', name: 'TaskB', pid, tid, ts: baseTs + 5e3, args},
{ph: 'E', name: 'TaskB', pid, tid, ts: baseTs + 115e3, args},
],
[
// TaskB is missing a B event after an X
{ph: 'X', name: 'TaskA', pid, tid, ts: baseTs, dur: 100e3, args},
{ph: 'E', name: 'TaskB', pid, tid, ts: baseTs + 10e3, args},
],
];
for (const invalidEvents of invalidEventSets) {
it('should throw on invalid task input', async () => {
const traceEvents = [
...boilerplateTrace,
...invalidEvents,
];
traceEvents.forEach(evt => Object.assign(evt, {cat: 'devtools.timeline'}));
const context = {computedCache: new Map()};
const promise = MainThreadTasks.request({traceEvents}, context);
await expect(promise).rejects.toBeTruthy();
});
}
});