MCPcopy Create free account
hub / github.com/code-pushup/cli / Profiler

Class Profiler

packages/utils/src/lib/profiler/profiler.ts:71–240  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

69 *
70 */
71export class Profiler<T extends ActionTrackConfigs> {
72 static instanceCount = 0;
73 readonly id = getProfilerId();
74 #enabled: boolean = false;
75 readonly #defaults: ActionTrackEntryPayload;
76 readonly tracks: Record<keyof T, ActionTrackEntryPayload> | undefined;
77 readonly #ctxOf: ReturnType<typeof measureCtx>;
78
79 /**
80 * Creates a new Profiler instance with the specified configuration.
81 *
82 * @param options - Configuration options for the profiler
83 * @param options.tracks - Custom track configurations merged with defaults
84 * @param options.prefix - Prefix for all measurement names
85 * @param options.track - Default track name for measurements
86 * @param options.trackGroup - Default track group for organization
87 * @param options.color - Default color for track entries
88 * @param options.enabled - Whether profiling is enabled (defaults to CP_PROFILING env var)
89 *
90 */
91 constructor(options: ProfilerOptions<T>) {
92 const { tracks, prefix, enabled, ...defaults } = options;
93 const dataType = 'track-entry';
94
95 this.#enabled = enabled ?? isEnvVarEnabled(PROFILER_ENABLED_ENV_VAR);
96 this.#defaults = { ...defaults, dataType };
97 this.tracks = tracks
98 ? setupTracks({ ...defaults, dataType }, tracks)
99 : undefined;
100 this.#ctxOf = measureCtx({
101 ...defaults,
102 dataType,
103 prefix,
104 });
105 }
106
107 /**
108 * Sets enabled state for this profiler.
109 *
110 * Also sets the `CP_PROFILING` environment variable.
111 * This means any future {@link Profiler} instantiations (including child processes) will use the same enabled state.
112 *
113 * @param enabled - Whether profiling should be enabled
114 */
115 setEnabled(enabled: boolean): void {
116 process.env[PROFILER_ENABLED_ENV_VAR] = `${enabled}`;
117 this.#enabled = enabled;
118 }
119
120 /**
121 * Is profiling enabled?
122 *
123 * Profiling is enabled by {@link setEnabled} call or `CP_PROFILING` environment variable.
124 *
125 * @returns Whether profiling is currently enabled
126 */
127 isEnabled(): boolean {
128 return this.#enabled;

Callers

nothing calls this directly

Calls 1

getProfilerIdFunction · 0.85

Tested by

no test coverage detected