π File detail
utils/profilerBase.ts
π― Use case
This file lives under βutils/β, which covers cross-cutting helpers (shell, tempfiles, settings, messages, process input, β¦). On the API surface it exposes getPerformance, formatMs, and formatTimelineLine β mainly functions, hooks, or classes. Dependencies touch perf_hooks. It composes internal code from format (relative imports). What the file header says: Shared infrastructure for profiler modules (startupProfiler, queryProfiler, headlessProfiler). All three use the same perf_hooks timeline and the same line format for detailed reports.
Generated from folder role, exports, dependency roots, and inline comments β not hand-reviewed for every path.
π§ Inline summary
Shared infrastructure for profiler modules (startupProfiler, queryProfiler, headlessProfiler). All three use the same perf_hooks timeline and the same line format for detailed reports.
π€ Exports (heuristic)
getPerformanceformatMsformatTimelineLine
π External import roots
Package roots from from "β¦" (relative paths omitted).
perf_hooks
π₯οΈ Source preview
/**
* Shared infrastructure for profiler modules (startupProfiler, queryProfiler,
* headlessProfiler). All three use the same perf_hooks timeline and the same
* line format for detailed reports.
*/
import type { performance as PerformanceType } from 'perf_hooks'
import { formatFileSize } from './format.js'
// Lazy-load performance API only when profiling is enabled.
// Shared across all profilers β perf_hooks.performance is a process-wide singleton.
let performance: typeof PerformanceType | null = null
export function getPerformance(): typeof PerformanceType {
if (!performance) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
performance = require('perf_hooks').performance
}
return performance!
}
export function formatMs(ms: number): string {
return ms.toFixed(3)
}
/**
* Render a single timeline line in the shared profiler report format:
* [+ total.ms] (+ delta.ms) name [extra] [| RSS: .., Heap: ..]
*
* totalPad/deltaPad control the padStart width so callers can align columns
* based on their expected magnitude (startup uses 8/7, query uses 10/9).
*/
export function formatTimelineLine(
totalMs: number,
deltaMs: number,
name: string,
memory: NodeJS.MemoryUsage | undefined,
totalPad: number,
deltaPad: number,
extra = '',
): string {
const memInfo = memory
? ` | RSS: ${formatFileSize(memory.rss)}, Heap: ${formatFileSize(memory.heapUsed)}`
: ''
return `[+${formatMs(totalMs).padStart(totalPad)}ms] (+${formatMs(deltaMs).padStart(deltaPad)}ms) ${name}${extra}${memInfo}`
}