11import {
22 calculateQuantiles ,
3+ InstrumentHooks ,
34 mongoMeasurement ,
45 msToNs ,
56 msToS ,
@@ -40,7 +41,11 @@ export function runWalltimeBench(bench: Bench, rootCallingFile: string): void {
4041 await task . warmup ( ) ;
4142 }
4243 await mongoMeasurement . start ( uri ) ;
43- const taskResult = await task . run ( ) ;
44+ InstrumentHooks . startBenchmark ( ) ;
45+ const taskResult = await InstrumentHooks . __codspeed_root_frame__ ( ( ) =>
46+ task . run ( )
47+ ) ;
48+ InstrumentHooks . stopBenchmark ( ) ;
4449 await mongoMeasurement . stop ( uri ) ;
4550 results . push ( taskResult ) ;
4651
@@ -67,8 +72,85 @@ export function runWalltimeBench(bench: Bench, rootCallingFile: string): void {
6772 } ;
6873
6974 benchmarks . push ( benchmark ) ;
75+ console . log ( ` ✔ Collected walltime data for ${ uri } ` ) ;
76+ InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
77+ } else {
78+ console . warn ( ` ⚠ No result data available for ${ uri } ` ) ;
79+ }
80+ }
81+
82+ // Write results to JSON file using core function
83+ if ( benchmarks . length > 0 ) {
84+ writeWalltimeResults ( benchmarks ) ;
85+ }
86+
87+ console . log (
88+ `[CodSpeed] Done collecting walltime data for ${ bench . tasks . length } benches.`
89+ ) ;
90+ // Restore our custom run method
91+ bench . run = originalRun ;
92+
93+ return results ;
94+ } ;
95+
96+ bench . runSync = ( ) => {
97+ console . log (
98+ `[CodSpeed] running with @codspeed/tinybench v${ __VERSION__ } (walltime mode)`
99+ ) ;
100+
101+ // Store the original run method before we override it
102+ const originalRun = bench . run ;
70103
104+ // Temporarily restore the original run to get actual benchmark results
105+ const benchProto = Object . getPrototypeOf ( bench ) ;
106+ const prototypeRun = benchProto . run ;
107+ bench . run = prototypeRun ;
108+
109+ const benchmarks : Benchmark [ ] = [ ] ;
110+
111+ // Run the bench naturally to collect TaskResult data
112+ const results = [ ] ;
113+
114+ // Collect and report walltime data
115+ for ( const task of bench . tasks ) {
116+ const uri = getTaskUri ( bench , task . name , rootCallingFile ) ;
117+
118+ // run the warmup of the task right before its actual run
119+ if ( bench . opts . warmup ) {
120+ task . warmup ( ) ;
121+ }
122+ // await mongoMeasurement.start(uri);
123+ InstrumentHooks . startBenchmark ( ) ;
124+ InstrumentHooks . __codspeed_root_frame__ ( ( ) => task . runSync ( ) ) ;
125+ InstrumentHooks . stopBenchmark ( ) ;
126+ // await mongoMeasurement.stop(uri);
127+ results . push ( task ) ;
128+
129+ if ( task . result ) {
130+ // Convert tinybench result to BenchmarkStats format
131+ const stats = convertTinybenchResultToBenchmarkStats (
132+ task . result ,
133+ bench . opts . warmup ? bench . opts . warmupIterations ?? 0 : 0
134+ ) ;
135+
136+ const benchmark : Benchmark = {
137+ name : task . name ,
138+ uri,
139+ config : {
140+ max_rounds : bench . opts . iterations ?? null ,
141+ max_time_ns : bench . opts . time ? msToNs ( bench . opts . time ) : null ,
142+ min_round_time_ns : null , // tinybench does not have an option for this
143+ warmup_time_ns :
144+ bench . opts . warmup && bench . opts . warmupTime
145+ ? msToNs ( bench . opts . warmupTime )
146+ : null ,
147+ } ,
148+ stats,
149+ } ;
150+
151+ benchmarks . push ( benchmark ) ;
71152 console . log ( ` ✔ Collected walltime data for ${ uri } ` ) ;
153+ InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
72154 } else {
73155 console . warn ( ` ⚠ No result data available for ${ uri } ` ) ;
74156 }
0 commit comments