forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile-tree.test.js
694 lines (644 loc) · 21.1 KB
/
profile-tree.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/* 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 {
getProfileFromTextSamples,
getMergedProfileFromTextSamples,
} from '../fixtures/profiles/processed-profile';
import {
getCallTree,
computeCallTreeCountsAndSummary,
} from '../../profile-logic/call-tree';
import { getRootsAndChildren } from '../../profile-logic/flame-graph';
import {
getCallNodeInfo,
invertCallstack,
getCallNodeIndexFromPath,
getOriginAnnotationForFunc,
filterThreadSamplesToRange,
} from '../../profile-logic/profile-data';
import { resourceTypes } from '../../profile-logic/data-structures';
import {
callTreeFromProfile,
formatTree,
formatTreeIncludeCategories,
} from '../fixtures/utils';
import { ensureExists } from 'firefox-profiler/utils/flow';
describe('unfiltered call tree', function () {
// These values are hoisted at the top for the ease of access. In the profile fixture
// for the unfiltered call tree, the indexes for funcs, frames, stacks, and callNodes
// all happen to share the same indexes as there is a 1 to 1 relationship between them.
const A = 0;
const B = 1;
const C = 2;
const D = 3;
const E = 4;
const F = 5;
const G = 6;
const H = 7;
const I = 8;
function getProfile() {
return getProfileFromTextSamples(`
A A A
B B B
C C H
D F I
E G
`).profile;
}
/**
* Before creating a CallTree instance some timings are pre-computed.
* This test ensures that these generated values are correct.
*/
describe('computed counts and timings', function () {
const profile = getProfile();
const [thread] = profile.threads;
const defaultCategory = ensureExists(
profile.meta.categories,
'Expected to find categories'
).findIndex((c) => c.name === 'Other');
const callNodeInfo = getCallNodeInfo(
thread.stackTable,
thread.frameTable,
thread.funcTable,
defaultCategory
);
it('yields expected results', function () {
expect(
computeCallTreeCountsAndSummary(
thread.samples,
callNodeInfo,
profile.meta.interval,
false
)
).toEqual({
rootCount: 1,
rootTotalSummary: 3,
callNodeChildCount: new Uint32Array([1, 2, 2, 1, 0, 1, 0, 1, 0]),
callNodeSummary: {
self: new Float32Array([0, 0, 0, 0, 1, 0, 1, 0, 1]),
total: new Float32Array([3, 3, 2, 1, 1, 1, 1, 1, 1]),
},
});
});
});
describe('roots and children for flame graph', function () {
const profile = getProfile();
const [thread] = profile.threads;
const defaultCategory = ensureExists(
profile.meta.categories,
'Expected to find categories'
).findIndex((c) => c.name === 'Other');
const callNodeInfo = getCallNodeInfo(
thread.stackTable,
thread.frameTable,
thread.funcTable,
defaultCategory
);
it('returns roots and children', function () {
expect(
getRootsAndChildren(
thread,
callNodeInfo.callNodeTable,
new Uint32Array([1, 2, 2, 1, 0, 1, 0, 1, 0]),
new Float32Array([3, 3, 2, 1, 1, 1, 1, 1, 1])
)
).toEqual({
roots: [0],
children: {
array: new Uint32Array([1, 7, 2, 5, 3, 4, 6, 8, 0]),
offsets: new Uint32Array([0, 1, 3, 5, 6, 6, 7, 7, 8, 8]),
},
});
});
});
/**
* Explicitly test the structure of the unfiltered call tree.
*/
describe('computed structure', function () {
/**
* The profile samples have the following structure:
*
* A -> A -> A
* | | |
* v v v
* B B B
* | | |
* v v v
* C C H
* | | |
* v v v
* D F I
* | |
* v v
* E G
*
* Assert this form for each node where (FuncName:Total,Self)
*
* A:3,0
* |
* v
* B:3,0
* / \
* v v
* C:2,0 H:1,0
* / \ \
* v v v
* D:1,0 F:1,0 I:1,1
* | |
* v v
* E:1,1 G:1,1
*/
const { profile } = getProfileFromTextSamples(`
A A A
B B B
C C H
D F I
E G
`);
const callTree = callTreeFromProfile(profile);
it('computes an unfiltered call tree', function () {
expect(formatTree(callTree)).toEqual([
'- A (total: 3, self: —)',
' - B (total: 3, self: —)',
' - C (total: 2, self: —)',
' - D (total: 1, self: —)',
' - E (total: 1, self: 1)',
' - F (total: 1, self: —)',
' - G (total: 1, self: 1)',
' - H (total: 1, self: —)',
' - I (total: 1, self: 1)',
]);
});
});
/**
* The same as the previous test, but with categories
*/
describe('computed structure with categories', function () {
/**
* Test that the category of a frame gets inherited down into its subtree
* of the call tree, until reaching a frame that has an explicit category.
*/
const { profile } = getProfileFromTextSamples(`
A A A
B[cat:DOM] B[cat:DOM] B[cat:DOM]
C C H
D F[cat:Graphics] I[cat:Other]
E G
`);
const callTree = callTreeFromProfile(profile);
it('computes an unfiltered call tree', function () {
expect(formatTreeIncludeCategories(callTree)).toEqual([
'- A [Other] (total: 3, self: —)',
' - B [DOM] (total: 3, self: —)',
' - C [DOM] (total: 2, self: —)',
' - D [DOM] (total: 1, self: —)',
' - E [DOM] (total: 1, self: 1)',
' - F [Graphics] (total: 1, self: —)',
' - G [Graphics] (total: 1, self: 1)',
' - H [DOM] (total: 1, self: —)',
' - I [Other] (total: 1, self: 1)',
]);
});
});
/**
* These tests provides coverage for every method of the CallTree. It's less about
* correct tree structure, and more of simple assertions about how the interface
* is supposed to behave. There is probably duplication of coverage with other tests.
*/
describe('CallTree methods', function () {
const { profile } = getProfileFromTextSamples(`
A A A
B B B
C C H
D F I[lib:libI.so]
E G
`);
const callTree = callTreeFromProfile(profile);
describe('getRoots()', function () {
it('returns an array with the root indexes', function () {
expect(callTree.getRoots()).toEqual([A]);
});
});
describe('getChildren()', function () {
it('returns an array with the children indexes', function () {
expect(callTree.getChildren(C)).toEqual([D, F]);
expect(callTree.getChildren(E)).toEqual([]);
});
});
describe('hasChildren()', function () {
it('determines if nodes have children', function () {
expect(callTree.hasChildren(C)).toEqual(true);
expect(callTree.hasChildren(E)).toEqual(false);
});
});
describe('getAllDescendants()', function () {
it('returns a set with the descendant indexes', function () {
expect(callTree.getAllDescendants(C)).toEqual(new Set([D, E, F, G]));
expect(callTree.getAllDescendants(E)).toEqual(new Set([]));
});
});
describe('getParent()', function () {
it("finds a callNode's parent", function () {
expect(callTree.getParent(A)).toBe(-1);
expect(callTree.getParent(B)).toBe(A);
});
});
describe('getDepth()', function () {
it('returns the depth of callNodes in the tree', function () {
expect(callTree.getDepth(A)).toBe(0);
expect(callTree.getDepth(B)).toBe(1);
});
});
describe('hasSameNodeIds()', function () {
it('determines if the node IDs are the same between two trees', function () {
// This is tested through strict equality, so re-generating callNodes is
// the only thing this method expects.
const otherTree = callTreeFromProfile(
getProfileFromTextSamples('A').profile
);
expect(callTree.hasSameNodeIds(callTree)).toBe(true);
expect(callTree.hasSameNodeIds(otherTree)).toBe(false);
});
});
describe('getNodeData()', function () {
it('gets a node for a given callNodeIndex', function () {
expect(callTree.getNodeData(A)).toEqual({
funcName: 'A',
total: 3,
totalRelative: 1,
self: 0,
selfRelative: 0,
});
});
});
describe('getDisplayData()', function () {
it('gets a node for a given callNodeIndex', function () {
expect(callTree.getDisplayData(A)).toEqual({
ariaLabel:
'A, running count is 3 samples (100%), self count is 0 samples',
isFrameLabel: true,
iconSrc: null,
icon: null,
lib: '',
name: 'A',
self: '—',
selfWithUnit: '—',
total: '3',
totalWithUnit: '3 samples',
totalPercent: '100%',
categoryColor: 'grey',
categoryName: 'Other',
});
expect(callTree.getDisplayData(I)).toEqual({
ariaLabel:
'I, running count is 1 sample (33%), self count is 1 sample',
isFrameLabel: false,
iconSrc: null,
icon: null,
lib: 'libI.so',
name: 'I',
self: '1',
selfWithUnit: '1 sample',
total: '1',
totalWithUnit: '1 sample',
totalPercent: '33%',
categoryColor: 'grey',
categoryName: 'Other',
});
});
});
describe('icons from the call tree', function () {
it('upgrades http to https', function () {
const { profile } = getProfileFromTextSamples(`
A[lib:examplecom.js]
`);
const callTree = callTreeFromProfile(profile);
const [thread] = profile.threads;
const hostStringIndex =
thread.stringTable.indexForString('examplecom.js');
thread.resourceTable.type[0] = resourceTypes.webhost;
thread.resourceTable.host[0] = hostStringIndex;
// Hijack the string table to provide the proper host name
thread.stringTable._array[hostStringIndex] = 'http://example.com';
expect(callTree.getDisplayData(A).icon).toEqual(
'https://example.com/favicon.ico'
);
});
});
});
/**
* While not specifically part of the call tree, this is a core function
* to help navigate stacks through a list of functions.
*/
describe('getCallNodeIndexFromPath', function () {
const profile = getProfile();
const [thread] = profile.threads;
const defaultCategory = ensureExists(
profile.meta.categories,
'Expected to find categories'
).findIndex((c) => c.name === 'Other');
const { callNodeTable } = getCallNodeInfo(
thread.stackTable,
thread.frameTable,
thread.funcTable,
defaultCategory
);
// Helper to make the assertions a little less verbose.
function checkStack(callNodePath, index, name) {
it(`finds stack that ends in ${name}`, function () {
expect(getCallNodeIndexFromPath(callNodePath, callNodeTable)).toBe(
index
);
});
}
checkStack([A], A, 'A');
checkStack([A, B], B, 'B');
checkStack([A, B, C], C, 'C');
checkStack([A, B, C, D], D, 'D');
checkStack([A, B, C, D, E], E, 'E');
checkStack([A, B, C, F], F, 'F');
checkStack([A, B, C, F, G], G, 'G');
checkStack([A, B, H], H, 'H');
checkStack([A, B, H, I], I, 'I');
it(`doesn't find a non-existent stack`, function () {
expect(
getCallNodeIndexFromPath([A, B, C, D, E, F, G], callNodeTable)
).toBe(null);
});
});
});
describe('inverted call tree', function () {
/**
* Explicitly test the structure of the inverted call tree.
*/
describe('computed structure', function () {
const profile = getProfileFromTextSamples(`
A A A
B[cat:DOM] B[cat:DOM] B[cat:DOM]
C[cat:Graphics] X C[cat:Graphics]
D[cat:Other] Y X
E Z Y
Z
`).profile;
const { interval } = profile.meta;
const categories = ensureExists(
profile.meta.categories,
'Expected to find categories'
);
const defaultCategory = categories.findIndex((c) => c.color === 'grey');
// Check the non-inverted tree first.
const thread = profile.threads[0];
const callNodeInfo = getCallNodeInfo(
thread.stackTable,
thread.frameTable,
thread.funcTable,
defaultCategory
);
const callTreeCountsAndSummary = computeCallTreeCountsAndSummary(
thread.samples,
callNodeInfo,
interval,
true
);
const callTree = getCallTree(
thread,
interval,
callNodeInfo,
categories,
'combined',
callTreeCountsAndSummary,
'samples'
);
it('computes an non-inverted call tree', function () {
expect(formatTreeIncludeCategories(callTree)).toEqual([
'- A [Other] (total: 3, self: 3)',
' - B [DOM] (total: 3, self: —)',
' - C [Graphics] (total: 2, self: —)',
' - D [Other] (total: 1, self: —)',
' - E [Other] (total: 1, self: —)',
' - X [Graphics] (total: 1, self: —)',
' - Y [Graphics] (total: 1, self: —)',
' - Z [Graphics] (total: 1, self: —)',
' - X [DOM] (total: 1, self: —)',
' - Y [DOM] (total: 1, self: —)',
' - Z [DOM] (total: 1, self: —)',
]);
});
// Now compute the inverted tree and check it.
const invertedThread = invertCallstack(thread, defaultCategory);
const invertedCallNodeInfo = getCallNodeInfo(
invertedThread.stackTable,
invertedThread.frameTable,
invertedThread.funcTable,
defaultCategory
);
const invertedCallTreeCountsAndSummary = computeCallTreeCountsAndSummary(
invertedThread.samples,
invertedCallNodeInfo,
interval,
true
);
const invertedCallTree = getCallTree(
invertedThread,
interval,
invertedCallNodeInfo,
categories,
'combined',
invertedCallTreeCountsAndSummary,
'samples'
);
/**
* Assert this tree form for each node.
*
* E:1,1 Z:2,2
* | |
* v v
* D:1,0 Y:2,0
* | |
* v v
* C:1,0 X:2,0
* | / \
* v v v
* B:1,0 B:1,0 C:1,0
* | | |
* v v v
* A:1,0 A:1,0 B:1,0
* |
* v
* A:1,0
*
* ^ ^ ^
* | | |
* L M R <- Label the branches. (left, middle, right)
*
* This test also checks that stacks of conflicting categories end up with
* the "Other" category. In this test, in the non-inverted tree, the nodes
* for the functions X, Y, and Z have different categories depending on
* which subtree they are in: They have category "Graphics" in the subtree
* under C, and the category "DOM" in the subtree that's directly attached
* to B. In the inverted tree, those nodes collapse into each other, and
* their category should be set to "Other".
*/
it('computes an inverted call tree', function () {
expect(formatTreeIncludeCategories(invertedCallTree)).toEqual([
'- Z [Other] (total: 2, self: 2)',
' - Y [Other] (total: 2, self: —)',
' - X [Other] (total: 2, self: —)',
' - B [DOM] (total: 1, self: —)',
' - A [Other] (total: 1, self: —)',
' - C [Graphics] (total: 1, self: —)',
' - B [DOM] (total: 1, self: —)',
' - A [Other] (total: 1, self: —)',
'- E [Other] (total: 1, self: 1)',
' - D [Other] (total: 1, self: —)',
' - C [Graphics] (total: 1, self: —)',
' - B [DOM] (total: 1, self: —)',
' - A [Other] (total: 1, self: —)',
]);
});
});
});
describe('diffing trees', function () {
function getProfile() {
const { profile } = getMergedProfileFromTextSamples(
`
A A A
B B C
D E F
`,
`
A A A
B B B
G I E
`
);
return profile;
}
it('displays a proper call tree, including nodes with total = 0', () => {
const profile = getProfile();
const callTree = callTreeFromProfile(profile, /* threadIndex */ 2);
const formattedTree = formatTree(callTree);
expect(formattedTree).toEqual([
'- A (total: —, self: —)',
' - B (total: 1, self: —)',
' - D (total: -1, self: -1)',
' - G (total: 1, self: 1)',
' - I (total: 1, self: 1)',
' - C (total: -1, self: —)',
' - F (total: -1, self: -1)',
]);
// There's no A -> B -> E node because the diff makes it completely disappear.
expect(formattedTree).not.toContainEqual(expect.stringMatching(/^\s*- E/));
});
it('displays a proper call tree, even for range-filtered threads', () => {
const profile = getProfile();
const rangeStart = 1;
const rangeEnd = 3;
profile.threads = profile.threads.map((thread) =>
filterThreadSamplesToRange(thread, rangeStart, rangeEnd)
);
const callTree = callTreeFromProfile(profile, /* threadIndex */ 2);
const formattedTree = formatTree(callTree);
expect(formattedTree).toEqual([
// A -> B -> D and A -> B -> G should be filtered out by the range filtering.
'- A (total: —, self: —)',
' - B (total: 1, self: —)',
' - I (total: 1, self: 1)',
' - C (total: -1, self: —)',
' - F (total: -1, self: -1)',
]);
});
it('computes a rootTotalSummary that is the absolute count of all intervals', () => {
const profile = getProfile();
const thread = profile.threads[2];
const { interval } = profile.meta;
const defaultCategory = ensureExists(
profile.meta.categories,
'Expected to find categories'
).findIndex((c) => c.name === 'Other');
const callNodeInfo = getCallNodeInfo(
thread.stackTable,
thread.frameTable,
thread.funcTable,
defaultCategory
);
const callTreeCountsAndSummary = computeCallTreeCountsAndSummary(
thread.samples,
callNodeInfo,
interval,
false
);
expect(callTreeCountsAndSummary.rootTotalSummary).toBe(4);
});
});
describe('origin annotation', function () {
const {
profile: {
threads: [thread],
},
funcNamesPerThread: [funcNames],
} = getProfileFromTextSamples(`
A
B
C
D
`);
function addResource(
funcName: string,
name: string,
host: string | null,
location: string | null
) {
const resourceIndex = thread.resourceTable.length;
const funcIndex = funcNames.indexOf(funcName);
thread.funcTable.resource[funcIndex] = resourceIndex;
thread.funcTable.fileName[funcIndex] = location
? thread.stringTable.indexForString(location)
: null;
thread.resourceTable.lib.push(-1);
thread.resourceTable.name.push(thread.stringTable.indexForString(name));
thread.resourceTable.host.push(
host ? thread.stringTable.indexForString(host) : null
);
thread.resourceTable.length++;
}
addResource(
'A',
'http://foobar.com',
'http://foobar.com',
'http://foobar.com/script.js'
);
addResource(
'B',
'Extension "Gecko Profiler"',
'moz-extension://bf3bb73c-919c-4fef-95c4-070a19fdaf85',
'moz-extension://bf3bb73c-919c-4fef-95c4-070a19fdaf85/script.js'
);
addResource('C', 'libxul.so', null, '/home/user/mozilla-central/xul.cpp');
addResource('D', 'libxul.so', null, null);
function getOrigin(funcName: string): string {
return getOriginAnnotationForFunc(
funcNames.indexOf(funcName),
thread.funcTable,
thread.resourceTable,
thread.stringTable
);
}
it('formats web origins correctly', function () {
expect(getOrigin('A')).toEqual('http://foobar.com/script.js');
});
it('formats extension origins correctly', function () {
expect(getOrigin('B')).toEqual(
'Extension "Gecko Profiler": ' +
'moz-extension://bf3bb73c-919c-4fef-95c4-070a19fdaf85/script.js'
);
});
it('formats library origins correctly', function () {
expect(getOrigin('C')).toEqual(
'libxul.so: /home/user/mozilla-central/xul.cpp'
);
expect(getOrigin('D')).toEqual('libxul.so');
});
});