File tree Expand file tree Collapse file tree 3 files changed +30
-6
lines changed Expand file tree Collapse file tree 3 files changed +30
-6
lines changed Original file line number Diff line number Diff line change 1
1
import Benchmark from "benchmark" ;
2
- import measurement from "@codspeed/core" ;
2
+ import { initCore , measurement , optimizeFunctionSync } from "@codspeed/core" ;
3
3
import { get as getStackTrace } from "stack-trace" ;
4
4
import path , { dirname } from "path" ;
5
5
import { findUpSync , Options } from "find-up" ;
@@ -18,12 +18,15 @@ function withCodSpeedBenchmark(bench: Benchmark): Benchmark {
18
18
if ( ! measurement . isInstrumented ( ) ) {
19
19
return bench ;
20
20
}
21
+ initCore ( ) ;
21
22
const callingFile = getCallingFile ( ) ;
22
23
// eslint-disable-next-line @typescript-eslint/no-unused-vars
23
24
bench . run = function ( options ?: Benchmark . Options ) : Benchmark {
24
25
const uri = callingFile + "::" + ( bench . name ?? "unknown" ) ;
26
+ const fn = bench . fn as CallableFunction ;
27
+ optimizeFunctionSync ( fn ) ;
25
28
measurement . startInstrumentation ( ) ;
26
- ( bench . fn as CallableFunction ) ( ) ;
29
+ fn ( ) ;
27
30
measurement . stopInstrumentation ( uri ) ;
28
31
return bench ;
29
32
} ;
Original file line number Diff line number Diff line change 26
26
isBound : false ,
27
27
} ;
28
28
}
29
- const measurement = m ;
30
- export default measurement ;
29
+ export const measurement = m ;
30
+
31
+ export const initCore = ( ) => {
32
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
33
+ require ( "v8" ) . setFlagsFromString ( "--allow-natives-syntax" ) ;
34
+ } ;
35
+
36
+ export const optimizeFunction = async ( fn : CallableFunction ) => {
37
+ // Source: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#optimization-killers
38
+ await fn ( ) ; //Fill type-info
39
+ await fn ( ) ; // 2 calls are needed to go from uninitialized -> pre-monomorphic -> monomorphic
40
+ eval ( "%OptimizeFunctionOnNextCall(fn)" ) ;
41
+ await fn ( ) ; // optimize
42
+ } ;
43
+
44
+ export const optimizeFunctionSync = ( fn : CallableFunction ) => {
45
+ // Source: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#optimization-killers
46
+ fn ( ) ; //Fill type-info
47
+ fn ( ) ; // 2 calls are needed to go from uninitialized -> pre-monomorphic -> monomorphic
48
+ eval ( "%OptimizeFunctionOnNextCall(fn)" ) ;
49
+ fn ( ) ; // optimize
50
+ } ;
Original file line number Diff line number Diff line change 1
- import measurement from "@codspeed/core" ;
1
+ import { initCore , measurement , optimizeFunction } from "@codspeed/core" ;
2
2
import { get as getStackTrace } from "stack-trace" ;
3
3
import path , { dirname } from "path" ;
4
4
import { findUpSync , Options } from "find-up" ;
@@ -8,11 +8,12 @@ export function withCodSpeed(bench: Bench): Bench {
8
8
if ( ! measurement . isInstrumented ( ) ) {
9
9
return bench ;
10
10
}
11
+ initCore ( ) ;
11
12
const callingFile = getCallingFile ( ) ;
12
13
bench . run = async ( ) => {
13
14
for ( const task of bench . tasks ) {
14
15
const uri = callingFile + "::" + task . name ;
15
- await task . warmup ( ) ;
16
+ await optimizeFunction ( task . fn ) ;
16
17
measurement . startInstrumentation ( ) ;
17
18
await task . fn ( ) ;
18
19
measurement . stopInstrumentation ( uri ) ;
You can’t perform that action at this time.
0 commit comments