chore: write metrics to disk for better diffing

This commit is contained in:
Daniel Roe 2025-01-21 13:25:11 +00:00
parent d86acf5795
commit c5c6b8105c
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 20 additions and 2 deletions

View File

@ -1,8 +1,12 @@
// @ts-check // @ts-check
import { fileURLToPath } from 'node:url'
import { declare } from '@babel/helper-plugin-utils' import { declare } from '@babel/helper-plugin-utils'
import { types as t } from '@babel/core' 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 // inlined from https://github.com/danielroe/errx
function captureStackTrace () { function captureStackTrace () {
const IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[a-z]:[/\\]/i const IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[a-z]:[/\\]/i
@ -45,7 +49,9 @@ function captureStackTrace () {
return trace return trace
} }
export const leading = ` export const leading = `
import { writeFileSync as ____writeFileSync } from 'node:fs'
const ___captureStackTrace = ${captureStackTrace.toString()}; const ___captureStackTrace = ${captureStackTrace.toString()};
globalThis.___calls ||= {}; globalThis.___calls ||= {};
globalThis.___timings ||= {}; globalThis.___timings ||= {};
@ -55,6 +61,16 @@ function onExit () {
if (globalThis.___logged) { return } if (globalThis.___logged) { return }
globalThis.___logged = true 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 // worst by total time
const timings = Object.entries(globalThis.___timings) const timings = Object.entries(globalThis.___timings)
@ -93,7 +109,7 @@ function onExit () {
console.table(topFunctionsAverageTime) 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 */ /** @param {string} functionName */
export function generateInitCode (functionName) { export function generateInitCode (functionName) {

View File

@ -15,6 +15,8 @@ declare global {
var ___calls: Record<string, number> var ___calls: Record<string, number>
// eslint-disable-next-line no-var // eslint-disable-next-line no-var
var ___callers: Record<string, number> var ___callers: Record<string, number>
// eslint-disable-next-line no-var
var ____writeFileSync: typeof import('fs').writeFileSync
} }
export function AnnotateFunctionTimingsPlugin () { export function AnnotateFunctionTimingsPlugin () {
@ -29,7 +31,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 ? `${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 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