chore: improve debugging plugins

This commit is contained in:
Daniel Roe 2025-01-21 10:22:08 +00:00
parent a5f1c211e2
commit 492b1ec65b
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 10 additions and 10 deletions

View File

@ -60,7 +60,7 @@ function onExit () {
const topFunctionsTotalTime = timings const topFunctionsTotalTime = timings
.sort((a, b) => b[1] - a[1]) .sort((a, b) => b[1] - a[1])
.slice(0, 10) .slice(0, 20)
.map(([name, time]) => ({ .map(([name, time]) => ({
name, name,
time: Number(Number(time).toFixed(2)), time: Number(Number(time).toFixed(2)),
@ -69,7 +69,7 @@ function onExit () {
})) }))
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('Top 10 functions by total time:') console.log('Top 20 functions by total time:')
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.table(topFunctionsTotalTime) console.table(topFunctionsTotalTime)
@ -79,7 +79,7 @@ function onExit () {
.map(([name, time]) => [name, time / (globalThis.___calls[name] || 1)]) .map(([name, time]) => [name, time / (globalThis.___calls[name] || 1)])
// @ts-expect-error // @ts-expect-error
.sort((a, b) => b[1] - a[1]) .sort((a, b) => b[1] - a[1])
.slice(0, 10) .slice(0, 20)
.map(([name, time]) => ({ .map(([name, time]) => ({
name, name,
time: Number(Number(time).toFixed(2)), time: Number(Number(time).toFixed(2)),
@ -88,7 +88,7 @@ function onExit () {
})) }))
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('Top 10 functions by average time:') console.log('Top 20 functions by average time:')
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.table(topFunctionsAverageTime) console.table(topFunctionsAverageTime)
} }
@ -98,20 +98,20 @@ export const trailing = `process.on("exit", ${onExit.toString()})`
/** @param {string} functionName */ /** @param {string} functionName */
export function generateInitCode (functionName) { export function generateInitCode (functionName) {
return ` return `
___calls.${functionName} = (___calls.${functionName} || 0) + 1; ___calls[${JSON.stringify(functionName)}] = (___calls[${JSON.stringify(functionName)}] || 0) + 1;
___timings.${functionName} ||= 0; ___timings[${JSON.stringify(functionName)}] ||= 0;
const ___now = Date.now();` const ___now = Date.now();`
} }
/** @param {string} functionName */ /** @param {string} functionName */
export function generateFinallyCode (functionName) { export function generateFinallyCode (functionName) {
return ` return `
___timings.${functionName} += Date.now() - ___now; ___timings[${JSON.stringify(functionName)}] += Date.now() - ___now;
try { try {
const ___callee = ___captureStackTrace()[1]?.function; const ___callee = ___captureStackTrace()[1]?.function;
if (___callee) { if (___callee) {
___callers.${functionName} ||= {}; ___callers[${JSON.stringify(functionName)}] ||= {};
___callers.${functionName}[' ' + ___callee] = (___callers.${functionName}[' ' + ___callee] || 0) + 1; ___callers[${JSON.stringify(functionName)}][' ' + ___callee] = (___callers[${JSON.stringify(functionName)}][' ' + ___callee] || 0) + 1;
} }
} catch {}` } catch {}`
} }

View File

@ -29,7 +29,7 @@ export function AnnotateFunctionTimingsPlugin () {
walk(ast as Node, { walk(ast as Node, {
enter (node) { enter (node) {
if (node.type === 'FunctionDeclaration' && node.id && node.id.name) { if (node.type === 'FunctionDeclaration' && node.id && node.id.name) {
const functionName = node.id.name const functionName = node.id.name ? `${node.id.name} (${id.match(/\/packages\/([^/]+)\//)?.[1]})` : ''
const start = (node.body as Node & { start: number, end: number }).start const start = (node.body as Node & { start: number, end: number }).start
const end = (node.body as Node & { start: number, end: number }).end const end = (node.body as Node & { start: number, end: number }).end