Open History to see the last commit
1function codspeedRunnerMode() {
2 return process.env.CODSPEED_ENV === undefined
3 ? undefined
4 : process.env.CODSPEED_RUNNER_MODE
5}
6β
7/**
8 * Whether CodSpeed's simulation or memory instrument drives the run. Both
9 * measure one invocation after a few warmup calls instead of sampling.
10 */
11export function isCodSpeedAnalysis() {
12 const mode = codspeedRunnerMode()
13 return (
14 mode === 'simulation' || mode === 'instrumentation' || mode === 'memory'
15 )
16}
17β
18/** Extra worker flags for CodSpeed CPU simulation, not walltime benchmarks. */
19export function cpuSimulationExecArgv() {
20 const mode = codspeedRunnerMode()
21 if (mode === 'simulation' || mode === 'instrumentation') {
22 // CodSpeed supplies --no-opt, which only disables TurboFan on Node 24.
23 // Maglev can still compile and inline hot functions between measured runs.
24 // Compile baseline code on first use, so warmup does not leave functions
25 // near Sparkplug's later tier-up/batch-compilation thresholds.
26 // V8 renamed --scavenge-task in Node 20; CodSpeed's old flag is omitted.
27 // Keep minor GC tied to allocations instead of scheduled foreground tasks.
28 // Fix the initial/minimum old-generation allocation budget. This can skip
29 // automatic full collections below the budget; minor GC remains enabled.
30 return [
31 '--no-maglev',
32 '--always-sparkplug',
33 '--no-minor-gc-task',
34 '--initial-old-space-size=512',
35 ]
36 }
37 return []
38}
39β
40/**
41 * Per-bench lifecycle for apps that a bench prepares and disposes outside
42 * timing. CodSpeed measures one invocation, so each invocation gets fresh
43 * state. Tinybench samples thousands of iterations and keeps one instance per
44 * warmup/run phase instead of preparing it for every iteration.
45 */
46export function benchLifecycle(hooks: {
47 before: () => Promise<void> | void
48 after: () => Promise<void> | void
49}) {
50 return isCodSpeedAnalysis()
51 ? { beforeEach: hooks.before, afterEach: hooks.after }
52 : { beforeAll: hooks.before, afterAll: hooks.after }
53}