4
4
optimizeFunction ,
5
5
teardownCore ,
6
6
} from "@codspeed/core" ;
7
- import { Bench , Fn , FnOptions } from "tinybench" ;
7
+ import { Bench , Fn , FnOptions , Task } from "tinybench" ;
8
8
import { getTaskUri } from "./uri" ;
9
9
10
10
declare const __VERSION__ : string ;
@@ -13,103 +13,89 @@ export function runInstrumentedBench(
13
13
bench : Bench ,
14
14
rootCallingFile : string
15
15
) : 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 } ;
23
18
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 ) ;
26
27
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
+ } ;
29
40
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 } ;
36
44
37
- // run instrumented benchmark
38
- await fnOpts ?. beforeEach ?. call ( task , "run" ) ;
45
+ fnOpts ?. beforeAll ?. call ( task , " run" ) ;
46
+ fnOpts ?. beforeEach ?. call ( task , "run" ) ;
39
47
40
- await mongoMeasurement . start ( uri ) ;
48
+ ( function __codspeed_root_frame__ ( ) {
41
49
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
+ } ;
51
59
52
- await fnOpts ?. afterAll ?. call ( task , "run" ) ;
60
+ bench . run = async ( ) => {
61
+ logStart ( ) ;
53
62
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 ) ;
60
67
}
61
68
62
- teardownCore ( ) ;
63
- console . log ( `[CodSpeed] Done running ${ bench . tasks . length } benches.` ) ;
64
- return bench . tasks ;
69
+ return logEnd ( ) ;
65
70
} ;
66
71
67
72
bench . runSync = ( ) => {
68
- console . log (
69
- `[CodSpeed] running with @codspeed/tinybench v${ __VERSION__ } (instrumented mode)`
70
- ) ;
73
+ logStart ( ) ;
71
74
72
75
for ( const task of bench . tasks ) {
73
76
const uri = getTaskUri ( bench , task . name , rootCallingFile ) ;
77
+ runTaskSync ( task , uri ) ;
78
+ logTaskCompletion ( uri ) ;
79
+ }
74
80
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
+ } ;
87
83
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
+ } ;
90
89
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
+ } ;
112
97
98
+ const logEnd = ( ) => {
113
99
teardownCore ( ) ;
114
100
console . log ( `[CodSpeed] Done running ${ bench . tasks . length } benches.` ) ;
115
101
return bench . tasks ;
0 commit comments