-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathprocess-profile.test.js
868 lines (761 loc) · 30.4 KB
/
process-profile.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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
/* 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 {
extractFuncsAndResourcesFromFrameLocations,
processGeckoProfile,
serializeProfile,
unserializeProfileOfArbitraryFormat,
GlobalDataCollector,
} from '../../profile-logic/process-profile';
import { computeTimeColumnForRawSamplesTable } from '../../profile-logic/profile-data';
import { StringTable } from '../../utils/string-table';
import {
createGeckoProfile,
createGeckoCounter,
createGeckoMarkerStack,
createGeckoProfilerOverhead,
getVisualMetrics,
} from '../fixtures/profiles/gecko-profile';
import { ensureExists } from '../../utils/flow';
import type {
JsAllocationPayload_Gecko,
NativeAllocationPayload_Gecko,
GeckoProfile,
GeckoThread,
GeckoCounter,
GeckoProfilerOverhead,
IndexIntoGeckoStackTable,
Milliseconds,
RawThread,
Pid,
} from 'firefox-profiler/types';
describe('extract functions and resource from location strings', function () {
// These location strings are turned into the proper funcs.
const locations = [
// Extract unsymbolicated memory and match them to libraries.
'0xc0ff33',
'0xe0ff33',
// Extract C++ function names and resources
'cppFunction1 (in c++ resource name1) + 123',
'cppFunction2 (in c++ resource name2) (234:345)',
'cppFunction3 (in c++ resource name2)',
// Extract JS functions URL information
'jsFunction1 (http://script.com/one.js:456:1)',
'http://script.com/one.js:456:1',
// Extension locations
'moz-extension://bf3bb73c-919c-4fef-95c4-070a19fdaf85/background.js:1:0',
'moz-extension://fa2edf9c-c45f-4445-b819-c09e3f2d58d5/content.js:1:0',
'backgroundFunction (moz-extension://bf3bb73c-919c-4fef-95c4-070a19fdaf85/background.js:2:1)',
'contentfunction (moz-extension://fa2edf9c-c45f-4445-b819-c09e3f2d58d5/content.js:2:1)',
// Something unknown
'mysterious location',
];
const libs = [
// This library will match the '0xc0ff33' location.
{
start: 0xc00000,
end: 0xd00000,
offset: 0,
arch: '',
name: 'No symbols library',
path: '',
debugName: '',
debugPath: '',
breakpadId: '',
},
// This library will match the '0xe0ff33' location, and it has an offset.
{
start: 0xe01000,
end: 0xf00000,
offset: 0x1000,
arch: '',
name: 'No symbols library',
path: '',
debugName: '',
debugPath: '',
breakpadId: '',
},
];
const stringTable = StringTable.withBackingArray([]);
const locationIndexes = locations.map((location) =>
stringTable.indexForString(location)
);
const extensions = {
baseURL: [
'moz-extension://bf3bb73c-919c-4fef-95c4-070a19fdaf85/',
'moz-extension://fa2edf9c-c45f-4445-b819-c09e3f2d58d5/',
],
id: ['[email protected]', '[email protected]'],
name: ['Gecko Profiler', 'Firefox Screenshots'],
length: 2,
};
const globalDataCollector = new GlobalDataCollector();
it('extracts the information for all different types of locations', function () {
const { funcTable, resourceTable, frameFuncs } =
extractFuncsAndResourcesFromFrameLocations(
locationIndexes,
locationIndexes.map(() => false),
stringTable,
libs,
extensions,
globalDataCollector
);
expect(
frameFuncs.map((funcIndex, locationIndex) => {
// Map all the results into a human readable object for easy snapshotting.
const locationName = locations[locationIndex];
const funcName = stringTable.getString(funcTable.name[funcIndex]);
const resourceIndex = funcTable.resource[funcIndex];
const isJS = funcTable.isJS[funcIndex];
const fileNameIndex = funcTable.fileName[funcIndex];
const fileName =
fileNameIndex === null ? null : stringTable.getString(fileNameIndex);
const lineNumber = funcTable.lineNumber[funcIndex];
const columnNumber = funcTable.columnNumber[funcIndex];
let libIndex, resourceName, host, resourceType;
if (resourceIndex === -1) {
resourceName = null;
host = null;
resourceType = null;
} else {
const hostStringIndex = resourceTable.host[resourceIndex];
libIndex = resourceTable.lib[resourceIndex];
resourceName = stringTable.getString(
resourceTable.name[resourceIndex]
);
host =
hostStringIndex === undefined || hostStringIndex === null
? null
: stringTable.getString(hostStringIndex);
resourceType = resourceTable.type[resourceIndex];
}
const lib =
libIndex === undefined || libIndex === null || libIndex === -1
? undefined
: libs[libIndex];
return [
locationName,
{
funcName,
isJS,
resourceIndex,
fileName,
lineNumber,
columnNumber,
libIndex,
resourceName,
host,
resourceType,
lib,
},
];
})
).toMatchSnapshot();
});
});
describe('gecko counters processing', function () {
function setup(): {|
parentGeckoProfile: GeckoProfile,
parentPid: Pid,
childPid: Pid,
parentCounter: GeckoCounter,
childCounter: GeckoCounter,
|} {
// Create a gecko profile with counters.
const findMainThread = (profile): GeckoThread =>
ensureExists(
profile.threads.find((thread) => thread.name === 'GeckoMain'),
'There should be a GeckoMain thread in the Gecko profile'
);
const parentGeckoProfile = createGeckoProfile();
const [childGeckoProfile] = parentGeckoProfile.processes;
const parentPid = findMainThread(parentGeckoProfile).pid;
const childPid = findMainThread(childGeckoProfile).pid;
expect(parentPid).toEqual(3333);
expect(childPid).toEqual(2222);
const parentCounter = createGeckoCounter(
findMainThread(parentGeckoProfile)
);
const childCounter = createGeckoCounter(findMainThread(childGeckoProfile));
// It's possible that no sample has been collected during our capture
// session, ignore this counter if that's the case.
const emptyCounterEntry = {
name: 'Empty counter',
category: 'Some category',
description: 'Some description',
samples: {
schema: {
time: 0,
count: 1,
number: 2,
},
data: [],
},
};
parentGeckoProfile.counters = [parentCounter, emptyCounterEntry];
childGeckoProfile.counters = [childCounter];
return {
parentGeckoProfile,
parentPid: `${parentPid}`,
childPid: `${childPid}`,
parentCounter,
childCounter,
};
}
it('can extract the counter information correctly', function () {
const { parentGeckoProfile, parentPid, childPid } = setup();
const processedProfile = processGeckoProfile(parentGeckoProfile);
const counters = ensureExists(
processedProfile.counters,
'Expected to find counters on the processed profile'
);
expect(counters.length).toBe(2);
expect(counters[0].pid).toBe(parentPid);
expect(counters[1].pid).toBe(childPid);
const findMainThreadIndexByPid = (pid: Pid): number =>
processedProfile.threads.findIndex(
(thread) => thread.name === 'GeckoMain' && thread.pid === pid
);
expect(counters[0].mainThreadIndex).toBe(
findMainThreadIndexByPid(parentPid)
);
expect(counters[1].mainThreadIndex).toBe(
findMainThreadIndexByPid(childPid)
);
});
it('offsets the counter timing for child processes', function () {
const { parentGeckoProfile, parentCounter, childCounter } = setup();
const processedProfile = processGeckoProfile(parentGeckoProfile);
const processedCounters = ensureExists(processedProfile.counters);
const originalTime = [0, 1, 2, 3, 4, 5, 6];
const offsetTime = originalTime.map((n) => n + 1000);
const extractTime = (counter) => {
if (counter.samples.data.length === 0) {
return [];
}
return counter.samples.data.map((tuple) => tuple[0]);
};
// The original times and parent process are not offset.
expect(extractTime(parentCounter)).toEqual(originalTime);
expect(extractTime(childCounter)).toEqual(originalTime);
expect(
computeTimeColumnForRawSamplesTable(processedCounters[0].samples)
).toEqual(originalTime);
// The subprocess times are offset when processed:
expect(
computeTimeColumnForRawSamplesTable(processedCounters[1].samples)
).toEqual(offsetTime);
});
});
describe('gecko profilerOverhead processing', function () {
function setup(): {|
parentGeckoProfile: GeckoProfile,
parentPid: Pid,
childPid: Pid,
parentOverhead: GeckoProfilerOverhead,
childOverhead: GeckoProfilerOverhead,
|} {
// Create a gecko profile with profilerOverhead.
const findMainThread = (profile) =>
ensureExists(
profile.threads.find((thread) => thread.name === 'GeckoMain'),
'There should be a GeckoMain thread in the Gecko profile'
);
const parentGeckoProfile = createGeckoProfile();
const [childGeckoProfile] = parentGeckoProfile.processes;
const parentPid = findMainThread(parentGeckoProfile).pid;
const childPid = findMainThread(childGeckoProfile).pid;
expect(parentPid).toEqual(3333);
expect(childPid).toEqual(2222);
const parentOverhead = createGeckoProfilerOverhead(
findMainThread(parentGeckoProfile)
);
const childOverhead = createGeckoProfilerOverhead(
findMainThread(childGeckoProfile)
);
parentGeckoProfile.profilerOverhead = parentOverhead;
childGeckoProfile.profilerOverhead = childOverhead;
return {
parentGeckoProfile,
parentPid: `${parentPid}`,
childPid: `${childPid}`,
parentOverhead,
childOverhead,
};
}
it('can extract the overhead information correctly', function () {
const { parentGeckoProfile, parentPid, childPid } = setup();
const processedProfile = processGeckoProfile(parentGeckoProfile);
const overhead = ensureExists(
processedProfile.profilerOverhead,
'Expected to find profilerOverhead on the processed profile'
);
expect(overhead.length).toBe(2);
expect(overhead[0].pid).toBe(parentPid);
expect(overhead[1].pid).toBe(childPid);
const findMainThreadIndexByPid = (pid: Pid): number =>
processedProfile.threads.findIndex(
(thread) => thread.name === 'GeckoMain' && thread.pid === pid
);
expect(overhead[0].mainThreadIndex).toBe(
findMainThreadIndexByPid(parentPid)
);
expect(overhead[1].mainThreadIndex).toBe(
findMainThreadIndexByPid(childPid)
);
});
it('offsets the overhead timing for child processes', function () {
const { parentGeckoProfile, parentOverhead, childOverhead } = setup();
const processedProfile = processGeckoProfile(parentGeckoProfile);
const processedOverheads = ensureExists(processedProfile.profilerOverhead);
const originalTime = [0, 1000, 2000, 3000, 4000, 5000, 6000];
const processedTime = originalTime.map((n) => n / 1000);
const offsetTime = processedTime.map((n) => n + 1000);
const extractTime = (overhead) =>
overhead.samples.data.map((tuple) => tuple[0]);
// The original times are not offset and in microseconds.
expect(extractTime(parentOverhead)).toEqual(originalTime);
expect(extractTime(childOverhead)).toEqual(originalTime);
// The parent process times are not offset and in milliseconds.
expect(processedOverheads[0].samples.time).toEqual(processedTime);
// The subprocess times are offset and in milliseconds when processed
expect(processedOverheads[1].samples.time).toEqual(offsetTime);
});
});
describe('serializeProfile', function () {
it('should produce a parsable profile string', async function () {
const profile = processGeckoProfile(createGeckoProfile());
const serialized = serializeProfile(profile);
expect(JSON.parse.bind(null, serialized)).not.toThrow();
});
it('should produce the same profile in a roundtrip', async function () {
const profile = processGeckoProfile(createGeckoProfile());
const serialized = serializeProfile(profile);
const roundtrip = await unserializeProfileOfArbitraryFormat(serialized);
// FIXME: Uncomment this line after resolving `undefined` serialization issue
// See: https://github.com/firefox-devtools/profiler/issues/1599
// expect(profile).toEqual(roundtrip);
const secondSerialized = serializeProfile(roundtrip);
const secondRountrip =
await unserializeProfileOfArbitraryFormat(secondSerialized);
expect(roundtrip).toEqual(secondRountrip);
});
});
describe('js allocation processing', function () {
function getAllocationMarkerHelper(geckoThread: GeckoThread) {
let time = 0;
return ({ byteSize, stackIndex }) => {
const thisTime = time++;
// Opt out of type checking, due to the schema look-up not being type checkable.
const markerTuple: any = [];
const payload: JsAllocationPayload_Gecko = {
type: 'JS allocation',
className: 'Function',
typeName: 'JSObject',
coarseType: 'Object',
size: byteSize,
inNursery: true,
stack: createGeckoMarkerStack({ stackIndex, time: thisTime }),
};
markerTuple[geckoThread.markers.schema.name] = 'JS allocation';
markerTuple[geckoThread.markers.schema.startTime] = thisTime;
markerTuple[geckoThread.markers.schema.endTime] = null;
markerTuple[geckoThread.markers.schema.phase] = 0;
markerTuple[geckoThread.markers.schema.data] = payload;
markerTuple[geckoThread.markers.schema.category] = 0;
geckoThread.markers.data.push(markerTuple);
};
}
function getFrameAddressesForStack(thread, stackIndex) {
const addresses = [];
let stack = stackIndex;
while (stack !== null) {
addresses.push(thread.frameTable.address[thread.stackTable.frame[stack]]);
stack = thread.stackTable.prefix[stack];
}
addresses.reverse();
return addresses;
}
it('should process JS allocation markers into a JS allocation table', function () {
const geckoProfile = createGeckoProfile();
const geckoThread = geckoProfile.threads[0];
const createAllocation = getAllocationMarkerHelper(geckoThread);
// Verify the test found the parent process' main thread.
expect(geckoThread.name).toBe('GeckoMain');
expect(geckoThread.processType).toBe('default');
// Create 3 allocations, and note the marker lengths.
const originalMarkersLength = geckoThread.markers.data.length;
// Note: the stack indexes used here must exist in the thread's stackTable.
createAllocation({ byteSize: 3, stackIndex: 5 });
createAllocation({ byteSize: 5, stackIndex: 6 });
createAllocation({ byteSize: 7, stackIndex: null });
const markersAndAllocationsLength = geckoThread.markers.data.length;
// Do a simple assertion to verify that the allocations were added by the test
// fixture as expected.
expect(markersAndAllocationsLength).toEqual(originalMarkersLength + 3);
// Process the profile and get out the new thread.
const processedProfile = processGeckoProfile(geckoProfile);
const processedThread = processedProfile.threads[0];
// Check for the existence of the allocations.
const { jsAllocations } = processedThread;
if (!jsAllocations) {
throw new Error('Could not find the jsAllocations on the main thread.');
}
// Assert that the transformation makes sense.
expect(jsAllocations.time).toEqual([0, 1, 2]);
expect(jsAllocations.weight).toEqual([3, 5, 7]);
// All addressses should be nudged by 1 byte, because js allocation stack frames
// all come from stack walking (the instruction pointer frame is removed by gecko).
expect(
getFrameAddressesForStack(processedThread, jsAllocations.stack[0])
).toEqual([-1, 0xf83, 0x1a44, 0x1bcc]);
expect(
getFrameAddressesForStack(processedThread, jsAllocations.stack[1])
).toEqual([-1, 0xf83, 0x1a44, 0x1bcd]);
expect(
getFrameAddressesForStack(processedThread, jsAllocations.stack[2])
).toEqual([]);
});
});
describe('native allocation processing', function () {
type CreateAllocation = ({
byteSize: number,
stackIndex: number | null,
}) => void;
// This helper will create a function that can be used to easily add allocations to
// the GeckoThread.
function getAllocationMarkerHelper(
geckoThread: GeckoThread
): CreateAllocation {
let time = 0;
return ({ byteSize, stackIndex }) => {
const thisTime = time++;
// Opt out of type checking, due to the schema look-up not being type checkable.
const markerTuple: any = [];
const payload: NativeAllocationPayload_Gecko = {
type: 'Native allocation',
size: byteSize,
stack: createGeckoMarkerStack({ stackIndex, time: thisTime }),
};
markerTuple[geckoThread.markers.schema.name] = 'Native allocation';
markerTuple[geckoThread.markers.schema.startTime] = thisTime;
markerTuple[geckoThread.markers.schema.endTime] = null;
markerTuple[geckoThread.markers.schema.phase] = 0;
markerTuple[geckoThread.markers.schema.data] = payload;
markerTuple[geckoThread.markers.schema.category] = 0;
geckoThread.markers.data.push(markerTuple);
};
}
it('should process native allocation markers into a native allocation table', function () {
const geckoProfile = createGeckoProfile();
const geckoThread = geckoProfile.threads[0];
const createAllocation = getAllocationMarkerHelper(geckoThread);
// Verify the test found the parent process' main thread.
expect(geckoThread.name).toBe('GeckoMain');
expect(geckoThread.processType).toBe('default');
// Create 3 allocations, and note the marker lengths.
const originalMarkersLength = geckoThread.markers.data.length;
// Note: the stack indexes used below must exist in the stackTable.
createAllocation({ byteSize: 3, stackIndex: 5 });
createAllocation({ byteSize: 5, stackIndex: 6 });
createAllocation({ byteSize: 7, stackIndex: null });
const markersAndAllocationsLength = geckoThread.markers.data.length;
// Do a simple assertion to verify that the allocations were added by the test
// fixture as expected.
expect(markersAndAllocationsLength).toEqual(originalMarkersLength + 3);
// Process the profile and get out the new thread.
const processedProfile = processGeckoProfile(geckoProfile);
const processedThread = processedProfile.threads[0];
// Check for the existence of the allocations.
const { nativeAllocations } = processedThread;
if (!nativeAllocations) {
throw new Error(
'Could not find the nativeAllocations on the main thread.'
);
}
// Assert that the transformation makes sense.
expect(nativeAllocations.time).toEqual([0, 1, 2]);
expect(nativeAllocations.weight).toEqual([3, 5, 7]);
expect(nativeAllocations.stack).toEqual([8, 9, null]);
});
});
describe('gecko samples table processing', function () {
it('properly converts all the fields in the schema', function () {
const geckoProfile = createGeckoProfile();
const geckoSamples = geckoProfile.threads[0].samples;
// Check if the gecko sample schema is correct.
expect(geckoSamples.schema).toEqual({
stack: 0,
time: 1,
eventDelay: 2,
threadCPUDelta: 3,
});
// Add some values to the samples table so we can have hardcoded tests.
const hardcodedTime: Milliseconds[] = [1, 2];
const hardcodedStack: Array<null | IndexIntoGeckoStackTable> = [6, 5];
const hardcodedStackAfterProcessing: Array<null | IndexIntoGeckoStackTable> =
[9, 8];
const hardcodedEventDelay: Milliseconds[] = [0, 1];
const hardcodedThreadCPUDelta: Array<number | null> = [0.1, 0.2];
const hardcodedSamplesTable = [
[
hardcodedStack[0],
hardcodedTime[0],
hardcodedEventDelay[0],
hardcodedThreadCPUDelta[0],
],
[
hardcodedStack[1],
hardcodedTime[1],
hardcodedEventDelay[1],
hardcodedThreadCPUDelta[1],
],
];
geckoSamples.data = [...hardcodedSamplesTable, ...geckoSamples.data];
// Process the profile.
const processedProfile = processGeckoProfile(geckoProfile);
const processedSamples = processedProfile.threads[0].samples;
// Check the processed samples length.
expect(processedSamples.length).toBe(geckoSamples.data.length);
// Let's check the hardcoded values here.
expect(processedSamples.stack.slice(0, 2)).toEqual(
hardcodedStackAfterProcessing
);
const sampleTimes = computeTimeColumnForRawSamplesTable(processedSamples);
expect(sampleTimes.slice(0, 2)).toEqual(hardcodedTime);
expect(ensureExists(processedSamples.eventDelay).slice(0, 2)).toEqual(
hardcodedEventDelay
);
expect(ensureExists(processedSamples.threadCPUDelta).slice(0, 2)).toEqual(
hardcodedThreadCPUDelta
);
// Check the processed profile samples array to see if we properly processed
// the sample fields.
for (const fieldName in geckoSamples.schema) {
if (fieldName === 'stack' || fieldName === 'time') {
// Don't check the stack here because profile processing changes the shape
// of the stack table and converts times to deltas, so the value is
// expected to change.
continue;
}
const fieldIndex = geckoSamples.schema[fieldName];
for (let i = 0; i < processedSamples.length; i++) {
expect(processedSamples[fieldName][i]).toBe(
geckoSamples.data[i][fieldIndex]
);
}
}
});
});
describe('threadCPUDelta processing', function () {
it('removes threadCPUDelta data when the array is filled with null values', () => {
const geckoProfile = createGeckoProfile();
const geckoSamples = geckoProfile.threads[0].samples;
const geckoSchema = geckoSamples.schema;
if (!geckoSchema.threadCPUDelta) {
throw new Error(
'This test works with threads that can contain threadCPUDelta data.'
);
}
// Fill the threadCPUDelta with null values.
for (const item of geckoSamples.data) {
// $FlowExpectError because Flow thinks this access can be out of bounds, but it is not.
item[geckoSchema.threadCPUDelta] = null;
}
// Process the profile.
const processedProfile = processGeckoProfile(geckoProfile);
const processedSamples = processedProfile.threads[0].samples;
// Check that the threadCPUdelta array has been removed.
expect(processedSamples.threadCPUDelta).not.toBeDefined();
});
it('keeps threadCPUDelta data when the array contains at least one non-null value', () => {
const geckoProfile = createGeckoProfile();
const geckoSamples = geckoProfile.threads[0].samples;
const geckoSchema = geckoSamples.schema;
if (!geckoSchema.threadCPUDelta) {
throw new Error(
'This test works with threads that can contain threadCPUDelta data.'
);
}
// Fill the threadCPUDelta with null values except the first.
for (const item of geckoSamples.data) {
// $FlowExpectError because Flow thinks this access can be out of bounds, but it is not.
item[geckoSchema.threadCPUDelta] = null;
}
// $FlowExpectError same reason as above
geckoSamples.data[0][geckoSchema.threadCPUDelta] = 2;
// Process the profile.
const processedProfile = processGeckoProfile(geckoProfile);
const processedSamples = processedProfile.threads[0].samples;
// Check that the threadCPUdelta array has been removed.
expect(processedSamples.threadCPUDelta).toBeDefined();
});
});
describe('profile meta processing', function () {
it('keeps the sampleUnits object successfully', function () {
const geckoProfile = createGeckoProfile();
const geckoMeta = geckoProfile.meta;
// Processing the profile.
const processedProfile = processGeckoProfile(geckoProfile);
const processedMeta = processedProfile.meta;
// Checking if it keeps the sampleUnits object.
expect(processedMeta.sampleUnits).toEqual(geckoMeta.sampleUnits);
});
it('keeps the profilingStartTime and profilingEndTime', function () {
const geckoProfile = createGeckoProfile();
const geckoMeta = geckoProfile.meta;
// Processing the profile.
const processedProfile = processGeckoProfile(geckoProfile);
const processedMeta = processedProfile.meta;
expect(processedMeta.profilingStartTime).toEqual(
geckoMeta.profilingStartTime
);
expect(processedMeta.profilingEndTime).toEqual(geckoMeta.profilingEndTime);
});
it('does not create the profilingStartTime and profilingEndTime fields', function () {
const geckoProfile = createGeckoProfile();
const geckoMeta = geckoProfile.meta;
delete geckoMeta.profilingStartTime;
delete geckoMeta.profilingEndTime;
// Processing the profile.
const processedProfile = processGeckoProfile(geckoProfile);
const processedMeta = processedProfile.meta;
expect(processedMeta.profilingStartTime).toEqual(undefined);
expect(processedMeta.profilingEndTime).toEqual(undefined);
});
});
describe('visualMetrics processing', function () {
function checkVisualMetricsForThread(
thread: RawThread,
metrics: Array<{|
name: string,
hasProgressMarker: boolean,
changeMarkerLength: number,
|}>
) {
for (const { name, hasProgressMarker, changeMarkerLength } of metrics) {
// Check the visual metric progress markers.
const metricProgressMarkerName = `${name} Progress`;
const metricProgressMarker = thread.markers.name.find(
(name) => thread.stringArray[name] === metricProgressMarkerName
);
if (hasProgressMarker) {
expect(metricProgressMarker).toBeTruthy();
} else {
expect(metricProgressMarker).toBeFalsy();
}
// Check the visual metric change markers.
const metricChangeMarkerName = `${name} Change`;
const metricChangeMarkers = thread.markers.name.filter(
(name) => thread.stringArray[name] === metricChangeMarkerName
);
expect(metricChangeMarkers).toHaveLength(changeMarkerLength);
}
}
it('adds markers to the parent process', function () {
const geckoProfile = createGeckoProfile();
const visualMetrics = getVisualMetrics();
geckoProfile.meta.visualMetrics = visualMetrics;
// Make sure that the visual metrics are not changed.
expect(visualMetrics.VisualProgress).toHaveLength(7);
expect(visualMetrics.ContentfulSpeedIndexProgress).toHaveLength(6);
expect(visualMetrics.PerceptualSpeedIndexProgress).toHaveLength(6);
// Processing the profile.
const processedProfile = processGeckoProfile(geckoProfile);
const parentProcessMainThread = processedProfile.threads.find(
(thread) =>
thread.name === 'GeckoMain' && thread.processType === 'default'
);
if (!parentProcessMainThread) {
throw new Error('Could not find the parent process main thread.');
}
checkVisualMetricsForThread(parentProcessMainThread, [
{ name: 'Visual', hasProgressMarker: true, changeMarkerLength: 7 },
{
name: 'ContentfulSpeedIndex',
hasProgressMarker: true,
changeMarkerLength: 6,
},
{
name: 'PerceptualSpeedIndex',
hasProgressMarker: true,
changeMarkerLength: 6,
},
]);
});
it('adds markers to the tab process', function () {
const geckoProfile = createGeckoProfile();
const visualMetrics = getVisualMetrics();
geckoProfile.meta.visualMetrics = visualMetrics;
// Make sure that the visual metrics are not changed.
expect(visualMetrics.VisualProgress).toHaveLength(7);
expect(visualMetrics.ContentfulSpeedIndexProgress).toHaveLength(6);
expect(visualMetrics.PerceptualSpeedIndexProgress).toHaveLength(6);
// Processing the profile.
const processedProfile = processGeckoProfile(geckoProfile);
const tabProcessMainThread = processedProfile.threads.find(
(thread) => thread.name === 'GeckoMain' && thread.processType === 'tab'
);
if (!tabProcessMainThread) {
throw new Error('Could not find the tab process main thread.');
}
checkVisualMetricsForThread(tabProcessMainThread, [
{ name: 'Visual', hasProgressMarker: true, changeMarkerLength: 7 },
{
name: 'ContentfulSpeedIndex',
hasProgressMarker: true,
changeMarkerLength: 6,
},
{
name: 'PerceptualSpeedIndex',
hasProgressMarker: true,
changeMarkerLength: 6,
},
]);
});
it('does not add markers to the parent process if metric timestamps are null', function () {
const geckoProfile = createGeckoProfile();
const visualMetrics = getVisualMetrics();
// Make all the VisualProgress timestamps null on purpose.
visualMetrics.VisualProgress = visualMetrics.VisualProgress.map(
(progress) => ({ ...progress, timestamp: null })
);
// Make only one ContentfulSpeedIndexProgress timestamp null on purpose.
ensureExists(visualMetrics.ContentfulSpeedIndexProgress)[0].timestamp =
null;
// Add the visual metrics to the profile.
geckoProfile.meta.visualMetrics = visualMetrics;
// Make sure that the visual metrics are not changed.
expect(visualMetrics.VisualProgress).toHaveLength(7);
expect(visualMetrics.ContentfulSpeedIndexProgress).toHaveLength(6);
expect(visualMetrics.PerceptualSpeedIndexProgress).toHaveLength(6);
// Processing the profile.
const processedProfile = processGeckoProfile(geckoProfile);
const parentProcessMainThread = processedProfile.threads.find(
(thread) =>
thread.name === 'GeckoMain' && thread.processType === 'default'
);
if (!parentProcessMainThread) {
throw new Error('Could not find the parent process main thread.');
}
checkVisualMetricsForThread(parentProcessMainThread, [
// Instead of 7, we should have 0 markers for Visual because we made all
// the timestamps null.
{ name: 'Visual', hasProgressMarker: false, changeMarkerLength: 0 },
// Instead of 6, we should have 5 markers for ContentfulSpeedIndex.
{
name: 'ContentfulSpeedIndex',
hasProgressMarker: true,
changeMarkerLength: 5,
},
// We didn't change the PerceptualSpeedIndexProgress, so we should have 6.
{
name: 'PerceptualSpeedIndex',
hasProgressMarker: true,
changeMarkerLength: 6,
},
]);
});
});