-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathperformance.ts
134 lines (116 loc) · 3.52 KB
/
performance.ts
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
import { EventTarget } from "./event";
/** Encapsulates a single performance metric that is part of the performance timeline. A performance entry can be directly created by making a performance mark or measure (for example by calling the mark() method) at an explicit point in an application. Performance entries are also created in indirect ways such as loading a resource (such as an image). */
class PerformanceEntry {
readonly duration: number;
readonly entryType: string;
readonly name: string;
readonly startTime: number;
constructor(name: string, startTime: number, entryType: string, duration = 0) {
this.startTime = startTime;
this.name = name;
this.entryType = entryType;
this.duration = duration;
}
toJSON() {
return {
duration: this.duration,
entryType: this.entryType,
name: this.name,
startTime: this.startTime,
};
};
}
class PerformanceMark extends PerformanceEntry {}
class PerformanceMeasure extends PerformanceEntry {}
type PerformanceEntryList = PerformanceEntry[];
const MARK_TYPE = "mark";
const MEASURE_TYPE = "measure";
class Performance extends EventTarget {
readonly timeOrigin: number;
private _entries = new Map<string, PerformanceEntryList>();
now() {
return Date.now() - this.timeOrigin;
}
constructor() {
super();
this.timeOrigin = Date.now();
}
getEntries(): PerformanceEntryList {
let ret: PerformanceEntryList = [];
for (const [type, list] of this._entries) {
ret = ret.concat(list);
}
return ret;
}
getEntriesByName(name: string, type?: string): PerformanceEntryList {
let ret: PerformanceEntryList = [];
for (const [entryType, list] of this._entries) {
if (type && type != entryType) continue;
list.map(e => {
if (e.name == name) {
ret.push(e);
}
});
}
return ret;
}
getEntriesByType(type: string): PerformanceEntryList {
return this._entries.get(type);
}
mark(markName: string) {
const mark = new PerformanceMark(markName, this.now(), MARK_TYPE);
let marks: PerformanceEntryList = this._entries.get(MARK_TYPE);
if (!marks) {
marks = [ mark ];
this._entries.set(MARK_TYPE, marks);
} else {
marks.push(mark);
}
return mark;
}
measure(measureName: string, startMark: string, endMark: string) {
let starts = this.getEntriesByName(startMark, MARK_TYPE);
if (starts.length == 0) throw new Error(`The mark '${startMark}' does not exist.`);
let ends = this.getEntriesByName(endMark, MARK_TYPE);
if (ends.length == 0) throw new Error(`The mark '${endMark}' does not exist.`);
const start = starts[starts.length - 1];
const end = ends[ends.length - 1];
const measure = new PerformanceMeasure(measureName, start.startTime, MEASURE_TYPE, end.startTime - start.startTime);
let measures: PerformanceEntryList = this._entries.get(MEASURE_TYPE);
if (!measures) {
measures = [ measure ];
this._entries.set(MEASURE_TYPE, measures);
} else {
measures.push(measure);
}
return measure;
}
clearMarks(markName: string): void {
let marks = this._entries.get(MARK_TYPE);
if (marks) {
marks = marks.filter( m => m.name === markName );
this._entries.set(MARK_TYPE, marks);
}
}
clearMeasures(measureName: string): void {
let measures = this._entries.get(MARK_TYPE);
if (measures) {
measures = measures.filter( m => m.name === measureName );
this._entries.set(MEASURE_TYPE, measures);
}
}
toJSON() {
return {
timeOrigin: this.timeOrigin,
}
}
};
export default {
exports: {
Performance,
PerformanceEntry,
PerformanceMark,
PerformanceMeasure,
performance: new Performance()
}
};