@@ -23,6 +23,13 @@ export const DEFAULT_WALLTIME_FORKS = 3;
2323export const ISOLATED_BENCHMARK_ENV = "CODSPEED_ISOLATED_BENCHMARK" ;
2424/** Set on each fork: the file where it writes its round samples. */
2525export const FORK_SAMPLES_FILE_ENV = "CODSPEED_FORK_SAMPLES_FILE" ;
26+ /**
27+ * Set on the forks after the first one by {@link runForks}: the number of
28+ * iterations that the first fork measured per round. Every fork of a benchmark
29+ * MUST batch the same way, because the samples of all forks are pooled.
30+ */
31+ export const FORK_ITERATIONS_PER_ROUND_ENV =
32+ "CODSPEED_FORK_ITERATIONS_PER_ROUND" ;
2633
2734/** Walltime settings that every integration exposes next to its own options. */
2835export interface WalltimeOptions {
@@ -40,8 +47,16 @@ export type IsolationMode =
4047 | { kind : "SharedProcess" }
4148 /** Every benchmark is measured in `forks` fresh processes, one after the other. */
4249 | { kind : "Forks" ; forks : number }
43- /** A fork: measures only `uri` and writes its samples to `samplesFile`. */
44- | { kind : "Fork" ; uri : string ; samplesFile : string } ;
50+ /**
51+ * A fork: measures only `uri` and writes its samples to `samplesFile`. The
52+ * first fork picks `iterationsPerRound` itself; the ones after it inherit it.
53+ */
54+ | {
55+ kind : "Fork" ;
56+ uri : string ;
57+ samplesFile : string ;
58+ iterationsPerRound ?: number ;
59+ } ;
4560
4661/**
4762 * The isolation of a walltime run, from the environment. `forks` is the
@@ -56,7 +71,16 @@ export function walltimeIsolation(forks?: number): IsolationMode {
5671 `[CodSpeed] ${ ISOLATED_BENCHMARK_ENV } is set without ${ FORK_SAMPLES_FILE_ENV } ` ,
5772 ) ;
5873 }
59- return { kind : "Fork" , uri, samplesFile } ;
74+ const iterationsPerRound = process . env [ FORK_ITERATIONS_PER_ROUND_ENV ] ;
75+ return {
76+ kind : "Fork" ,
77+ uri,
78+ samplesFile,
79+ iterationsPerRound :
80+ iterationsPerRound === undefined || iterationsPerRound === ""
81+ ? undefined
82+ : validPositiveInt ( iterationsPerRound , FORK_ITERATIONS_PER_ROUND_ENV ) ,
83+ } ;
6084 }
6185 const isolation = process . env [ PROCESS_ISOLATION_ENV ] ;
6286 if ( isolation === "0" || isolation === "false" ) {
@@ -77,22 +101,22 @@ export function walltimeIsolation(forks?: number): IsolationMode {
77101export function resolveWalltimeForks ( option ?: number ) : number {
78102 const env = process . env [ WALLTIME_FORKS_ENV ] ;
79103 if ( env !== undefined && env !== "" ) {
80- return validForks ( env , WALLTIME_FORKS_ENV ) ;
104+ return validPositiveInt ( env , WALLTIME_FORKS_ENV ) ;
81105 }
82106 if ( option !== undefined ) {
83- return validForks ( option , "forks" ) ;
107+ return validPositiveInt ( option , "forks" ) ;
84108 }
85109 return DEFAULT_WALLTIME_FORKS ;
86110}
87111
88- function validForks ( raw : string | number , source : string ) : number {
89- const forks = Number ( raw ) ;
90- if ( ! Number . isInteger ( forks ) || forks < 1 ) {
112+ function validPositiveInt ( raw : string | number , source : string ) : number {
113+ const value = Number ( raw ) ;
114+ if ( ! Number . isInteger ( value ) || value < 1 ) {
91115 throw new Error (
92116 `[CodSpeed] ${ source } must be a positive integer, got \`${ raw } \`` ,
93117 ) ;
94118 }
95- return forks ;
119+ return value ;
96120}
97121
98122/** The command of this process, which each fork re-runs. */
@@ -101,48 +125,68 @@ function currentCommand(): string[] {
101125 return [ process . execPath , ...process . execArgv , ...process . argv . slice ( 1 ) ] ;
102126}
103127
128+ /** The pooled samples of the forks of one benchmark. */
129+ export interface ForkSamples {
130+ /** Round samples (milliseconds), one per measured round. */
131+ samples : Float64Array ;
132+ /** Benchmark iterations one round measured; every fork MUST agree. */
133+ iterationsPerRound : number ;
134+ }
135+
104136/**
105137 * Runs the benchmark `uri` in `forks` fresh processes, one after the other,
106138 * and returns the round samples of all of them, in milliseconds. Throws when
107139 * a fork fails, without running the next one.
108140 *
141+ * The first fork picks how many iterations one round batches; the ones after
142+ * it inherit that count, so the pooled samples mean the same thing.
143+ *
109144 * Each fork re-runs this process's command, not fork(): Node can't fork, and a
110145 * fork would only copy the calling thread. Call it from the process whose
111146 * command runs the benchmarks: Vitest's main process, not a worker.
112147 */
113148export async function runForks (
114149 uri : string ,
115150 forks : number ,
116- ) : Promise < Float64Array > {
117- const samples : Float64Array [ ] = [ ] ;
151+ ) : Promise < ForkSamples > {
152+ const chunks : ForkSamples [ ] = [ ] ;
118153 for ( let fork = 0 ; fork < forks ; fork ++ ) {
119- samples . push ( await runFork ( uri ) ) ;
154+ chunks . push ( await runFork ( uri , chunks [ 0 ] ?. iterationsPerRound ) ) ;
120155 }
121- return concatSamples ( samples ) ;
156+ return poolForkSamples ( uri , chunks ) ;
122157}
123158
124159/** {@link runForks } for synchronous runs, which block anyway. */
125- export function runForksSync ( uri : string , forks : number ) : Float64Array {
126- const samples : Float64Array [ ] = [ ] ;
160+ export function runForksSync ( uri : string , forks : number ) : ForkSamples {
161+ const chunks : ForkSamples [ ] = [ ] ;
127162 for ( let fork = 0 ; fork < forks ; fork ++ ) {
128- samples . push ( runForkSync ( uri ) ) ;
163+ chunks . push ( runForkSync ( uri , chunks [ 0 ] ?. iterationsPerRound ) ) ;
129164 }
130- return concatSamples ( samples ) ;
165+ return poolForkSamples ( uri , chunks ) ;
131166}
132167
133168/**
134169 * Samples stay in typed arrays: a fast benchmark records tens of millions of
135170 * them per fork, which as JS numbers exceed the V8 heap limit of the parent.
136171 */
137- function concatSamples ( chunks : Float64Array [ ] ) : Float64Array {
138- const length = chunks . reduce ( ( sum , chunk ) => sum + chunk . length , 0 ) ;
172+ function poolForkSamples ( uri : string , chunks : ForkSamples [ ] ) : ForkSamples {
173+ const iterationsPerRound = chunks [ 0 ] ?. iterationsPerRound ?? 1 ;
174+ let length = 0 ;
175+ for ( const chunk of chunks ) {
176+ if ( chunk . iterationsPerRound !== iterationsPerRound ) {
177+ throw new Error (
178+ `[CodSpeed] the forks of ${ uri } measured a different number of iterations per round (${ iterationsPerRound } and ${ chunk . iterationsPerRound } )` ,
179+ ) ;
180+ }
181+ length += chunk . samples . length ;
182+ }
139183 const samples = new Float64Array ( length ) ;
140184 let offset = 0 ;
141185 for ( const chunk of chunks ) {
142- samples . set ( chunk , offset ) ;
143- offset += chunk . length ;
186+ samples . set ( chunk . samples , offset ) ;
187+ offset += chunk . samples . length ;
144188 }
145- return samples ;
189+ return { samples, iterationsPerRound } ;
146190}
147191
148192/** A fresh file per fork: overlapping runForks() calls must not share one. */
@@ -154,12 +198,15 @@ function newSamplesFile(): string {
154198}
155199
156200/** Runs one fork of `uri` and returns the samples it wrote. */
157- async function runFork ( uri : string ) : Promise < Float64Array > {
201+ async function runFork (
202+ uri : string ,
203+ iterationsPerRound ?: number ,
204+ ) : Promise < ForkSamples > {
158205 const samplesFile = newSamplesFile ( ) ;
159206 const [ executable , ...args ] = currentCommand ( ) ;
160207 const child = spawn ( executable , args , {
161208 stdio : "inherit" ,
162- env : forkEnv ( uri , samplesFile ) ,
209+ env : forkEnv ( uri , samplesFile , iterationsPerRound ) ,
163210 } ) ;
164211 const { promise, resolve, reject } =
165212 Promise . withResolvers < [ number | null , NodeJS . Signals | null ] > ( ) ;
@@ -169,24 +216,29 @@ async function runFork(uri: string): Promise<Float64Array> {
169216 return forkSamples ( uri , samplesFile , status , signal ) ;
170217}
171218
172- function runForkSync ( uri : string ) : Float64Array {
219+ function runForkSync ( uri : string , iterationsPerRound ?: number ) : ForkSamples {
173220 const samplesFile = newSamplesFile ( ) ;
174221 const [ executable , ...args ] = currentCommand ( ) ;
175222 const result = spawnSync ( executable , args , {
176223 stdio : "inherit" ,
177- env : forkEnv ( uri , samplesFile ) ,
224+ env : forkEnv ( uri , samplesFile , iterationsPerRound ) ,
178225 } ) ;
179226 if ( result . error !== undefined ) {
180227 throw startError ( uri , result . error ) ;
181228 }
182229 return forkSamples ( uri , samplesFile , result . status , result . signal ) ;
183230}
184231
185- function forkEnv ( uri : string , samplesFile : string ) : NodeJS . ProcessEnv {
232+ function forkEnv (
233+ uri : string ,
234+ samplesFile : string ,
235+ iterationsPerRound ?: number ,
236+ ) : NodeJS . ProcessEnv {
186237 return {
187238 ...process . env ,
188239 [ ISOLATED_BENCHMARK_ENV ] : uri ,
189240 [ FORK_SAMPLES_FILE_ENV ] : samplesFile ,
241+ [ FORK_ITERATIONS_PER_ROUND_ENV ] : iterationsPerRound ?. toString ( ) ,
190242 } ;
191243}
192244
@@ -202,15 +254,15 @@ function forkSamples(
202254 samplesFile : string ,
203255 status : number | null ,
204256 signal : NodeJS . Signals | null ,
205- ) : Float64Array {
257+ ) : ForkSamples {
206258 try {
207259 if ( status !== 0 ) {
208260 throw new Error (
209261 `[CodSpeed] the process for ${ uri } failed with ${ signal ?? `exit code ${ status } ` } ` ,
210262 ) ;
211263 }
212264 try {
213- return deserialize ( gunzipSync ( readFileSync ( samplesFile ) ) ) as Float64Array ;
265+ return deserialize ( gunzipSync ( readFileSync ( samplesFile ) ) ) as ForkSamples ;
214266 } catch ( error ) {
215267 throw new Error (
216268 `[CodSpeed] the process for ${ uri } did not report its samples: ${ ( error as Error ) . message } ` ,
@@ -221,15 +273,22 @@ function forkSamples(
221273 }
222274}
223275
224- /** Hands round samples (milliseconds) to the parent as gzip-compressed V8 data. */
276+ /**
277+ * Hands round samples (milliseconds) to the parent as gzip-compressed V8 data,
278+ * along with the number of benchmark iterations one round measured.
279+ */
225280export function writeForkSamples (
226281 samplesFile : string ,
227282 samples : ArrayLike < number > ,
283+ iterationsPerRound = 1 ,
228284) {
229285 writeFileSync (
230286 samplesFile ,
231- gzipSync ( serialize ( Float64Array . from ( samples ) ) , {
232- level : constants . Z_BEST_SPEED ,
233- } ) ,
287+ gzipSync (
288+ serialize ( { samples : Float64Array . from ( samples ) , iterationsPerRound } ) ,
289+ {
290+ level : constants . Z_BEST_SPEED ,
291+ } ,
292+ ) ,
234293 ) ;
235294}
0 commit comments