-
Notifications
You must be signed in to change notification settings - Fork 8
/
perf.js
96 lines (78 loc) · 1.95 KB
/
perf.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
// @flow strict-local
import {
type Decoder,
array,
autoRecord,
dict,
number,
pair,
string,
} from "tiny-decoders";
export const MAX_PERF_ENTRIES = 9;
export type Durations = Array<[string, number]>;
export const decodeDurations: Decoder<Durations> = array(pair(string, number));
export type Stats = {
url: string,
numTotalElements: number,
numTrackedElements: number,
numVisibleElements: number,
numVisibleFrames: number,
bailed: number,
durations: Durations,
};
export const decodeStats: Decoder<Stats> = autoRecord({
url: string,
numTotalElements: number,
numTrackedElements: number,
numVisibleElements: number,
numVisibleFrames: number,
bailed: number,
durations: decodeDurations,
});
export type Perf = Array<{
timeToFirstPaint: number,
timeToLastPaint: number,
topDurations: Durations,
collectStats: Array<Stats>,
renderDurations: Durations,
}>;
export const decodePerf: Decoder<Perf> = array(
autoRecord({
timeToFirstPaint: number,
timeToLastPaint: number,
topDurations: decodeDurations,
collectStats: array(decodeStats),
renderDurations: decodeDurations,
})
);
export type TabsPerf = { [tabId: string]: Perf, ... };
export const decodeTabsPerf: Decoder<TabsPerf> = dict(decodePerf);
export class TimeTracker {
_durations: Durations = [];
_current: ?{ label: string, timestamp: number } = undefined;
start(label: string) {
this.stop();
this._current = {
label,
timestamp: Date.now(),
};
}
stop() {
const current = this._current;
if (current == null) {
return;
}
const duration = Date.now() - current.timestamp;
const previous = this._durations.find(([label]) => label === current.label);
if (previous) {
previous[1] += duration;
} else {
this._durations.push([current.label, duration]);
}
this._current = undefined;
}
export(): Durations {
this.stop();
return this._durations.slice();
}
}