mirror of
https://github.com/Kaelio/ktx.git
synced 2026-07-01 08:59:39 +02:00
Initial open-source release
This commit is contained in:
commit
1a42152e6f
1199 changed files with 257054 additions and 0 deletions
54
packages/cli/src/startup-profile.ts
Normal file
54
packages/cli/src/startup-profile.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
const enabled = process.env.KLO_PROFILE_STARTUP === '1' || process.env.KLO_PROFILE_STARTUP === 'true';
|
||||
const processStart = performance.now() - process.uptime() * 1000;
|
||||
|
||||
interface StartupProfileEvent {
|
||||
label: string;
|
||||
at: number;
|
||||
duration?: number;
|
||||
}
|
||||
|
||||
const events: StartupProfileEvent[] = [];
|
||||
|
||||
function now(): number {
|
||||
return performance.now() - processStart;
|
||||
}
|
||||
|
||||
export function profileMark(label: string): void {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
events.push({ label, at: now() });
|
||||
}
|
||||
|
||||
export async function profileSpan<T>(label: string, run: () => Promise<T>): Promise<T> {
|
||||
if (!enabled) {
|
||||
return await run();
|
||||
}
|
||||
const start = now();
|
||||
try {
|
||||
return await run();
|
||||
} finally {
|
||||
events.push({ label, at: start, duration: now() - start });
|
||||
}
|
||||
}
|
||||
|
||||
export function installStartupProfileReporter(): void {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
process.once('beforeExit', () => {
|
||||
const total = now();
|
||||
process.stderr.write('\nKLO startup profile\n');
|
||||
for (const event of events) {
|
||||
const elapsed = event.at.toFixed(1).padStart(7);
|
||||
if (event.duration === undefined) {
|
||||
process.stderr.write(`${elapsed} ms ${event.label}\n`);
|
||||
} else {
|
||||
const duration = event.duration.toFixed(1).padStart(7);
|
||||
process.stderr.write(`${elapsed} ms ${duration} ms ${event.label}\n`);
|
||||
}
|
||||
}
|
||||
process.stderr.write(`${total.toFixed(1).padStart(7)} ms total\n`);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue