-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathperf.ts
More file actions
59 lines (53 loc) · 2.06 KB
/
perf.ts
File metadata and controls
59 lines (53 loc) · 2.06 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
import { nullRecord, nullTrace } from "./trace.ts";
import { PerfManager } from "./tracker.ts";
import type { PerfArea, PerformanceConfiguration } from "./types.ts";
let defaultManager: PerfManager | undefined = undefined;
/**
* Start tracking performance for the specified mode and configuration. Mode can be:
* - true to enable all tracking
* - a specific area to track (e.g. "metro", "resolve", "transform", "serialize")
* - a list of areas to track
* - undefined which will default to true and enable all tracking
* Calling multiple times will be additive in terms of areas tracked and will overwrite the configuration
*
* @param mode tracking modes to enable
* @param config performance configuration
*/
export function trackPerformance(
mode: true | PerfArea | PerfArea[] = true,
config?: PerformanceConfiguration
) {
if (!defaultManager) {
defaultManager = new PerfManager(config);
} else if (config) {
defaultManager.updateConfig(config);
}
defaultManager.enable(mode);
}
/**
* Finish tracking (rather than waiting for process exit) and print the report to the console.
* @param peekOnly if true, will print the report but continue tracking and still report out at process exit.
*/
export function reportPerfData(peekOnly = false) {
defaultManager?.report(peekOnly);
}
/**
* Check if tracking is enabled for a specific category. If category is undefined, checks if all tracking is enabled.
*/
export function isTrackingEnabled(category?: string) {
return defaultManager?.isEnabled(category) ?? false;
}
/**
* Get a trace function for the specified category. If not enabled it will be a non-tracking passthrough
* @param category category or undefined for unscoped
*/
export function getTrace(category?: string) {
return defaultManager?.getTrace(category) ?? nullTrace;
}
/**
* Get a recorder function for the specified category. If not enabled it will be a non-tracking no-op
* @param category category or undefined for unscoped
*/
export function getRecorder(category?: string) {
return defaultManager?.getRecorder(category) ?? nullRecord;
}