forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marker-data.js
1094 lines (998 loc) · 34.6 KB
/
marker-data.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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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 { getEmptyRawMarkerTable } from './data-structures';
import type {
SamplesTable,
RawMarkerTable,
IndexIntoStringTable,
IndexIntoRawMarkerTable,
} from '../types/profile';
import type { Marker, MarkerIndex } from '../types/profile-derived';
import type { BailoutPayload, NetworkPayload } from '../types/markers';
import type { UniqueStringArray } from '../utils/unique-string-array';
import type { StartEndRange } from '../types/units';
/**
* Jank instances are created from responsiveness values. Responsiveness is a profiler
* feature that can be turned on and off. When on, every sample includes a responsiveness
* value.
*
* This timing is captured by instrumenting the event queue. A runnable is added to the
* browser's event queue, then the profiler times how long it takes to come back.
* Generally, if this takes longer than some threshold, then this can be jank for the
* browser.
*
* This function converts those measurings of milliseconds into individual markers.
*
* For instance, take an array of responsiveness values:
*
* [5, 25, 33, 3, 23, 42, 65, 71, 3, 10, 22, 31, 42, 3, 20, 40]
* |___| |___| |___|
* Runnable is reset Jank of 71ms. The Runnable reset under threshold.
* but under 50ms, responsiveness was
* no jank. reset from 71 to 3.
*/
export function deriveJankMarkers(
samples: SamplesTable,
thresholdInMs: number
): Marker[] {
const addMarker = () =>
jankInstances.push({
start: lastTimestamp - lastResponsiveness,
dur: lastResponsiveness,
title: `${lastResponsiveness.toFixed(2)}ms event processing delay`,
name: 'Jank',
data: null,
});
let lastResponsiveness: number = 0;
let lastTimestamp: number = 0;
const jankInstances = [];
for (let i = 0; i < samples.length; i++) {
const currentResponsiveness = samples.responsiveness[i];
if (currentResponsiveness === null || currentResponsiveness === undefined) {
// Ignore anything that's not numeric. This can happen if there is no responsiveness
// information, or if the sampler failed to collect a responsiveness value. This
// can happen intermittently.
//
// See Bug 1506226.
continue;
}
if (currentResponsiveness < lastResponsiveness) {
if (lastResponsiveness >= thresholdInMs) {
addMarker();
}
}
lastResponsiveness = currentResponsiveness;
lastTimestamp = samples.time[i];
}
if (lastResponsiveness >= thresholdInMs) {
addMarker();
}
return jankInstances;
}
export function getSearchFilteredMarkerIndexes(
getMarker: MarkerIndex => Marker,
markerIndexes: MarkerIndex[],
searchRegExp: RegExp | null
): MarkerIndex[] {
if (!searchRegExp) {
return markerIndexes;
}
const newMarkers: MarkerIndex[] = [];
for (const markerIndex of markerIndexes) {
const { data, name } = getMarker(markerIndex);
// Reset regexp for each iteration. Otherwise state from previous
// iterations can cause matches to fail if the search is global or
// sticky.
searchRegExp.lastIndex = 0;
if (searchRegExp.test(name)) {
newMarkers.push(markerIndex);
continue;
}
if (data && typeof data === 'object') {
if (data.type === 'FileIO') {
const { filename, operation, source } = data;
if (
searchRegExp.test(filename) ||
searchRegExp.test(operation) ||
searchRegExp.test(source)
) {
newMarkers.push(markerIndex);
continue;
}
}
if (
typeof data.eventType === 'string' &&
searchRegExp.test(data.eventType)
) {
// Match DOMevents data.eventType
newMarkers.push(markerIndex);
continue;
}
if (typeof data.name === 'string' && searchRegExp.test(data.name)) {
// Match UserTiming's name.
newMarkers.push(markerIndex);
continue;
}
if (
typeof data.category === 'string' &&
searchRegExp.test(data.category)
) {
// Match UserTiming's name.
newMarkers.push(markerIndex);
continue;
}
}
}
return newMarkers;
}
/**
* This function takes a marker that packs in a marker payload into the string of the
* name. This extracts that and turns it into a payload.
*/
export function extractMarkerDataFromName(
markers: RawMarkerTable,
stringTable: UniqueStringArray
): RawMarkerTable {
const newMarkers: RawMarkerTable = {
data: markers.data.slice(),
name: markers.name.slice(),
time: markers.time.slice(),
length: markers.length,
};
// Match: "Bailout_MonitorTypes after add on line 1013 of self-hosted:1008"
// Match: "Bailout_TypeBarrierO at jumptarget on line 1490 of resource://devtools/shared/base-loader.js -> resource://devtools/client/shared/vendor/immutable.js:1484"
const bailoutRegex =
// Capture groups:
// type afterAt where bailoutLine script functionLine
// ↓ ↓ ↓ ↓ ↓ ↓
/^Bailout_(\w+) (after|at) ([\w _-]+) on line (\d+) of (.*):(\d+)$/;
// Match: "Invalidate resource://devtools/shared/base-loader.js -> resource://devtools/client/shared/vendor/immutable.js:3662"
// Match: "Invalidate self-hosted:4032"
const invalidateRegex =
// Capture groups:
// url line
// ↓ ↓
/^Invalidate (.*):(\d+)$/;
const bailoutStringIndex = stringTable.indexForString('Bailout');
const invalidationStringIndex = stringTable.indexForString('Invalidate');
for (let markerIndex = 0; markerIndex < markers.length; markerIndex++) {
const nameIndex = markers.name[markerIndex];
const time = markers.time[markerIndex];
const name = stringTable.getString(nameIndex);
let matchFound = false;
if (name.startsWith('Bailout_')) {
matchFound = true;
const match = name.match(bailoutRegex);
if (!match) {
console.error(`Could not match regex for bailout: "${name}"`);
} else {
const [
,
type,
afterAt,
where,
bailoutLine,
script,
functionLine,
] = match;
newMarkers.name[markerIndex] = bailoutStringIndex;
newMarkers.data[markerIndex] = ({
type: 'Bailout',
bailoutType: type,
where: afterAt + ' ' + where,
script: script,
bailoutLine: +bailoutLine,
functionLine: +functionLine,
startTime: time,
endTime: time,
}: BailoutPayload);
}
} else if (name.startsWith('Invalidate ')) {
matchFound = true;
const match = name.match(invalidateRegex);
if (!match) {
console.error(`Could not match regex for bailout: "${name}"`);
} else {
const [, url, line] = match;
newMarkers.name[markerIndex] = invalidationStringIndex;
newMarkers.data[markerIndex] = {
type: 'Invalidation',
url,
line,
startTime: time,
endTime: time,
};
}
}
if (matchFound && markers.data[markerIndex]) {
console.error(
"A marker's payload was rewritten based off the text content of the marker. " +
"profiler.firefox.com assumed that the payload was empty, but it turns out it wasn't. " +
'This is most likely an error and should be fixed. The marker name is:',
name
);
}
}
return newMarkers;
}
export function deriveMarkersFromRawMarkerTable(
rawMarkers: RawMarkerTable,
stringTable: UniqueStringArray,
firstSampleTime: number,
lastSampleTime: number,
interval: number
): Marker[] {
// This is the resulting array.
const matchedMarkers: Marker[] = [];
// These maps contain the start markers we find while looping the marker
// table.
// The first map contains the start markers for tracing markers. They can be
// nested and that's why we use an array structure as value.
const openTracingMarkers: Map<
IndexIntoStringTable,
MarkerIndex[]
> = new Map();
// The second map contains the start markers for network markers.
// Note that we don't have more than 2 network markers with the same name as
// the name contains an incremented index. Therefore we don't need to use an
// array as value like for tracing markers.
const openNetworkMarkers: Map<IndexIntoStringTable, MarkerIndex> = new Map();
// We don't add a screenshot marker as we find it, because to know its
// duration we need to wait until the next one or the end of the profile. So
// we keep it here.
let previousScreenshotMarker: MarkerIndex | null = null;
for (let i = 0; i < rawMarkers.length; i++) {
const name = rawMarkers.name[i];
const time = rawMarkers.time[i];
const data = rawMarkers.data[i];
if (!data) {
// Add a marker with a zero duration
matchedMarkers.push({
start: time,
dur: 0,
name: stringTable.getString(name),
title: null,
data: null,
});
continue;
}
// Depending on the type we have to do some special handling.
switch (data.type) {
case 'tracing': {
// Markers are created from two distinct raw markers that are created at
// the start and end of whatever code that is running that we care about.
// This is implemented by AutoProfilerTracing in Gecko.
//
// In this function we convert both of these raw markers into a single
// marker with a non-null duration.
//
// We also handle nested markers by assuming markers of the same type are
// never interwoven: given input markers startA, startB, endC, endD, we'll
// get 2 markers A-D and B-C.
//
// Sometimes we don't have one side of the pair, in this case we still
// insert a marker and try to fill it with sensible values.
if (data.interval === 'start') {
let openMarkersForName = openTracingMarkers.get(name);
if (!openMarkersForName) {
openMarkersForName = [];
openTracingMarkers.set(name, openMarkersForName);
}
openMarkersForName.push(i);
// We're not inserting anything to matchedMarkers yet. We wait for the
// end marker for that so that we know about the duration.
//
// We'll loop at all open markers after the main loop.
} else if (data.interval === 'end') {
const openMarkersForName = openTracingMarkers.get(name);
let startIndex;
if (openMarkersForName) {
startIndex = openMarkersForName.pop();
}
if (startIndex !== undefined) {
// A start marker matches this end marker.
const start = rawMarkers.time[startIndex];
matchedMarkers.push({
start,
name: stringTable.getString(name),
dur: time - start,
title: null,
data: rawMarkers.data[startIndex],
});
} else {
// No matching "start" marker has been encountered before this "end".
// This means it was issued before the capture started. Here we create
// an "incomplete" marker which will be truncated at the starting end
// since we don't know exactly when it started.
// Note we won't have additional data (eg the cause stack) for this
// marker because that data is contained in the "start" marker.
// Also note that the end marker could occur before the
// first sample. In that case it'll become a dot marker at
// the location of the end marker. Otherwise we'll use the
// time of the first sample as its start.
const start = Math.min(time, firstSampleTime);
matchedMarkers.push({
start,
name: stringTable.getString(name),
dur: time - start,
title: null,
data,
incomplete: true,
});
}
} else {
console.error(
`'data.interval' holds the invalid value '${
data.interval
}' in marker index ${i}. This should not normally happen.`
);
matchedMarkers.push({
start: time,
dur: 0,
name: stringTable.getString(name),
title: null,
data,
});
}
break;
}
case 'Network': {
// Network markers are similar to tracing markers in that they also
// normally exist in pairs of start/stop markers. But unlike tracing
// markers they have a duration and "startTime/endTime" properties like
// more generic markers. Lastly they're always adjacent: the start
// markers ends when the stop markers starts.
//
// The timestamps on the start and end markers describe two
// non-overlapping parts of the same load. The start marker has a
// duration from channel-creation until Start (i.e. AsyncOpen()). The
// end marker has a duration from AsyncOpen time until OnStopRequest.
// In the merged marker, we want to represent the entire duration, from
// channel-creation until OnStopRequest.
//
// |--- start marker ---|--- stop marker with timings ---|
//
// Usually the start marker is very small. It's emitted mostly to know
// about the start of the request. But most of the interesting bits are
// in the stop marker.
if (data.status === 'STATUS_START') {
openNetworkMarkers.set(name, i);
} else {
// End status can be any status other than 'STATUS_START'. They are
// either 'STATUS_STOP' or 'STATUS_REDIRECT'.
const endData = data;
const startIndex = openNetworkMarkers.get(name);
if (startIndex !== undefined) {
// A start marker matches this end marker.
openNetworkMarkers.delete(name);
// We know this startIndex points to a Network marker.
const startData: NetworkPayload = (rawMarkers.data[
startIndex
]: any);
matchedMarkers.push({
start: startData.startTime,
dur: endData.endTime - startData.startTime,
name: stringTable.getString(name),
title: null,
data: {
...endData,
startTime: startData.startTime,
fetchStart: endData.startTime,
},
});
} else {
// There's no start marker matching this end marker. This means an
// abstract marker exists before the start of the profile.
const start = Math.min(firstSampleTime, endData.startTime);
matchedMarkers.push({
start,
dur: endData.endTime - start,
name: stringTable.getString(name),
title: null,
data: {
...endData,
startTime: start,
fetchStart: endData.startTime,
},
incomplete: true,
});
}
}
break;
}
case 'CompositorScreenshot': {
// Screenshot markers are already ordered. In the raw marker table,
// they're dot markers, but since they're valid until the following
// raw marker of the same type, we convert them to markers with a
// duration using the following marker.
if (previousScreenshotMarker !== null) {
const start = rawMarkers.time[previousScreenshotMarker];
const data = rawMarkers.data[previousScreenshotMarker];
matchedMarkers.push({
start,
dur: time - start,
name: 'CompositorScreenshot',
title: null,
data,
});
}
previousScreenshotMarker = i;
break;
}
default:
if (
typeof data.startTime === 'number' &&
typeof data.endTime === 'number'
) {
matchedMarkers.push({
start: data.startTime,
dur: data.endTime - data.startTime,
name: stringTable.getString(name),
data,
title: null,
});
} else {
// Ensure all raw markers are converted to markers, even if they have no
// more timing information. This ensures that markers can be filtered by time
// in a consistent manner.
matchedMarkers.push({
start: time,
dur: 0,
name: stringTable.getString(name),
data,
title: null,
});
}
}
}
const endOfThread = lastSampleTime + interval;
// Loop over "start" markers without any "end" markers.
for (const markerBucket of openTracingMarkers.values()) {
for (const startIndex of markerBucket) {
const start = rawMarkers.time[startIndex];
matchedMarkers.push({
start,
dur: Math.max(endOfThread - start, 0),
name: stringTable.getString(rawMarkers.name[startIndex]),
data: rawMarkers.data[startIndex],
title: null,
incomplete: true,
});
}
}
for (const startIndex of openNetworkMarkers.values()) {
// We know this startIndex points to a Network marker.
const startData: NetworkPayload = (rawMarkers.data[startIndex]: any);
matchedMarkers.push({
start: startData.startTime,
dur: Math.max(endOfThread - startData.startTime, 0),
name: stringTable.getString(rawMarkers.name[startIndex]),
title: null,
data: startData,
incomplete: true,
});
}
// And we also need to add the "last screenshot marker".
if (previousScreenshotMarker !== null) {
const start = rawMarkers.time[previousScreenshotMarker];
matchedMarkers.push({
start,
dur: Math.max(endOfThread - start, 0),
name: 'CompositorScreenshot',
data: rawMarkers.data[previousScreenshotMarker],
title: null,
});
}
return matchedMarkers;
}
/**
* This function filters markers from a thread's raw marker table using the
* range specified as parameter.
*/
export function filterRawMarkerTableToRange(
markers: RawMarkerTable,
rangeStart: number,
rangeEnd: number
): RawMarkerTable {
const newMarkerTable = getEmptyRawMarkerTable();
const filteredMarkerIndexesIter = filterRawMarkerTableToRangeIndexGenerator(
markers,
rangeStart,
rangeEnd
);
for (const index of filteredMarkerIndexesIter) {
newMarkerTable.time.push(markers.time[index]);
newMarkerTable.name.push(markers.name[index]);
newMarkerTable.data.push(markers.data[index]);
newMarkerTable.length++;
}
return newMarkerTable;
}
/**
* This function filters marker indexes from a thread's raw marker table using
* the range specified as parameter.
* It especially takes care of the markers that need a special handling because
* of how the rest of the code handles them.
*
* There's more explanations about this special handling in the switch block
* below.
*
* This is a generator function and it returns a IndexIntoMarkers every step.
* You can use that function inside a for..of or use it with `.next()` function.
* The reason to use generator function is avoiding creating an intermediate
* markers array on some consumers.
*/
export function* filterRawMarkerTableToRangeIndexGenerator(
markers: RawMarkerTable,
rangeStart: number,
rangeEnd: number
): Generator<MarkerIndex, void, void> {
const isTimeInRange = (time: number): boolean =>
time < rangeEnd && time >= rangeStart;
const intersectsRange = (start: number, end: number): boolean =>
start < rangeEnd && end >= rangeStart;
// These maps contain the start markers we find while looping the marker
// table.
// The first map contains the start markers for tracing markers. They can be
// nested and that's why we use an array structure as value.
const openTracingMarkers: Map<
IndexIntoStringTable,
IndexIntoRawMarkerTable[]
> = new Map();
// The second map contains the start markers for network markers.
// Note that we don't have more than 2 network markers with the same name as
// the name contains an incremented index. Therefore we don't need to use an
// array as value like for tracing markers.
const openNetworkMarkers: Map<
IndexIntoStringTable,
IndexIntoRawMarkerTable
> = new Map();
let previousScreenshotMarker = null;
for (let i = 0; i < markers.length; i++) {
const name = markers.name[i];
const time = markers.time[i];
const data = markers.data[i];
if (!data) {
if (isTimeInRange(time)) {
yield i;
}
continue;
}
// Depending on the type we have to do some special handling.
switch (data.type) {
case 'tracing': {
// Tracing markers are pairs of start/end markers. To retain their
// duration if we have it, we keep both markers of the pair if they
// represent a marker that's partially in the range.
if (data.interval === 'start') {
let openMarkersForName = openTracingMarkers.get(name);
if (!openMarkersForName) {
openMarkersForName = [];
openTracingMarkers.set(name, openMarkersForName);
}
openMarkersForName.push(i);
// We're not inserting anything to newMarkerTable yet. We wait for the
// end marker to decide whether we should add this start marker, as we
// will add start markers from before the range if the end marker is
// in or after the range.
//
// We'll loop at all open markers after the main loop, to add them to
// the new marker table if they're in the range.
} else if (data.interval === 'end') {
const openMarkersForName = openTracingMarkers.get(name);
let startIndex;
if (openMarkersForName) {
startIndex = openMarkersForName.pop();
}
if (startIndex !== undefined) {
// A start marker matches this end marker.
if (intersectsRange(markers.time[startIndex], time)) {
// This couple of markers define a marker that's at least partially
// in the range.
yield startIndex;
yield i;
}
} else {
// No start marker matches this end marker, then we'll add it only if
// it's in or after the time range.
if (time >= rangeStart) {
yield i;
}
}
} else {
console.error(
`'data.interval' holds the invalid value '${
data.interval
}' in marker index ${i}. This should not normally happen.`
);
if (isTimeInRange(time)) {
yield i;
}
}
break;
}
case 'Network': {
// Network markers are similar to tracing markers in that they also
// normally exist in pairs of start/stop markers. Just like tracing
// markers we keep both markers of the pair if they're partially in the
// range so that we keep all the useful data. But unlike tracing markers
// they have a duration and "startTime/endTime" properties like more
// generic markers. Lastly they're always adjacent.
if (data.status === 'STATUS_START') {
openNetworkMarkers.set(name, i);
} else {
// End status can be any status other than 'STATUS_START'
const startIndex = openNetworkMarkers.get(name);
if (startIndex !== undefined) {
// A start marker matches this end marker.
openNetworkMarkers.delete(name);
// We know this startIndex points to a Network marker.
const startData: NetworkPayload = (markers.data[startIndex]: any);
const endData = data;
if (intersectsRange(startData.startTime, endData.endTime)) {
// This couple of markers define a network marker that's at least
// partially in the range.
yield startIndex;
yield i;
}
} else {
// There's no start marker matching this end marker. This means an
// abstract marker exists before the start of the profile.
// Then we add it if it ends after the start of the range.
if (data.endTime >= rangeStart) {
yield i;
}
}
}
break;
}
case 'CompositorScreenshot': {
// Between two screenshot markers, we keep on displaying the previous
// screenshot. this is why we always keep the last screenshot marker
// before the start of the range, if it exists. These markers are
// ordered by time and the rest of our code rely on it, so this
// invariant is also kept here.
if (time < rangeStart) {
previousScreenshotMarker = i;
continue;
}
if (time < rangeEnd) {
if (previousScreenshotMarker !== null) {
yield previousScreenshotMarker;
previousScreenshotMarker = null;
}
yield i;
}
// If previousScreenshotMarker isn't null after the loop, it will be
// considered for addition to the marker table.
break;
}
default:
if (
typeof data.startTime === 'number' &&
typeof data.endTime === 'number'
) {
if (intersectsRange(data.startTime, data.endTime)) {
yield i;
}
} else {
if (isTimeInRange(time)) {
yield i;
}
}
}
}
// Loop over "start" markers without any "end" markers. We add one only if
// it's in or before the specified range.
// Note: doing it at the end, we change the order of markers compared to the
// source, but it's OK because the only important invariant is that pairs of
// start/end come in order.
for (const markerBucket of openTracingMarkers.values()) {
for (const startIndex of markerBucket) {
if (markers.time[startIndex] < rangeEnd) {
yield startIndex;
}
}
}
for (const startIndex of openNetworkMarkers.values()) {
const data: NetworkPayload = (markers.data[startIndex]: any);
if (data.startTime < rangeEnd) {
yield startIndex;
}
}
// And we should add the "last screenshot marker before the range" if it
// hadn't been added yet.
if (previousScreenshotMarker !== null) {
yield previousScreenshotMarker;
}
}
/**
* This function filters markers from a thread's raw marker table using the
* range and marker indexes array specified as parameters.
*
* Uses `filterRawMarkerTableToRangeIndexGenerator` function and excludes
* markers in `markersToDelete` set.
*/
export function filterRawMarkerTableToRangeWithMarkersToDelete(
markerTable: RawMarkerTable,
markersToDelete: Set<IndexIntoRawMarkerTable>,
filterRange: StartEndRange | null
): {
rawMarkerTable: RawMarkerTable,
oldMarkerIndexToNew: Map<IndexIntoRawMarkerTable, IndexIntoRawMarkerTable>,
} {
const oldMarkers = markerTable;
const newMarkerTable = getEmptyRawMarkerTable();
const oldMarkerIndexToNew: Map<
IndexIntoRawMarkerTable,
IndexIntoRawMarkerTable
> = new Map();
const addMarkerIndexIfIncluded = (index: IndexIntoRawMarkerTable) => {
if (markersToDelete.has(index)) {
return;
}
oldMarkerIndexToNew.set(index, newMarkerTable.length);
newMarkerTable.name.push(oldMarkers.name[index]);
newMarkerTable.time.push(oldMarkers.time[index]);
newMarkerTable.data.push(oldMarkers.data[index]);
newMarkerTable.length++;
};
if (filterRange === null) {
// If user doesn't want to filter out the full time range, remove only
// markers that we want to remove.
for (let i = 0; i < oldMarkers.length; i++) {
addMarkerIndexIfIncluded(i);
}
} else {
// If user wants to remove full time range, filter all the markers
// accordingly.
const { start, end } = filterRange;
const filteredMarkerIndexIter = filterRawMarkerTableToRangeIndexGenerator(
oldMarkers,
start,
end
);
for (const index of filteredMarkerIndexIter) {
addMarkerIndexIfIncluded(index);
}
}
return {
rawMarkerTable: newMarkerTable,
oldMarkerIndexToNew,
};
}
/**
* This utility function makes it easier to implement functions filtering
* markers, with marker indexes both as input and output.
*/
export function filterMarkerIndexes(
getMarker: MarkerIndex => Marker,
markerIndexes: MarkerIndex[],
filterFunc: Marker => boolean
): MarkerIndex[] {
return markerIndexes.filter(markerIndex => {
return filterFunc(getMarker(markerIndex));
});
}
export function filterMarkerIndexesToRange(
getMarker: MarkerIndex => Marker,
markerIndexes: MarkerIndex[],
rangeStart: number,
rangeEnd: number
): MarkerIndex[] {
return filterMarkerIndexes(
getMarker,
markerIndexes,
marker => marker.start < rangeEnd && marker.start + marker.dur >= rangeStart
);
}
export function isNetworkMarker(marker: Marker): boolean {
return !!(marker.data && marker.data.type === 'Network');
}
export function isNavigationMarker({ name, data }: Marker) {
if (name === 'TTI') {
// TTI is only selectable by name, as it doesn't have a structured payload.
return true;
}
if (!data) {
// This marker has no payload, only consider the name.
if (name === 'Navigation::Start') {
return true;
}
if (name.startsWith('Contentful paint ')) {
// This is a long plaintext marker.
// e.g. "Contentful paint after 322ms for URL https://developer.mozilla.org/en-US/, foreground tab"
return true;
}
return false;
}
if (data.category === 'Navigation') {
// Filter by payloads.
if (name === 'Load' || name === 'DOMContentLoaded') {
return true;
}
}
return false;
}
export function isFileIoMarker(marker: Marker): boolean {
return !!(marker.data && marker.data.type === 'FileIO');
}
export function isMemoryMarker(marker: Marker): boolean {
const { data } = marker;
if (!data) {
return false;
}
return (
data.type === 'GCMajor' ||
data.type === 'GCMinor' ||
data.type === 'GCSlice' ||
(data.type === 'tracing' && data.category === 'CC')
);
}
export function filterForNetworkChart(markers: Marker[]): Marker[] {
return markers.filter(marker => isNetworkMarker(marker));
}
export function filterForMarkerChart(
getMarker: MarkerIndex => Marker,
markerIndexes: MarkerIndex[]
): MarkerIndex[] {
return filterMarkerIndexes(
getMarker,
markerIndexes,
marker => !isNetworkMarker(marker)
);
}
// Identifies mime type of a network request.
export function guessMimeTypeFromNetworkMarker(
payload: NetworkPayload
): string | null {
let uri;
try {
uri = new URL(payload.URI);
} catch (e) {
return null;
}
// Extracting the fileName from the path.
// This is a workaround until we have
// mime types passed from gecko to network marker requests.
const fileName = uri.pathname;
const lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex < 0) {
return null;
}
const fileExt = fileName.slice(lastDotIndex + 1);
switch (fileExt) {
case 'js':
return 'application/javascript';
case 'css':
case 'html':
return `text/${fileExt}`;
case 'gif':
case 'png':
return `image/${fileExt}`;
case 'jpeg':
case 'jpg':
return 'image/jpeg';
case 'svg':
return 'image/svg+xml';
default:
return null;
}
}
// This function returns one of the global css classes, or the empty string,
// depending on the input mime type. Usually this function is fed the result of
// `guessMimeTypeFromNetworkMarker`.
export function getColorClassNameForMimeType(
mimeType: string | null
):
| 'network-color-css'
| 'network-color-js'
| 'network-color-html'
| 'network-color-img'
| 'network-color-other' {
switch (mimeType) {
case 'text/css':
return 'network-color-css';
case 'text/html':
return 'network-color-html';
case 'application/javascript':
return 'network-color-js';
case null:
return 'network-color-other';
default:
if (mimeType.startsWith('image/')) {
return 'network-color-img';
}
return 'network-color-other';
}
}
export function groupScreenshotsById(
getMarker: MarkerIndex => Marker,