11// Benchmarks `pnpm test:changed` planning/runtime behavior across repeated runs.
22import { spawnSync } from "node:child_process" ;
33import path from "node:path" ;
4+ import { pathToFileURL } from "node:url" ;
45import { parseFlagArgs , stringFlag } from "./lib/arg-utils.mjs" ;
56import { formatMs } from "./lib/vitest-report-cli-utils.mjs" ;
67
@@ -35,7 +36,7 @@ function positiveIntegerFlag(flag, key) {
3536 } ;
3637}
3738
38- function parseArgs ( argv ) {
39+ export function parseArgs ( argv ) {
3940 const args = parseFlagArgs (
4041 argv ,
4142 {
@@ -103,16 +104,16 @@ function listChangedPaths(opts) {
103104 return runGitList ( [ "diff" , "--name-only" , `${ opts . ref } ...HEAD` ] , opts . cwd ) ;
104105}
105106
106- function parseMaxRssKb ( output ) {
107+ export function parseMaxRssBytes ( output ) {
107108 const match = output . match ( / ( \d + ) \s + m a x i m u m r e s i d e n t s e t s i z e / u) ;
108109 return match ? Number . parseInt ( match [ 1 ] , 10 ) : null ;
109110}
110111
111- function formatRss ( valueKb ) {
112- if ( valueKb === null ) {
112+ export function formatRss ( valueBytes ) {
113+ if ( valueBytes === null ) {
113114 return "n/a" ;
114115 }
115- return `${ ( valueKb / 1024 ) . toFixed ( 1 ) } MB` ;
116+ return `${ ( valueBytes / 1024 / 1024 ) . toFixed ( 1 ) } MB` ;
116117}
117118
118119function runBenchCommand ( params ) {
@@ -136,7 +137,7 @@ function runBenchCommand(params) {
136137 const output = `${ result . stdout ?? "" } ${ result . stderr ?? "" } ` ;
137138 return {
138139 elapsedMs,
139- maxRssKb : params . rss ? parseMaxRssKb ( output ) : null ,
140+ maxRssBytes : params . rss ? parseMaxRssBytes ( output ) : null ,
140141 status : result . status ?? 1 ,
141142 output,
142143 } ;
@@ -145,81 +146,92 @@ function runBenchCommand(params) {
145146function printRunSummary ( label , result ) {
146147 console . log (
147148 `${ label . padEnd ( 8 , " " ) } wall=${ formatMs ( result . elapsedMs ) . padStart ( 9 , " " ) } rss=${ formatRss (
148- result . maxRssKb ,
149+ result . maxRssBytes ,
149150 ) . padStart ( 9 , " " ) } `,
150151 ) ;
151152}
152153
153- let opts ;
154- try {
155- opts = parseArgs ( process . argv . slice ( 2 ) ) ;
156- } catch ( error ) {
157- console . error ( error instanceof Error ? error . message : String ( error ) ) ;
158- process . exit ( 1 ) ;
159- }
160- const changedPaths = listChangedPaths ( opts ) ;
161- if ( changedPaths . length === 0 ) {
154+ function main ( ) {
155+ let opts ;
156+ try {
157+ opts = parseArgs ( process . argv . slice ( 2 ) ) ;
158+ } catch ( error ) {
159+ console . error ( error instanceof Error ? error . message : String ( error ) ) ;
160+ process . exit ( 1 ) ;
161+ }
162+ const changedPaths = listChangedPaths ( opts ) ;
163+ if ( changedPaths . length === 0 ) {
164+ console . log (
165+ opts . mode === "worktree"
166+ ? "[bench-test-changed] no changed paths in worktree"
167+ : `[bench-test-changed] no changed paths for ${ opts . ref } ...HEAD` ,
168+ ) ;
169+ process . exit ( 0 ) ;
170+ }
171+
162172 console . log (
163173 opts . mode === "worktree"
164- ? "[bench-test-changed] no changed paths in worktree"
165- : `[bench-test-changed] no changed paths for ${ opts . ref } ...HEAD ` ,
174+ ? "[bench-test-changed] mode= worktree"
175+ : `[bench-test-changed] ref= ${ opts . ref } ` ,
166176 ) ;
167- process . exit ( 0 ) ;
168- }
177+ console . log ( "[bench-test-changed] changed paths:" ) ;
178+ for ( const changedPath of changedPaths ) {
179+ console . log ( `- ${ changedPath } ` ) ;
180+ }
169181
170- console . log (
171- opts . mode === "worktree"
172- ? "[bench-test-changed] mode=worktree"
173- : `[bench-test-changed] ref=${ opts . ref } ` ,
174- ) ;
175- console . log ( "[bench-test-changed] changed paths:" ) ;
176- for ( const changedPath of changedPaths ) {
177- console . log ( `- ${ changedPath } ` ) ;
178- }
182+ const routedCommand =
183+ opts . mode === "worktree"
184+ ? [ process . execPath , "scripts/test-projects.mjs" , ...changedPaths ]
185+ : [ process . execPath , "scripts/test-projects.mjs" , "--changed" , opts . ref ] ;
186+ const rootCommand = [
187+ process . execPath ,
188+ "scripts/run-vitest.mjs" ,
189+ "run" ,
190+ "--config" ,
191+ "vitest.config.ts" ,
192+ ...changedPaths ,
193+ ] ;
194+
195+ console . log ( `[bench-test-changed] routed: ${ routedCommand . map ( quoteArg ) . join ( " " ) } ` ) ;
196+ const routed = runBenchCommand ( {
197+ command : routedCommand ,
198+ cwd : opts . cwd ,
199+ rss : opts . rss ,
200+ ...( typeof opts . maxWorkers === "number" ? { maxWorkers : opts . maxWorkers } : { } ) ,
201+ } ) ;
202+ if ( routed . status !== 0 ) {
203+ process . stderr . write ( routed . output ) ;
204+ process . exit ( routed . status ) ;
205+ }
179206
180- const routedCommand =
181- opts . mode === "worktree"
182- ? [ process . execPath , "scripts/test-projects.mjs" , ...changedPaths ]
183- : [ process . execPath , "scripts/test-projects.mjs" , "--changed" , opts . ref ] ;
184- const rootCommand = [
185- process . execPath ,
186- "scripts/run-vitest.mjs" ,
187- "run" ,
188- "--config" ,
189- "vitest.config.ts" ,
190- ...changedPaths ,
191- ] ;
192-
193- console . log ( `[bench-test-changed] routed: ${ routedCommand . map ( quoteArg ) . join ( " " ) } ` ) ;
194- const routed = runBenchCommand ( {
195- command : routedCommand ,
196- cwd : opts . cwd ,
197- rss : opts . rss ,
198- ...( typeof opts . maxWorkers === "number" ? { maxWorkers : opts . maxWorkers } : { } ) ,
199- } ) ;
200- if ( routed . status !== 0 ) {
201- process . stderr . write ( routed . output ) ;
202- process . exit ( routed . status ) ;
203- }
207+ console . log ( `[bench-test-changed] root: ${ rootCommand . map ( quoteArg ) . join ( " " ) } ` ) ;
208+ const root = runBenchCommand ( {
209+ command : rootCommand ,
210+ cwd : opts . cwd ,
211+ rss : opts . rss ,
212+ ...( typeof opts . maxWorkers === "number" ? { maxWorkers : opts . maxWorkers } : { } ) ,
213+ } ) ;
214+ if ( root . status !== 0 ) {
215+ process . stderr . write ( root . output ) ;
216+ process . exit ( root . status ) ;
217+ }
204218
205- console . log ( `[bench-test-changed] root: ${ rootCommand . map ( quoteArg ) . join ( " " ) } ` ) ;
206- const root = runBenchCommand ( {
207- command : rootCommand ,
208- cwd : opts . cwd ,
209- rss : opts . rss ,
210- ...( typeof opts . maxWorkers === "number" ? { maxWorkers : opts . maxWorkers } : { } ) ,
211- } ) ;
212- if ( root . status !== 0 ) {
213- process . stderr . write ( root . output ) ;
214- process . exit ( root . status ) ;
219+ printRunSummary ( "routed" , routed ) ;
220+ printRunSummary ( "root" , root ) ;
221+ console . log (
222+ `[bench-test-changed] delta wall=${ formatMs ( root . elapsedMs - routed . elapsedMs ) } rss=${
223+ routed . maxRssBytes !== null && root . maxRssBytes !== null
224+ ? formatRss ( root . maxRssBytes - routed . maxRssBytes )
225+ : "n/a"
226+ } `,
227+ ) ;
215228}
216229
217- printRunSummary ( "routed" , routed ) ;
218- printRunSummary ( "root" , root ) ;
219- console . log (
220- `[bench-test-changed] delta wall=${ formatMs ( root . elapsedMs - routed . elapsedMs ) } rss=${
221- routed . maxRssKb !== null && root . maxRssKb !== null
222- ? formatRss ( root . maxRssKb - routed . maxRssKb )
223- : "n/a"
224- } `,
225- ) ;
230+ const isMain =
231+ typeof process . argv [ 1 ] === "string" &&
232+ process . argv [ 1 ] . length > 0 &&
233+ import . meta. url === pathToFileURL ( path . resolve ( process . argv [ 1 ] ) ) . href ;
234+
235+ if ( isMain ) {
236+ main ( ) ;
237+ }
0 commit comments