forked from MCJack123/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeasure-performance.ts
More file actions
83 lines (71 loc) · 2.17 KB
/
measure-performance.ts
File metadata and controls
83 lines (71 loc) · 2.17 KB
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
import { performance } from "perf_hooks";
// We use our own performance hooks implementation for easier use, but also call node's performance hooks, so it shows up in the profiler.
let enabled = false;
const marks = new Map<string, number>();
const durations = new Map<string, number>();
function timestamp() {
return performance.now();
}
/**
* Marks a performance event, with the given markName.
*/
function mark(markName: string) {
if (enabled) {
marks.set(markName, timestamp());
performance.mark(markName);
}
}
/**
* Adds a performance measurement with the specified name.
*
* @param measureName The name of the performance measurement.
* @param startMarkName The name of the starting mark
* @param endMarkName The name of the ending mark
*/
function measure(measureName: string, startMarkName: string, endMarkName: string) {
if (enabled) {
const end = marks.get(endMarkName) ?? timestamp();
const start = marks.get(startMarkName) ?? performance.timeOrigin;
const previousDuration = durations.get(measureName) ?? 0;
durations.set(measureName, previousDuration + (end - start));
performance.measure(measureName, startMarkName, endMarkName);
}
}
/**
* Starts a performance measurement section.
* @param name name of the measurement
*/
export function startSection(name: string) {
mark("start " + name);
}
/**
* Ends a performance measurement section.
* @param name name of the measurement
*/
export function endSection(name: string) {
mark("end " + name);
measure(name, "start " + name, "end " + name);
}
export function isMeasurementEnabled() {
return enabled;
}
export function enableMeasurement() {
if (!enabled) {
enabled = true;
}
}
export function disableMeasurement() {
if (enabled) {
enabled = false;
marks.clear();
durations.clear();
}
}
export function forEachMeasure(callback: (measureName: string, duration: number) => void) {
durations.forEach((duration, measureName) => callback(measureName, duration));
}
export function getTotalDuration() {
let total = 0;
forEachMeasure((_, duration) => (total += duration));
return total;
}