@@ -2,18 +2,40 @@ import {
2
2
InstrumentHooks ,
3
3
mongoMeasurement ,
4
4
optimizeFunction ,
5
- teardownCore ,
6
5
} from "@codspeed/core" ;
7
6
import { Bench , Fn , FnOptions , Task } from "tinybench" ;
8
- import { getTaskUri } from "./uri " ;
7
+ import { BaseBenchRunner } from "./shared " ;
9
8
10
- declare const __VERSION__ : string ;
11
-
12
- export function runInstrumentedBench (
9
+ export function setupCodspeedInstrumentedBench (
13
10
bench : Bench ,
14
11
rootCallingFile : string
15
12
) : void {
16
- const runTaskAsync = async ( task : Task , uri : string ) : Promise < void > => {
13
+ const runner = new InstrumentedBenchRunner ( bench , rootCallingFile ) ;
14
+ runner . setupBenchMethods ( ) ;
15
+ }
16
+
17
+ class InstrumentedBenchRunner extends BaseBenchRunner {
18
+ protected getModeName ( ) : string {
19
+ return "instrumented mode" ;
20
+ }
21
+
22
+ private taskCompletionMessage ( ) {
23
+ return InstrumentHooks . isInstrumented ( ) ? "Measured" : "Checked" ;
24
+ }
25
+
26
+ private wrapFunctionWithFrame ( fn : Fn , isAsync : boolean ) : Fn {
27
+ if ( isAsync ) {
28
+ return async function __codspeed_root_frame__ ( ) {
29
+ await fn ( ) ;
30
+ } ;
31
+ } else {
32
+ return function __codspeed_root_frame__ ( ) {
33
+ fn ( ) ;
34
+ } ;
35
+ }
36
+ }
37
+
38
+ protected async runTaskAsync ( task : Task , uri : string ) : Promise < void > {
17
39
const { fnOpts, fn } = task as unknown as { fnOpts ?: FnOptions ; fn : Fn } ;
18
40
19
41
await fnOpts ?. beforeAll ?. call ( task , "run" ) ;
@@ -25,79 +47,38 @@ export function runInstrumentedBench(
25
47
await fnOpts ?. beforeEach ?. call ( task , "run" ) ;
26
48
await mongoMeasurement . start ( uri ) ;
27
49
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
- } ) ( ) ;
50
+ global . gc ?.( ) ;
51
+ await this . wrapWithInstrumentHooksAsync (
52
+ this . wrapFunctionWithFrame ( fn , true ) ,
53
+ uri
54
+ ) ;
35
55
36
56
await mongoMeasurement . stop ( uri ) ;
37
57
await fnOpts ?. afterEach ?. call ( task , "run" ) ;
38
58
await fnOpts ?. afterAll ?. call ( task , "run" ) ;
39
- } ;
40
59
41
- // Sync task runner
42
- const runTaskSync = ( task : Task , uri : string ) : void => {
60
+ this . logTaskCompletion ( uri , this . taskCompletionMessage ( ) ) ;
61
+ }
62
+
63
+ protected runTaskSync ( task : Task , uri : string ) : void {
43
64
const { fnOpts, fn } = task as unknown as { fnOpts ?: FnOptions ; fn : Fn } ;
44
65
45
66
fnOpts ?. beforeAll ?. call ( task , "run" ) ;
46
67
fnOpts ?. beforeEach ?. call ( task , "run" ) ;
47
68
48
- ( function __codspeed_root_frame__ ( ) {
49
- global . gc ?.( ) ;
50
- InstrumentHooks . startBenchmark ( ) ;
51
- fn ( ) ;
52
- InstrumentHooks . stopBenchmark ( ) ;
53
- InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
54
- } ) ( ) ;
69
+ this . wrapWithInstrumentHooks ( this . wrapFunctionWithFrame ( fn , false ) , uri ) ;
55
70
56
71
fnOpts ?. afterEach ?. call ( task , "run" ) ;
57
72
fnOpts ?. afterAll ?. call ( task , "run" ) ;
58
- } ;
59
-
60
- bench . run = async ( ) => {
61
- logStart ( ) ;
62
73
63
- for ( const task of bench . tasks ) {
64
- const uri = getTaskUri ( bench , task . name , rootCallingFile ) ;
65
- await runTaskAsync ( task , uri ) ;
66
- logTaskCompletion ( uri ) ;
67
- }
68
-
69
- return logEnd ( ) ;
70
- } ;
71
-
72
- bench . runSync = ( ) => {
73
- logStart ( ) ;
74
-
75
- for ( const task of bench . tasks ) {
76
- const uri = getTaskUri ( bench , task . name , rootCallingFile ) ;
77
- runTaskSync ( task , uri ) ;
78
- logTaskCompletion ( uri ) ;
79
- }
74
+ this . logTaskCompletion ( uri , this . taskCompletionMessage ( ) ) ;
75
+ }
80
76
81
- return logEnd ( ) ;
82
- } ;
83
-
84
- const logStart = ( ) => {
85
- console . log (
86
- `[CodSpeed] running with @codspeed/tinybench v${ __VERSION__ } (instrumented mode)`
87
- ) ;
88
- } ;
89
-
90
- const logTaskCompletion = ( uri : string ) => {
91
- console . log (
92
- ` ✔ ${
93
- InstrumentHooks . isInstrumented ( ) ? "Measured" : "Checked"
94
- } ${ uri } `
95
- ) ;
96
- } ;
77
+ protected finalizeAsyncRun ( ) : Task [ ] {
78
+ return this . finalizeBenchRun ( ) ;
79
+ }
97
80
98
- const logEnd = ( ) => {
99
- teardownCore ( ) ;
100
- console . log ( `[CodSpeed] Done running ${ bench . tasks . length } benches.` ) ;
101
- return bench . tasks ;
102
- } ;
81
+ protected finalizeSyncRun ( ) : Task [ ] {
82
+ return this . finalizeBenchRun ( ) ;
83
+ }
103
84
}
0 commit comments