@@ -2,18 +2,20 @@ import {
22 InstrumentHooks ,
33 mongoMeasurement ,
44 optimizeFunction ,
5- teardownCore ,
65} from "@codspeed/core" ;
76import { Bench , Fn , FnOptions , Task } from "tinybench" ;
8- import { getTaskUri } from "./uri " ;
7+ import { BaseBenchRunner } from "./shared " ;
98
10- declare const __VERSION__ : string ;
9+ class InstrumentedBenchRunner extends BaseBenchRunner {
10+ protected getModeName ( ) : string {
11+ return "instrumented mode" ;
12+ }
1113
12- export function runInstrumentedBench (
13- bench : Bench ,
14- rootCallingFile : string
15- ) : void {
16- const runTaskAsync = async ( task : Task , uri : string ) : Promise < void > = > {
14+ private taskCompletionMessage ( ) {
15+ return InstrumentHooks . isInstrumented ( ) ? "Measured" : "Checked" ;
16+ }
17+
18+ protected async runTaskAsync ( task : Task , uri : string ) : Promise < void > {
1719 const { fnOpts, fn } = task as unknown as { fnOpts ?: FnOptions ; fn : Fn } ;
1820
1921 await fnOpts ?. beforeAll ?. call ( task , "run" ) ;
@@ -25,79 +27,48 @@ export function runInstrumentedBench(
2527 await fnOpts ?. beforeEach ?. call ( task , "run" ) ;
2628 await mongoMeasurement . start ( uri ) ;
2729
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- } ) ( ) ;
30+ await this . wrapWithHooksAsync (
31+ this . wrapFunctionWithFrame ( fn , true ) as ( ) => Promise < void > ,
32+ uri
33+ ) ;
3534
3635 await mongoMeasurement . stop ( uri ) ;
3736 await fnOpts ?. afterEach ?. call ( task , "run" ) ;
3837 await fnOpts ?. afterAll ?. call ( task , "run" ) ;
39- } ;
4038
41- // Sync task runner
42- const runTaskSync = ( task : Task , uri : string ) : void => {
39+ this . logTaskCompletion ( uri , this . taskCompletionMessage ( ) ) ;
40+ }
41+
42+ protected runTaskSync ( task : Task , uri : string ) : void {
4343 const { fnOpts, fn } = task as unknown as { fnOpts ?: FnOptions ; fn : Fn } ;
4444
4545 fnOpts ?. beforeAll ?. call ( task , "run" ) ;
4646 fnOpts ?. beforeEach ?. call ( task , "run" ) ;
4747
48- ( function __codspeed_root_frame__ ( ) {
49- global . gc ?.( ) ;
50- InstrumentHooks . startBenchmark ( ) ;
51- fn ( ) ;
52- InstrumentHooks . stopBenchmark ( ) ;
53- InstrumentHooks . setExecutedBenchmark ( process . pid , uri ) ;
54- } ) ( ) ;
48+ this . wrapWithHooks (
49+ this . wrapFunctionWithFrame ( fn , false ) as ( ) => void ,
50+ uri
51+ ) ;
5552
5653 fnOpts ?. afterEach ?. call ( task , "run" ) ;
5754 fnOpts ?. afterAll ?. call ( task , "run" ) ;
58- } ;
59-
60- bench . run = async ( ) => {
61- logStart ( ) ;
62-
63- for ( const task of bench . tasks ) {
64- const uri = getTaskUri ( bench , task . name , rootCallingFile ) ;
65- await runTaskAsync ( task , uri ) ;
66- logTaskCompletion ( uri ) ;
67- }
6855
69- return logEnd ( ) ;
70- } ;
56+ this . logTaskCompletion ( uri , this . taskCompletionMessage ( ) ) ;
57+ }
7158
72- bench . runSync = ( ) => {
73- logStart ( ) ;
59+ protected finalizeAsyncRun ( ) : Task [ ] {
60+ return this . finalizeBenchRun ( ) ;
61+ }
7462
75- for ( const task of bench . tasks ) {
76- const uri = getTaskUri ( bench , task . name , rootCallingFile ) ;
77- runTaskSync ( task , uri ) ;
78- logTaskCompletion ( uri ) ;
79- }
80-
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- } ;
63+ protected finalizeSyncRun ( ) : Task [ ] {
64+ return this . finalizeBenchRun ( ) ;
65+ }
66+ }
9767
98- const logEnd = ( ) => {
99- teardownCore ( ) ;
100- console . log ( `[CodSpeed] Done running ${ bench . tasks . length } benches.` ) ;
101- return bench . tasks ;
102- } ;
68+ export function runInstrumentedBench (
69+ bench : Bench ,
70+ rootCallingFile : string
71+ ) : void {
72+ const runner = new InstrumentedBenchRunner ( bench , rootCallingFile ) ;
73+ runner . setupBenchMethods ( ) ;
10374}
0 commit comments