forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-tracer.test.js
467 lines (434 loc) · 13.6 KB
/
js-tracer.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import { storeWithProfile } from '../fixtures/stores';
import { selectedThreadSelectors } from '../../selectors/per-thread';
import { ensureExists } from '../../utils/flow';
import { changeShowJsTracerSummary } from '../../actions/profile-view';
import {
convertJsTracerToThread,
getJsTracerFixed,
} from '../../profile-logic/js-tracer';
import { getEmptyProfile } from '../../profile-logic/data-structures';
import { formatTree } from '../fixtures/utils';
import {
getProfileFromTextSamples,
getProfileWithJsTracerEvents,
type TestDefinedJsTracerEvent,
} from '../fixtures/profiles/processed-profile';
import type { Profile } from 'firefox-profiler/types';
describe('jsTracerFixed', function () {
function fixTiming(events: TestDefinedJsTracerEvent[]) {
const profile = getProfileWithJsTracerEvents(events);
const jsTracer = ensureExists(profile.threads[0].jsTracer);
const jsTracerFixed = getJsTracerFixed(jsTracer);
return {
start: jsTracerFixed.start,
end: jsTracerFixed.end,
};
}
it('does not modify a valid structure', function () {
const timing = fixTiming([
// [mozilla ]
// [int ][ion ]
// [int] [ion ]
['https://mozilla.org', 0, 20],
['Interpreter', 1, 5],
['Interpreter', 2, 4],
['IonMonkey', 5, 19],
['IonMonkey', 6, 18],
]);
expect(timing).toEqual({
start: [0, 1000, 2000, 5000, 6000],
end: [20000, 5000, 4000, 19000, 18000],
});
});
it('fixes overlapping structures, cutting off the beginning', function () {
const timing = fixTiming([
// [aaaaaa]
// [bbbbbbbbb]
['a', 0, 5],
['b', 4, 9],
]);
expect(timing).toEqual({
// [aaaaaa][bbbbbb]
start: [0, 5000],
end: [5000, 9000],
});
});
it('fixes overlapping structures, cutting off the end', function () {
const timing = fixTiming([
// [aaaaaa]
// [bbbbbb]
['a', 0, 5],
['b', 1, 6],
]);
expect(timing).toEqual({
// [aaaaaa][bbbbbb]
start: [0, 1000],
end: [5000, 5000],
});
});
it('fixes events which are too early', function () {
const timing = fixTiming([
// [aaaaaa]
// [bbbbbb]
['a', 3, 8],
['b', 1, 6],
]);
expect(timing).toEqual({
// [aaaaaa]
// [bbb]
start: [3000, 3000],
end: [8000, 6000],
});
});
it('fixes events which are way too early', function () {
const timing = fixTiming([
// [aaaa]
// [bbb]
['a', 5, 9],
['b', 0, 2],
]);
expect(timing).toEqual({
// [aaaaaa]
// [bbb]
start: [5000, 5000],
end: [9000, 7000],
});
});
});
describe('convertJsTracerToThread', function () {
it('can generate stacks correctly', function () {
const existingProfile = getProfileWithJsTracerEvents([
// [mozilla ]
// [int ][ion ]
// [int] [ion ]
['https://mozilla.org', 0, 20],
['Interpreter', 1, 5],
['Interpreter', 2, 4],
['IonMonkey', 5, 19],
['IonMonkey', 6, 18],
]);
const existingThread = existingProfile.threads[0];
const categories = ensureExists(
existingProfile.meta.categories,
'Expected to find categories.'
);
const profile = getEmptyProfile();
const jsTracer = ensureExists(existingThread.jsTracer);
profile.threads = [
convertJsTracerToThread(existingThread, jsTracer, categories),
];
const { getState } = storeWithProfile(profile);
const callTree = selectedThreadSelectors.getCallTree(getState());
expect(formatTree(callTree)).toEqual([
// This is the real timing:
'- https://mozilla.org (total: 20, self: 2)',
' - Interpreter (total: 18, self: 2)',
' - IonMonkey (total: 14, self: 2)',
' - IonMonkey (total: 12, self: 12)',
' - Interpreter (total: 2, self: 2)',
]);
});
it('can generate the frameTable implementations correctly', function () {
const existingProfile = getProfileWithJsTracerEvents([
// [mozilla ]
// [int ][ion ]
// [A ] [B ]
['https://mozilla.org', 0, 20],
['Interpreter', 1, 5],
['FuncA', 2, 4],
['IonMonkey', 5, 19],
['FuncB', 6, 18],
]);
const existingThread = existingProfile.threads[0];
const categories = ensureExists(
existingProfile.meta.categories,
'Expected to find categories'
);
const jsTracer = ensureExists(existingThread.jsTracer);
const thread = convertJsTracerToThread(
existingThread,
jsTracer,
categories
);
const implementationNames = thread.frameTable.implementation.map(
(implementation) =>
implementation === null
? null
: thread.stringTable.getString(implementation)
);
expect(implementationNames).toEqual([
null, // 'https://mozilla.org'
'interpreter', // 'Interpreter'
'interpreter', // 'FuncA'
'ion', // 'IonMonkey'
'ion', // 'FuncB'
]);
});
});
describe('selectors/getJsTracerTiming', function () {
describe('full stack-based view', function () {
it('has no JS tracer timing if no js tracer info is present', function () {
const { profile } = getProfileFromTextSamples('A');
const { getState } = storeWithProfile(profile);
expect(profile.threads[0].jsTracer).toBe(undefined);
expect(
selectedThreadSelectors.getExpensiveJsTracerTiming(getState())
).toEqual(null);
});
it('has empty JS tracer timing if no events are in the js tracer table', function () {
expect(
getHumanReadableJsTracerTiming({ useSelfTime: false, events: [] })
).toEqual([]);
});
it('can generate some simple nested timing', function () {
expect(
getHumanReadableJsTracerTiming({
useSelfTime: false,
events: [
['https://mozilla.org', 0, 20],
['Interpreter', 1, 19],
['IonMonkey', 2, 18],
],
})
).toEqual([
'https://mozilla.org (0:20)',
'Interpreter (1:19)',
'IonMonkey (2:18)',
]);
});
it('can generate sibling timing', function () {
expect(
getHumanReadableJsTracerTiming({
useSelfTime: false,
events: [
['https://mozilla.org', 0, 20],
['Interpreter', 1, 5],
['Interpreter', 2, 4],
['IonMonkey', 5, 19],
['IonMonkey', 6, 18],
],
})
).toEqual([
'https://mozilla.org (0:20)',
'Interpreter (1:5) | IonMonkey (5:19)',
'Interpreter (2:4) | IonMonkey (6:18)',
]);
});
});
describe('self time', function () {
it('has empty JS tracer timing if no events are in the js tracer table', function () {
expect(
getHumanReadableJsTracerTiming({ useSelfTime: true, events: [] })
).toEqual([]);
});
it('can generate some simple nested timing', function () {
expect(
getHumanReadableJsTracerTiming({
useSelfTime: true,
events: [
['https://mozilla.org', 0, 20],
['Interpreter', 1, 19],
['IonMonkey', 2, 18],
],
})
).toEqual([
'Interpreter (1:2) | Interpreter (18:19)',
'IonMonkey (2:18)',
'https://mozilla.org (0:1) | https://mozilla.org (19:20)',
]);
});
it('can "drain off" prefixes the first if branch of the algorithm', function () {
// This test is checking a specific if branch of the timing function:
//
// xxxxxxxxxxxxxxxx[================]
// xxxxxxxx[======] [current]
// [prefix]
expect(
getHumanReadableJsTracerTiming({
useSelfTime: true,
events: [
// This comment makes the formatting "prettier".
['A', 0, 2],
['B', 0, 2],
['C', 0, 1],
['A', 2, 3],
],
})
).toEqual([
// This comment makes the formatting "prettier".
'A (2:3)',
'B (1:2)',
'C (0:1)',
]);
});
it('handles float precision errors where the child event outlasts the parent', function () {
// 0 1 2 3 4 5
// [prefix--]
// [current-]
// 0 1 2 3 4 5
// [A-------]
// [B-------]
expect(
getHumanReadableJsTracerTiming({
useSelfTime: true,
events: [
// This comment makes the formatting "prettier".
['A', 0, 3],
['B', 1, 4],
],
})
).toEqual([
// This comment makes the formatting "prettier".
'A (0:1)',
'B (1:3)',
]);
});
it('can split a prefix stack if the ending time matches', function () {
// This test is checking a specific if branch of the timing function:
//
// [prefix]xxxxxxxxx
// [current]
expect(
getHumanReadableJsTracerTiming({
useSelfTime: true,
events: [
// This comment makes the formatting "prettier".
['A', 0, 2],
['B', 1, 2],
],
})
).toEqual([
// This comment makes the formatting "prettier".
'A (0:1)',
'B (1:2)',
]);
});
});
describe('match function names', function () {
it('works on the sampled data', function () {
// Create a profile from text samples.
const {
profile,
funcNamesDictPerThread: [funcNamesDict],
} = getProfileFromTextSamples(`
Foo.js
Bar.js
Baz.js
`);
{
// Now here comes the fun part. Manually manipulate the data so that the profile
// has matching JS tracer information, such that we can deduce functions from
// event names.
const thread = profile.threads[0];
// Also create a JS tracer profile.
const { stringTable: tracerStringTable, jsTracer } =
getProfileWithJsTracerEvents([
['Root', 0, 20],
['Node', 1, 19],
['https://mozilla.org', 2, 18],
['https://mozilla.org', 3, 16],
]).threads[0];
if (!jsTracer) {
throw new Error('Unable to find a JS tracer table');
}
// Merge the js tracer data into the profile.
thread.jsTracer = jsTracer;
for (
let jsTracerIndex = 0;
jsTracerIndex < jsTracer.length;
jsTracerIndex++
) {
// Map the old string to the new string.
jsTracer.events[jsTracerIndex] = thread.stringTable.indexForString(
tracerStringTable.getString(jsTracer.events[jsTracerIndex])
);
}
const foo = funcNamesDict['Foo.js'];
const fooLine = 3;
const fooColumn = 5;
thread.funcTable.lineNumber[foo] = fooLine;
thread.funcTable.columnNumber[foo] = fooColumn;
thread.funcTable.fileName[foo] = thread.stringTable.indexForString(
'https://mozilla.org'
);
const bar = funcNamesDict['Bar.js'];
const barLine = 7;
const barColumn = 11;
thread.funcTable.lineNumber[bar] = barLine;
thread.funcTable.columnNumber[bar] = barColumn;
thread.funcTable.fileName[bar] = thread.stringTable.indexForString(
'https://mozilla.org'
);
const baz = funcNamesDict['Baz.js'];
// Use bar's line and column information.
thread.funcTable.lineNumber[baz] = barLine;
thread.funcTable.columnNumber[baz] = barColumn;
thread.funcTable.fileName[baz] = thread.stringTable.indexForString(
'https://mozilla.org'
);
// Manually update the JS tracer events to point to the right column numbers.
jsTracer.line[2] = fooLine;
jsTracer.column[2] = fooColumn;
jsTracer.line[3] = barLine;
jsTracer.column[3] = barColumn;
}
expect(
getHumanReadableJsTracerTiming({
useSelfTime: false,
profile,
})
).toEqual([
'Root (0:20)',
'Node (1:19)',
'ƒ Foo.js https://mozilla.org (2:18)',
'(multiple matching functions) https://mozilla.org (3:16)',
]);
});
});
});
/**
* Create JS tracing information from a list of events, and then compute a human
* readable representation of the timing.
*
* "EventName1 (StartTime:EndTime) | EventName2 (StartTime:EndTime)"
*/
function getHumanReadableJsTracerTiming({
useSelfTime,
events,
profile,
}: {|
useSelfTime: boolean,
events?: TestDefinedJsTracerEvent[],
profile?: Profile,
|}): string[] {
if (!profile) {
profile = getProfileWithJsTracerEvents(
ensureExists(
events,
'Expected to have a list of tracer events when no profile was supplied.'
)
);
}
const { dispatch, getState } = storeWithProfile(profile);
const computeTiming = useSelfTime
? selectedThreadSelectors.getExpensiveJsTracerLeafTiming
: selectedThreadSelectors.getExpensiveJsTracerTiming;
dispatch(changeShowJsTracerSummary(useSelfTime));
return ensureExists(computeTiming(getState())).map(
({ start, end, label, length }) => {
const lines = [];
for (let i = 0; i < length; i++) {
lines.push(
`${label[i]} (${parseFloat(start[i].toFixed(2))}:${parseFloat(
end[i].toFixed(2)
)})`
);
}
return lines.join(' | ');
}
);
}