@@ -161,13 +161,15 @@ function isolationCommand(): string[] {
161161 * measures the benchmark like an isolated child and writes its samples to the
162162 * file named by {@link FORK_SAMPLES_FILE_ENV} instead of reporting results.
163163 */
164- export function runForks ( uri : string , forks : number ) : number [ ] {
164+ export async function runForks ( uri : string , forks : number ) : Promise < number [ ] > {
165165 const dir = mkdtempSync ( join ( tmpdir ( ) , "codspeed-forks-" ) ) ;
166166 try {
167167 let samples : number [ ] = [ ] ;
168168 for ( let fork = 0 ; fork < forks ; fork ++ ) {
169169 const file = join ( dir , `${ fork } .json` ) ;
170- const failure = runChild ( uri , { [ FORK_SAMPLES_FILE_ENV ] : file } ) ;
170+ const failure = await runChildAsync ( uri , {
171+ [ FORK_SAMPLES_FILE_ENV ] : file ,
172+ } ) ;
171173 if ( failure !== undefined ) {
172174 // process.exit() skips the `finally` below.
173175 rmSync ( dir , { recursive : true , force : true } ) ;
@@ -222,14 +224,11 @@ function runInChildProcess(uri: string): void {
222224}
223225
224226/** Runs the benchmark command limited to `uri`; the exit code if it failed. */
225- function runChild (
226- uri : string ,
227- env : NodeJS . ProcessEnv = { } ,
228- ) : number | undefined {
227+ function runChild ( uri : string ) : number | undefined {
229228 const [ executable , ...args ] = isolationCommand ( ) ;
230229 const result = spawnSync ( executable , args , {
231230 stdio : "inherit" ,
232- env : childEnv ( uri , env ) ,
231+ env : childEnv ( uri ) ,
233232 } ) ;
234233 if ( result . error !== undefined ) {
235234 throw startError ( uri , result . error ) ;
@@ -243,20 +242,28 @@ function runChild(
243242 * time out otherwise.
244243 */
245244async function runInChildProcessAsync ( uri : string ) : Promise < void > {
245+ const failure = await runChildAsync ( uri ) ;
246+ if ( failure !== undefined ) {
247+ process . exit ( failure ) ;
248+ }
249+ }
250+
251+ /** {@link runChild } without blocking the event loop. */
252+ async function runChildAsync (
253+ uri : string ,
254+ env : NodeJS . ProcessEnv = { } ,
255+ ) : Promise < number | undefined > {
246256 const [ executable , ...args ] = isolationCommand ( ) ;
247257 const child = spawn ( executable , args , {
248258 stdio : "inherit" ,
249- env : childEnv ( uri ) ,
259+ env : childEnv ( uri , env ) ,
250260 } ) ;
251261 const { promise, resolve, reject } =
252262 Promise . withResolvers < [ number | null , NodeJS . Signals | null ] > ( ) ;
253263 child . once ( "error" , ( error ) => reject ( startError ( uri , error ) ) ) ;
254264 child . once ( "close" , ( code , signal ) => resolve ( [ code , signal ] ) ) ;
255265 const [ status , signal ] = await promise ;
256- const failure = failureCode ( status , signal ) ;
257- if ( failure !== undefined ) {
258- process . exit ( failure ) ;
259- }
266+ return failureCode ( status , signal ) ;
260267}
261268
262269function childEnv ( uri : string , env : NodeJS . ProcessEnv = { } ) : NodeJS . ProcessEnv {
0 commit comments