-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathprofiling.ts
86 lines (76 loc) · 2.22 KB
/
profiling.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { debugPerformance } from '../../utils/environment-options';
let cumulativeDurations: Map<string, number[]> | undefined;
export function resetCumulativeDurations(): void {
cumulativeDurations?.clear();
}
export function logCumulativeDurations(): void {
if (!debugPerformance || !cumulativeDurations) {
return;
}
for (const [name, durations] of cumulativeDurations) {
let total = 0;
let min;
let max;
for (const duration of durations) {
total += duration;
if (min === undefined || duration < min) {
min = duration;
}
if (max === undefined || duration > max) {
max = duration;
}
}
const average = total / durations.length;
// eslint-disable-next-line no-console
console.log(
`DURATION[${name}]: ${total.toFixed(9)}s [count: ${durations.length}; avg: ${average.toFixed(
9,
)}s; min: ${min?.toFixed(9)}s; max: ${max?.toFixed(9)}s]`,
);
}
}
function recordDuration(name: string, startTime: bigint, cumulative?: boolean): void {
const duration = Number(process.hrtime.bigint() - startTime) / 10 ** 9;
if (cumulative) {
cumulativeDurations ??= new Map<string, number[]>();
const durations = cumulativeDurations.get(name) ?? [];
durations.push(duration);
cumulativeDurations.set(name, durations);
} else {
// eslint-disable-next-line no-console
console.log(`DURATION[${name}]: ${duration.toFixed(9)}s`);
}
}
export async function profileAsync<T>(
name: string,
action: () => Promise<T>,
cumulative?: boolean,
): Promise<T> {
if (!debugPerformance) {
return action();
}
const startTime = process.hrtime.bigint();
try {
return await action();
} finally {
recordDuration(name, startTime, cumulative);
}
}
export function profileSync<T>(name: string, action: () => T, cumulative?: boolean): T {
if (!debugPerformance) {
return action();
}
const startTime = process.hrtime.bigint();
try {
return action();
} finally {
recordDuration(name, startTime, cumulative);
}
}