11import {
22 InstrumentHooks ,
3+ Isolation ,
34 logDebug ,
45 mongoMeasurement ,
56 optimizeFunction ,
@@ -41,44 +42,50 @@ async function runAnalysisBench(
4142 suite : RunnerTestSuite ,
4243 currentSuiteName : string ,
4344 tinybenchModule : Tinybench ,
45+ isolation : Isolation ,
4446) {
4547 const uri = `${ currentSuiteName } ::${ benchmark . name } ` ;
46- const fn = getBenchFn ( benchmark ) ;
47-
48- // Constructing a Bench applies tinybench's no-op defaults for the setup and
49- // teardown hooks and gives them the Task they expect. The bench itself is
50- // never run: this runner drives the benchmark function directly.
51- const bench = new tinybenchModule . Bench ( getBenchOptions ( benchmark ) ) ;
52- const task = new tinybenchModule . Task ( bench , benchmark . name , fn ) ;
53-
54- await bench . setup ( task , "warmup" ) ;
55- await optimizeFunction ( async ( ) => {
48+ if ( ! isolation . shouldRun ( uri ) ) return ;
49+
50+ await isolation . run ( uri , async ( ) => {
51+ const fn = getBenchFn ( benchmark ) ;
52+
53+ // Constructing a Bench applies tinybench's no-op defaults for the setup and
54+ // teardown hooks and gives them the Task they expect. The bench itself is
55+ // never run: this runner drives the benchmark function directly.
56+ const bench = new tinybenchModule . Bench ( getBenchOptions ( benchmark ) ) ;
57+ const task = new tinybenchModule . Task ( bench , benchmark . name , fn ) ;
58+
59+ await bench . setup ( task , "warmup" ) ;
60+ await optimizeFunction ( async ( ) => {
61+ await callSuiteHook ( suite , benchmark , "beforeEach" ) ;
62+ await fn ( ) ;
63+ await callSuiteHook ( suite , benchmark , "afterEach" ) ;
64+ } ) ;
65+ await bench . teardown ( task , "warmup" ) ;
66+
67+ await bench . setup ( task , "run" ) ;
5668 await callSuiteHook ( suite , benchmark , "beforeEach" ) ;
57- await fn ( ) ;
69+ await mongoMeasurement . start ( uri ) ;
70+ global . gc ?.( ) ;
71+ await wrapWithRootFrame ( async ( ) => {
72+ InstrumentHooks . startBenchmark ( ) ;
73+ await fn ( ) ;
74+ InstrumentHooks . stopBenchmark ( ) ;
75+ InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
76+ } ) ( ) ;
77+ await mongoMeasurement . stop ( uri ) ;
5878 await callSuiteHook ( suite , benchmark , "afterEach" ) ;
79+ await bench . teardown ( task , "run" ) ;
80+
81+ logCodSpeed ( `${ uri } done` ) ;
5982 } ) ;
60- await bench . teardown ( task , "warmup" ) ;
61-
62- await bench . setup ( task , "run" ) ;
63- await callSuiteHook ( suite , benchmark , "beforeEach" ) ;
64- await mongoMeasurement . start ( uri ) ;
65- global . gc ?.( ) ;
66- await wrapWithRootFrame ( async ( ) => {
67- InstrumentHooks . startBenchmark ( ) ;
68- await fn ( ) ;
69- InstrumentHooks . stopBenchmark ( ) ;
70- InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
71- } ) ( ) ;
72- await mongoMeasurement . stop ( uri ) ;
73- await callSuiteHook ( suite , benchmark , "afterEach" ) ;
74- await bench . teardown ( task , "run" ) ;
75-
76- logCodSpeed ( `${ uri } done` ) ;
7783}
7884
7985async function runAnalysisBenchmarkSuite (
8086 suite : RunnerTestSuite ,
8187 tinybenchModule : Tinybench ,
88+ isolation : Isolation ,
8289 parentSuiteName ?: string ,
8390) {
8491 const currentSuiteName = parentSuiteName
@@ -91,9 +98,20 @@ async function runAnalysisBenchmarkSuite(
9198 if ( task . mode !== "run" ) continue ;
9299
93100 if ( task . type === "suite" ) {
94- await runAnalysisBenchmarkSuite ( task , tinybenchModule , currentSuiteName ) ;
101+ await runAnalysisBenchmarkSuite (
102+ task ,
103+ tinybenchModule ,
104+ isolation ,
105+ currentSuiteName ,
106+ ) ;
95107 } else if ( isVitestTaskBenchmark ( task ) ) {
96- await runAnalysisBench ( task , suite , currentSuiteName , tinybenchModule ) ;
108+ await runAnalysisBench (
109+ task ,
110+ suite ,
111+ currentSuiteName ,
112+ tinybenchModule ,
113+ isolation ,
114+ ) ;
97115 }
98116 }
99117
@@ -107,9 +125,24 @@ export class AnalysisRunner extends NodeBenchmarkRunner {
107125
108126 patchRootSuiteWithFullFilePath ( suite ) ;
109127
110- logCodSpeed ( `running suite ${ suite . name } ` ) ;
111- await runAnalysisBenchmarkSuite ( suite , await this . importTinybench ( ) ) ;
112- logCodSpeed ( `running suite ${ suite . name } done` ) ;
128+ // Without instrumentation nothing is measured, so there is nothing to isolate.
129+ const isolation = InstrumentHooks . isInstrumented ( )
130+ ? Isolation . current ( )
131+ : Isolation . shared ( ) ;
132+ // The parent already printed the suite lines, children stay quiet.
133+ const isChild = isolation . isChild ( ) ;
134+
135+ if ( ! isChild ) {
136+ logCodSpeed ( `running suite ${ suite . name } ` ) ;
137+ }
138+ await runAnalysisBenchmarkSuite (
139+ suite ,
140+ await this . importTinybench ( ) ,
141+ isolation ,
142+ ) ;
143+ if ( ! isChild ) {
144+ logCodSpeed ( `running suite ${ suite . name } done` ) ;
145+ }
113146
114147 teardownCore ( ) ;
115148 }
0 commit comments