From c5c6b8105c76f13d187f80a90c5eb69de26eebf3 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 21 Jan 2025 13:25:11 +0000 Subject: [PATCH] chore: write metrics to disk for better diffing --- debug/plugins/timings-babel.mjs | 18 +++++++++++++++++- debug/plugins/timings-unbuild.ts | 4 +++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/debug/plugins/timings-babel.mjs b/debug/plugins/timings-babel.mjs index 3c5a56236d..59e76b78e6 100644 --- a/debug/plugins/timings-babel.mjs +++ b/debug/plugins/timings-babel.mjs @@ -1,8 +1,12 @@ // @ts-check +import { fileURLToPath } from 'node:url' + import { declare } from '@babel/helper-plugin-utils' import { types as t } from '@babel/core' +const metricsPath = fileURLToPath(new URL('../../debug-timings.json', import.meta.url)) + // inlined from https://github.com/danielroe/errx function captureStackTrace () { const IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[a-z]:[/\\]/i @@ -45,7 +49,9 @@ function captureStackTrace () { return trace } + export const leading = ` +import { writeFileSync as ____writeFileSync } from 'node:fs' const ___captureStackTrace = ${captureStackTrace.toString()}; globalThis.___calls ||= {}; globalThis.___timings ||= {}; @@ -55,6 +61,16 @@ function onExit () { if (globalThis.___logged) { return } globalThis.___logged = true + // eslint-disable-next-line no-undef + ____writeFileSync(metricsPath, JSON.stringify(Object.fromEntries(Object.entries(globalThis.___timings).map(([name, time]) => [ + name, + { + time: Number(Number(time).toFixed(2)), + calls: globalThis.___calls[name], + callers: globalThis.___callers[name] ? Object.fromEntries(Object.entries(globalThis.___callers[name]).map(([name, count]) => [name.trim(), count]).sort((a, b) => typeof b[0] === 'string' && typeof a[0] === 'string' ? a[0].localeCompare(b[0]) : 0)) : undefined, + }, + ]).sort((a, b) => typeof b[0] === 'string' && typeof a[0] === 'string' ? a[0].localeCompare(b[0]) : 0)), null, 2)) + // worst by total time const timings = Object.entries(globalThis.___timings) @@ -93,7 +109,7 @@ function onExit () { console.table(topFunctionsAverageTime) } -export const trailing = `process.on("exit", ${onExit.toString()})` +export const trailing = `process.on("exit", ${onExit.toString().replace('metricsPath', JSON.stringify(metricsPath))})` /** @param {string} functionName */ export function generateInitCode (functionName) { diff --git a/debug/plugins/timings-unbuild.ts b/debug/plugins/timings-unbuild.ts index 09574dd347..d1fdd5df8d 100644 --- a/debug/plugins/timings-unbuild.ts +++ b/debug/plugins/timings-unbuild.ts @@ -15,6 +15,8 @@ declare global { var ___calls: Record // eslint-disable-next-line no-var var ___callers: Record + // eslint-disable-next-line no-var + var ____writeFileSync: typeof import('fs').writeFileSync } export function AnnotateFunctionTimingsPlugin () { @@ -29,7 +31,7 @@ export function AnnotateFunctionTimingsPlugin () { walk(ast as Node, { enter (node) { if (node.type === 'FunctionDeclaration' && node.id && node.id.name) { - const functionName = node.id.name ? `${node.id.name} (${id.match(/\/packages\/([^/]+)\//)?.[1]})` : '' + const functionName = node.id.name ? `${node.id.name} (${id.match(/[\\/]packages[\\/]([^/]+)[\\/]/)?.[1]})` : '' const start = (node.body as Node & { start: number, end: number }).start const end = (node.body as Node & { start: number, end: number }).end