@@ -10,31 +10,54 @@ const cli = CLI(`usage: ./node run.js [options] [--] <category> ...
1010
1111 --filter pattern string to filter benchmark scripts
1212 --set variable=value set benchmark variable (can be repeated)
13+ --format [simple|csv] optional value that specifies the output format
1314` , {
1415 arrayArgs : [ 'set' ]
1516} ) ;
1617const benchmarks = cli . benchmarks ( ) ;
1718
1819if ( benchmarks . length === 0 ) {
19- console . error ( 'no benchmarks found' ) ;
20- process . exit ( 1 ) ;
20+ console . error ( 'No benchmarks found' ) ;
21+ process . exitCode = 1 ;
22+ return ;
23+ }
24+
25+ const validFormats = [ 'csv' , 'simple' ] ;
26+ const format = cli . optional . format || 'simple' ;
27+ if ( ! validFormats . includes ( format ) ) {
28+ console . error ( 'Invalid format detected' ) ;
29+ process . exitCode = 1 ;
30+ return ;
31+ }
32+
33+ if ( format === 'csv' ) {
34+ console . log ( '"filename", "configuration", "rate", "time"' ) ;
2135}
2236
2337( function recursive ( i ) {
2438 const filename = benchmarks [ i ] ;
2539 const child = fork ( path . resolve ( __dirname , filename ) , cli . optional . set ) ;
2640
27- console . log ( ) ;
28- console . log ( filename ) ;
41+ if ( format !== 'csv' ) {
42+ console . log ( ) ;
43+ console . log ( filename ) ;
44+ }
2945
3046 child . on ( 'message' , function ( data ) {
3147 // Construct configuration string, " A=a, B=b, ..."
3248 let conf = '' ;
3349 for ( const key of Object . keys ( data . conf ) ) {
3450 conf += ' ' + key + '=' + JSON . stringify ( data . conf [ key ] ) ;
3551 }
36-
37- console . log ( `${ data . name } ${ conf } : ${ data . rate } ` ) ;
52+ // delete first space of the configuration
53+ conf = conf . slice ( 1 ) ;
54+ if ( format === 'csv' ) {
55+ // Escape quotes (") for correct csv formatting
56+ conf = conf . replace ( / " / g, '""' ) ;
57+ console . log ( `"${ data . name } ", "${ conf } ", ${ data . rate } , ${ data . time } ` ) ;
58+ } else {
59+ console . log ( `${ data . name } ${ conf } : ${ data . rate } ` ) ;
60+ }
3861 } ) ;
3962
4063 child . once ( 'close' , function ( code ) {
0 commit comments