44 optimizeFunction ,
55 teardownCore ,
66} from "@codspeed/core" ;
7- import { Bench , Fn , FnOptions } from "tinybench" ;
7+ import { Bench , Fn , FnOptions , Task } from "tinybench" ;
88import { getTaskUri } from "./uri" ;
99
1010declare const __VERSION__ : string ;
@@ -13,103 +13,89 @@ export function runInstrumentedBench(
1313 bench : Bench ,
1414 rootCallingFile : string
1515) : void {
16- bench . run = async ( ) => {
17- console . log (
18- `[CodSpeed] running with @codspeed/tinybench v${ __VERSION__ } (instrumented mode)`
19- ) ;
20-
21- for ( const task of bench . tasks ) {
22- const uri = getTaskUri ( bench , task . name , rootCallingFile ) ;
16+ const runTaskAsync = async ( task : Task , uri : string ) : Promise < void > => {
17+ const { fnOpts, fn } = task as unknown as { fnOpts ?: FnOptions ; fn : Fn } ;
2318
24- // Access private fields
25- const { fnOpts, fn } = task as unknown as { fnOpts ?: FnOptions ; fn : Fn } ;
19+ await fnOpts ?. beforeAll ?. call ( task , "run" ) ;
20+ await optimizeFunction ( async ( ) => {
21+ await fnOpts ?. beforeEach ?. call ( task , "run" ) ;
22+ await fn ( ) ;
23+ await fnOpts ?. afterEach ?. call ( task , "run" ) ;
24+ } ) ;
25+ await fnOpts ?. beforeEach ?. call ( task , "run" ) ;
26+ await mongoMeasurement . start ( uri ) ;
2627
27- // Call beforeAll hook if it exists
28- await fnOpts ?. beforeAll ?. call ( task , "run" ) ;
28+ await ( async function __codspeed_root_frame__ ( ) {
29+ global . gc ?.( ) ;
30+ InstrumentHooks . startBenchmark ( ) ;
31+ await fn ( ) ;
32+ InstrumentHooks . stopBenchmark ( ) ;
33+ InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
34+ } ) ( ) ;
35+
36+ await mongoMeasurement . stop ( uri ) ;
37+ await fnOpts ?. afterEach ?. call ( task , "run" ) ;
38+ await fnOpts ?. afterAll ?. call ( task , "run" ) ;
39+ } ;
2940
30- // run optimizations
31- await optimizeFunction ( async ( ) => {
32- await fnOpts ?. beforeEach ?. call ( task , "run" ) ;
33- await fn ( ) ;
34- await fnOpts ?. afterEach ?. call ( task , "run" ) ;
35- } ) ;
41+ // Sync task runner
42+ const runTaskSync = ( task : Task , uri : string ) : void => {
43+ const { fnOpts, fn } = task as unknown as { fnOpts ?: FnOptions ; fn : Fn } ;
3644
37- // run instrumented benchmark
38- await fnOpts ?. beforeEach ?. call ( task , "run" ) ;
45+ fnOpts ?. beforeAll ?. call ( task , " run" ) ;
46+ fnOpts ?. beforeEach ?. call ( task , "run" ) ;
3947
40- await mongoMeasurement . start ( uri ) ;
48+ ( function __codspeed_root_frame__ ( ) {
4149 global . gc ?.( ) ;
42- await ( async function __codspeed_root_frame__ ( ) {
43- InstrumentHooks . startBenchmark ( ) ;
44- await fn ( ) ;
45- InstrumentHooks . stopBenchmark ( ) ;
46- InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
47- } ) ( ) ;
48- await mongoMeasurement . stop ( uri ) ;
49-
50- await fnOpts ?. afterEach ?. call ( task , "run" ) ;
50+ InstrumentHooks . startBenchmark ( ) ;
51+ fn ( ) ;
52+ InstrumentHooks . stopBenchmark ( ) ;
53+ InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
54+ } ) ( ) ;
55+
56+ fnOpts ?. afterEach ?. call ( task , "run" ) ;
57+ fnOpts ?. afterAll ?. call ( task , "run" ) ;
58+ } ;
5159
52- await fnOpts ?. afterAll ?. call ( task , "run" ) ;
60+ bench . run = async ( ) => {
61+ logStart ( ) ;
5362
54- // print results
55- console . log (
56- ` ✔ ${
57- InstrumentHooks . isInstrumented ( ) ? "Measured" : "Checked"
58- } ${ uri } `
59- ) ;
63+ for ( const task of bench . tasks ) {
64+ const uri = getTaskUri ( bench , task . name , rootCallingFile ) ;
65+ await runTaskAsync ( task , uri ) ;
66+ logTaskCompletion ( uri ) ;
6067 }
6168
62- teardownCore ( ) ;
63- console . log ( `[CodSpeed] Done running ${ bench . tasks . length } benches.` ) ;
64- return bench . tasks ;
69+ return logEnd ( ) ;
6570 } ;
6671
6772 bench . runSync = ( ) => {
68- console . log (
69- `[CodSpeed] running with @codspeed/tinybench v${ __VERSION__ } (instrumented mode)`
70- ) ;
73+ logStart ( ) ;
7174
7275 for ( const task of bench . tasks ) {
7376 const uri = getTaskUri ( bench , task . name , rootCallingFile ) ;
77+ runTaskSync ( task , uri ) ;
78+ logTaskCompletion ( uri ) ;
79+ }
7480
75- // Access private fields
76- const { fnOpts, fn } = task as unknown as { fnOpts ?: FnOptions ; fn : Fn } ;
77-
78- // Call beforeAll hook if it exists
79- fnOpts ?. beforeAll ?. call ( task , "run" ) ;
80-
81- // run optimizations
82- optimizeFunction ( async ( ) => {
83- fnOpts ?. beforeEach ?. call ( task , "run" ) ;
84- fn ( ) ;
85- fnOpts ?. afterEach ?. call ( task , "run" ) ;
86- } ) ;
81+ return logEnd ( ) ;
82+ } ;
8783
88- // run instrumented benchmark
89- fnOpts ?. beforeEach ?. call ( task , "run" ) ;
84+ const logStart = ( ) => {
85+ console . log (
86+ `[CodSpeed] running with @codspeed/tinybench v${ __VERSION__ } (instrumented mode)`
87+ ) ;
88+ } ;
9089
91- // await mongoMeasurement.start(uri);
92- global . gc ?.( ) ;
93- ( function __codspeed_root_frame__ ( ) {
94- InstrumentHooks . startBenchmark ( ) ;
95- fn ( ) ;
96- InstrumentHooks . stopBenchmark ( ) ;
97- InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
98- } ) ( ) ;
99- mongoMeasurement . stop ( uri ) ;
100-
101- fnOpts ?. afterEach ?. call ( task , "run" ) ;
102-
103- fnOpts ?. afterAll ?. call ( task , "run" ) ;
104-
105- // print results
106- console . log (
107- ` ✔ ${
108- InstrumentHooks . isInstrumented ( ) ? "Measured" : "Checked"
109- } ${ uri } `
110- ) ;
111- }
90+ const logTaskCompletion = ( uri : string ) => {
91+ console . log (
92+ ` ✔ ${
93+ InstrumentHooks . isInstrumented ( ) ? "Measured" : "Checked"
94+ } ${ uri } `
95+ ) ;
96+ } ;
11297
98+ const logEnd = ( ) => {
11399 teardownCore ( ) ;
114100 console . log ( `[CodSpeed] Done running ${ bench . tasks . length } benches.` ) ;
115101 return bench . tasks ;
0 commit comments