Open History to see the last commit
1#!/usr/bin/env node
2β
3import { spawnSync } from 'node:child_process'
4import fs from 'node:fs'
5import path from 'node:path'
6import { fileURLToPath, pathToFileURL } from 'node:url'
7import { parseArgs } from 'node:util'
8β
9const scriptDir = path.dirname(fileURLToPath(import.meta.url))
10const repoRoot = path.resolve(scriptDir, '../../..')
11β
12/**
13 * @typedef {object} CommandOptions
14 * @property {string} cwd
15 * @property {NodeJS.ProcessEnv} env
16 * @property {'inherit' | ['ignore', number, number]} stdio
17 * @typedef {(command: string, args: string[], options: CommandOptions) => {status: number | null, error?: Error, signal?: string | null}} Execute
18 */
19β
20/**
21 * @param {string} resultsRoot
22 * @param {string} name
23 */
24function namedRunDir(resultsRoot, name) {
25 if (!/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/.test(name)) {
26 throw new Error(
27 'Run names must start with a letter or digit and contain only letters, digits, hyphens, or underscores.',
28 )
29 }
30 return path.join(resultsRoot, 'runs', name)
31}
32β
33/**
34 * @param {string[]} argv
35 * @param {Execute} [execute]
36 */
37export function run(argv, execute = spawnSync) {
38 const separator = argv.indexOf('--')
39 const testArgs = separator === -1 ? [] : argv.slice(separator + 1)
40 const { values } = parseArgs({
41 args: separator === -1 ? argv : argv.slice(0, separator),
42 allowPositionals: false,
43 options: {
44 name: { type: 'string' },
45 baseline: { type: 'string' },
46 'test-projects': { type: 'string' },
47 scenario: { type: 'string' },
48 'results-dir': { type: 'string' },
49 'dist-dir': { type: 'string' },
50 analysis: { type: 'boolean' },
51 sourcemap: { type: 'boolean' },
52 timings: { type: 'boolean' },
53 'skip-package-builds': { type: 'boolean' },
54 help: { type: 'boolean', short: 'h' },
55 },
56 })
57β
58 if (values.help) {
59 console.log(`Usage: pnpm benchmark:bundle-size:run [options] [-- test arguments]
60β
61 --scenario <ids> Comma-separated scenarios; omit for all scenarios
62 --name <name> Save a named run; existing results are not overwritten
63 --baseline <name|file> Compare against a named run or a current.json file
64 --test-projects <names> Run Nx unit tests before measurement
65 --results-dir <dir> Results root (default: benchmarks/bundle-size/results)
66 --dist-dir <dir> Override the emitted bundle directory
67 --analysis Include source attribution in current.json
68 --sourcemap Emit hidden source maps
69 --timings Record fresh phase timings in measure.log
70 --skip-package-builds Reuse package builds only when they are unchanged
71β
72Named runs use <results-root>/runs/<name>/current.json.
73Without --name, current.json is replaced in the results root.
74Logs stay beside current.json; failures print a bounded log tail.
75With --scenario, comparisons report only the scenarios measured in this run.
76Arguments after -- go to the selected unit tests, not the measurement.`)
77 return 0
78 }
79β
80 if (testArgs.length > 0 && !values['test-projects']) {
81 throw new Error('Test arguments after -- require --test-projects.')
82 }
83 if (
84 values['test-projects'] !== undefined &&
85 !values['test-projects'].split(',').some((name) => name.trim())
86 ) {
87 throw new Error('--test-projects requires at least one project.')
88 }
89 if (
90 values.scenario !== undefined &&
91 !values.scenario.split(',').some((name) => name.trim())
92 ) {
93 throw new Error('--scenario requires at least one scenario.')
94 }
95β
96 const resultsRoot = values['results-dir']
97 ? path.resolve(values['results-dir'])
98 : path.join(repoRoot, 'benchmarks/bundle-size/results')
99 const resultsDir =
100 values.name === undefined
101 ? resultsRoot
102 : namedRunDir(resultsRoot, values.name)
103 const currentPath = path.join(resultsDir, 'current.json')
104 let baselinePath
105β
106 if (values.name !== undefined && fs.existsSync(currentPath)) {
107 throw new Error(`Named run already exists: ${values.name}`)
108 }
109 if (values.baseline !== undefined) {
110 baselinePath = fs.realpathSync(
111 /[/\\]|\.json$/i.test(values.baseline)
112 ? path.resolve(values.baseline)
113 : path.join(namedRunDir(resultsRoot, values.baseline), 'current.json'),
114 )
115 const baseline = JSON.parse(fs.readFileSync(baselinePath, 'utf8'))
116 if (!Array.isArray(baseline.metrics) || baseline.metrics.length === 0) {
117 throw new Error(`Baseline contains no metrics: ${baselinePath}`)
118 }
119 if (
120 fs.existsSync(currentPath) &&
121 fs.realpathSync(currentPath) === baselinePath
122 ) {
123 throw new Error(
124 'The baseline cannot be the current output. Use --name to save it first.',
125 )
126 }
127 }
128β
129 fs.mkdirSync(resultsDir, { recursive: true })
130 const env = {
131 ...process.env,
132 CI: '1',
133 NX_DAEMON: 'false',
134 FORCE_COLOR: '0',
135 }
136β
137 /**
138 * @param {string} label
139 * @param {string} command
140 * @param {string[]} args
141 */
142 function step(label, command, args, logged = true) {
143 const logPath = path.join(resultsDir, `${label}.log`)
144 const fd = logged ? fs.openSync(logPath, 'w') : undefined
145 let result
146 try {
147 if (logged) {
148 console.error(`${label}: ${path.relative(repoRoot, logPath)}`)
149 }
150 result = execute(command, args, {
151 cwd: repoRoot,
152 env,
153 stdio: fd === undefined ? 'inherit' : ['ignore', fd, fd],
154 })
155 } finally {
156 if (fd !== undefined) {
157 fs.closeSync(fd)
158 }
159 }
160β
161 if (result.error || result.status !== 0) {
162 console.error(
163 `${label} failed: ${result.error?.message || result.signal || `exit ${result.status}`}`,
164 )
165 if (logged) {
166 const tail = fs
167 .readFileSync(logPath, 'utf8')
168 .slice(-8000)
169 .trimEnd()
170 .split(/\r?\n/)
171 .slice(-40)
172 .join('\n')
173 if (tail) {
174 console.error(tail)
175 }
176 }
177 return result.status || 1
178 }
179 return 0
180 }
181β
182 if (values['test-projects']) {
183 const pnpmPath = process.env.npm_execpath
184 const status = step(
185 'tests',
186 pnpmPath
187 ? process.execPath
188 : process.platform === 'win32'
189 ? 'pnpm.cmd'
190 : 'pnpm',
191 [
192 ...(pnpmPath ? [pnpmPath] : []),
193 'nx',
194 'run-many',
195 '--target=test:unit',
196 `--projects=${values['test-projects']}`,
197 '--parallel=1',
198 '--outputStyle=stream',
199 '--skipRemoteCache',
200 ...(testArgs.length > 0 ? ['--', ...testArgs] : []),
201 ],
202 )
203 if (status !== 0) {
204 return status
205 }
206 }
207β
208 const measureArgs = [
209 path.join(scriptDir, 'measure.mjs'),
210 '--results-dir',
211 resultsDir,
212 ]
213 for (const [option, value] of Object.entries({
214 scenario: values.scenario,
215 'dist-dir':
216 values['dist-dir'] === undefined
217 ? undefined
218 : path.resolve(values['dist-dir']),
219 })) {
220 if (value !== undefined) {
221 measureArgs.push(`--${option}`, value)
222 }
223 }
224 for (const [option, value] of Object.entries({
225 analysis: values.analysis,
226 sourcemap: values.sourcemap,
227 timings: values.timings,
228 'skip-package-builds': values['skip-package-builds'],
229 })) {
230 if (value) {
231 measureArgs.push(`--${option}`)
232 }
233 }
234β
235 const status = step('measure', process.execPath, measureArgs)
236 if (status !== 0) {
237 return status
238 }
239β
240 return step(
241 'report',
242 process.execPath,
243 [
244 path.join(scriptDir, baselinePath ? 'diff.mjs' : 'query.mjs'),
245 '--current',
246 currentPath,
247 ...(baselinePath ? ['--baseline', baselinePath] : []),
248 ...(baselinePath && values.scenario ? ['--current-only'] : []),
249 ],
250 false,
251 )
252}
253β
254if (
255 process.argv[1] &&
256 import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href
257) {
258 try {
259 process.exitCode = run(process.argv.slice(2))
260 } catch (error) {
261 console.error(error instanceof Error ? error.message : error)
262 process.exitCode = 1
263 }
264}