ktx/packages/cli/src/startup-profile.ts
Andrey Avtomonov 00cdf2de90
refactor: enforce ktx naming and AGENTS.md compliance sweep (#289)
Align the tree with AGENTS.md/CLAUDE.md conventions:

- Rewrite user-facing strings, docs, and tests to lowercase `ktx`
  (no bare uppercase `KTX` tokens remain outside literal identifiers).
- Drop the legacy `historicSql` migration path and its now-unused
  helpers, per the no-backward-compat rule.
- Remove `as unknown as` / `any` casts: narrow `BaseTool` generics to
  `z.ZodObject`, add a typed `createLookerClient`, and delete the dead
  `getParametersSchema`/`toAnthropicFormat` pre-AI-SDK helpers.
- Use `InvalidArgumentError` for Commander parse failures.
- Finish the adapter→connector prose conversion in the `ktx.yaml` docs
  while keeping the literal `adapters` config key.
2026-06-11 13:49:45 +02:00

54 lines
1.4 KiB
TypeScript

const enabled = process.env.KTX_PROFILE_STARTUP === '1' || process.env.KTX_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('\nktx 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`);
});
}