mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-22 11:22:43 +00:00
chore: write metrics to disk for better diffing
This commit is contained in:
parent
d86acf5795
commit
c5c6b8105c
@ -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) {
|
||||
|
@ -15,6 +15,8 @@ declare global {
|
||||
var ___calls: Record<string, number>
|
||||
// eslint-disable-next-line no-var
|
||||
var ___callers: Record<string, number>
|
||||
// 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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user