@@ -3,7 +3,9 @@ const { setTimeout: delay } = require('node:timers/promises');
33const path = require ( 'node:path' ) ;
44const { spawn } = require ( 'node:child_process' ) ;
55const assert = require ( 'node:assert' ) ;
6+
67const autocannon = require ( 'autocannon' ) ;
8+ const Benchmark = require ( 'benchmark' ) ;
79
810const runner = {
911 autocannon : ( opts ) => {
@@ -15,6 +17,27 @@ const runner = {
1517 requests : opts . http . routes ,
1618 } )
1719 } ,
20+ benchmarkjs : ( opts ) => {
21+ const suite = new Benchmark . Suite ;
22+
23+ for ( const operation of opts . operations ) {
24+ suite . add ( operation . name , operation . fn ) ;
25+ }
26+
27+ return new Promise ( ( resolve ) => {
28+ const results = [ ] ;
29+ suite . on ( 'cycle' , function ( event ) {
30+ results . push ( {
31+ name : event . target . name ,
32+ opsSec : event . target . hz ,
33+ samples : event . target . cycles ,
34+ } ) ;
35+ } ) . on ( 'complete' , function ( ) {
36+ resolve ( results ) ;
37+ } )
38+ . run ( { 'async' : true } ) ;
39+ } )
40+ } ,
1841}
1942
2043const parser = {
@@ -28,6 +51,13 @@ const parser = {
2851 errors : result . errors ,
2952 }
3053 } ;
54+ } ,
55+ benchmarkjs : ( settings , result ) => {
56+ return {
57+ name : settings . name ,
58+ method : 'benchmarkjs' ,
59+ operations : result ,
60+ }
3161 }
3262}
3363
@@ -54,11 +84,11 @@ function spawnServer(settings) {
5484}
5585
5686async function runBenchmark ( settings ) {
57- assert . ok ( settings . http . server , 'HTTP Benchmark must have a server to be spawned' ) ;
5887 assert . ok ( ALLOWED_BENCHMARKER . includes ( settings . benchmarker ) , 'Invalid settings.benchmarker' ) ;
5988
6089 let server = undefined ;
6190 if ( settings . type === 'http' ) {
91+ assert . ok ( settings . http . server , 'HTTP Benchmark must have a server to be spawned' ) ;
6292 server = spawnServer ( settings ) ;
6393 // TODO: replace this workaround to use IPC to know when server is up
6494 await delay ( 1000 ) ;
@@ -78,7 +108,10 @@ async function main() {
78108 for ( const file of files ) {
79109 if ( file . match ( / .* - b e n c h m a r k \. j s $ / ) ) {
80110 const bench = require ( path . join ( __dirname , './src/' , file ) ) ;
81- console . log ( bench . name , 'results' , await runBenchmark ( bench ) ) ;
111+ // TODO(rafaelgss): possibly should be more accurate to run each benchmark in
112+ // a separate worker
113+ const result = await runBenchmark ( bench )
114+ console . log ( bench . name , 'results' , JSON . stringify ( result , null , 2 ) ) ;
82115 }
83116 }
84117}
0 commit comments