@@ -3,6 +3,7 @@ import { init as initDotNet } from "./dotnet/init.mjs";
33import { init as initDotNetLLVM } from "./dotnet-llvm/init.mjs" ;
44import { init as initGo } from "./go/init.mjs" ;
55import { init as initRust } from "./rust/init.mjs" ;
6+ import * as fixtures from "./fixtures.mjs" ;
67
78/**
89 * @typedef {Object } Exports
@@ -12,7 +13,7 @@ import { init as initRust } from "./rust/init.mjs";
1213 */
1314
1415const lang = process . argv [ 2 ] ;
15- const baseline = [ ] ;
16+ const baseline = new Map ;
1617
1718if ( ! lang || lang . toLowerCase ( ) === "rust" )
1819 await run ( "Rust" , await initRust ( ) ) ;
@@ -29,23 +30,37 @@ if (!lang || lang.toLowerCase() === "go")
2930 * @param {Exports } exports */
3031async function run ( lang , exports ) {
3132 console . log ( `\n\nBenching ${ lang } ...\n` ) ;
32- console . log ( `Echo number: ${ iterate ( 0 , exports . echoNumber , 100 , 3 , 1000 ) } ` ) ;
33- console . log ( `Echo struct: ${ iterate ( 1 , exports . echoStruct , 100 , 3 , 100 ) } ` ) ;
34- console . log ( `Fibonacci: ${ iterate ( 2 , ( ) => exports . fi ( 33 ) , 100 , 3 , 1 ) } ` ) ;
33+ await new Promise ( r => setTimeout ( r , 100 ) ) ;
34+ bench ( "Echo number" , exports . echoNumber , 100 , 3 , 1000 , fixtures . getNumber ( ) ) ;
35+ bench ( "Echo struct" , exports . echoStruct , 100 , 3 , 100 , fixtures . getStruct ( ) ) ;
36+ bench ( "Fibonacci" , ( ) => exports . fi ( 33 ) , 100 , 3 , 1 ) ;
3537}
3638
37- function iterate ( idx , fn , iterations , warms , loops ) {
39+ function bench ( name , fn , iters , warms , loops , expected = undefined ) {
40+ if ( expected ) {
41+ expected = JSON . stringify ( expected ) ;
42+ const actual = JSON . stringify ( fn ( ) ) ;
43+ if ( actual !== expected ) {
44+ console . error ( `Wrong result of '${ name } '. Expected: ${ expected } Actual: ${ actual } ` ) ;
45+ return ;
46+ }
47+ }
48+
3849 const results = [ ] ;
3950 warms *= - 1 ;
40- for ( let i = warms ; i < iterations ; i ++ ) {
51+ for ( let i = warms ; i < iters ; i ++ ) {
4152 const start = performance . now ( ) ;
4253 for ( let l = 0 ; l < loops ; l ++ ) fn ( ) ;
4354 if ( i >= 0 ) results . push ( performance . now ( ) - start ) ;
4455 }
4556 let media = median ( results ) ;
46- if ( baseline [ idx ] ) return `${ ( media / baseline [ idx ] ) . toFixed ( 1 ) } ` ;
47- else baseline [ idx ] = media ;
48- return `${ media . toFixed ( 3 ) } ms` ;
57+
58+ if ( baseline . has ( name ) ) {
59+ console . log ( `${ name } : ${ ( media / baseline . get ( name ) ) . toFixed ( 1 ) } ` ) ;
60+ } else {
61+ baseline . set ( name , media ) ;
62+ console . log ( `${ name } : ${ media . toFixed ( 3 ) } ms` ) ;
63+ }
4964}
5065
5166function median ( numbers ) {
0 commit comments