1'use strict'; 2 3const path = require('path'); 4const fork = require('child_process').fork; 5const CLI = require('./_cli.js'); 6 7const cli = new CLI(`usage: ./node run.js [options] [--] <category> ... 8 Run each benchmark in the <category> directory a single time, more than one 9 <category> directory can be specified. 10 11 --filter pattern includes only benchmark scripts matching <pattern> 12 (can be repeated) 13 --exclude pattern excludes scripts matching <pattern> (can be 14 repeated) 15 --set variable=value set benchmark variable (can be repeated) 16 --format [simple|csv] optional value that specifies the output format 17 test only run a single configuration from the options 18 matrix 19 all each benchmark category is run one after the other 20`, { arrayArgs: ['set', 'filter', 'exclude'] }); 21const benchmarks = cli.benchmarks(); 22 23if (benchmarks.length === 0) { 24 console.error('No benchmarks found'); 25 process.exitCode = 1; 26 return; 27} 28 29const validFormats = ['csv', 'simple']; 30const format = cli.optional.format || 'simple'; 31if (!validFormats.includes(format)) { 32 console.error('Invalid format detected'); 33 process.exitCode = 1; 34 return; 35} 36 37if (format === 'csv') { 38 console.log('"filename", "configuration", "rate", "time"'); 39} 40 41(function recursive(i) { 42 const filename = benchmarks[i]; 43 const child = fork( 44 path.resolve(__dirname, filename), 45 cli.test ? ['--test'] : cli.optional.set, 46 ); 47 48 if (format !== 'csv') { 49 console.log(); 50 console.log(filename); 51 } 52 53 child.on('message', (data) => { 54 if (data.type !== 'report') { 55 return; 56 } 57 // Construct configuration string, " A=a, B=b, ..." 58 let conf = ''; 59 for (const key of Object.keys(data.conf)) { 60 if (conf !== '') 61 conf += ' '; 62 conf += `${key}=${JSON.stringify(data.conf[key])}`; 63 } 64 if (format === 'csv') { 65 // Escape quotes (") for correct csv formatting 66 conf = conf.replace(/"/g, '""'); 67 console.log(`"${data.name}", "${conf}", ${data.rate}, ${data.time}`); 68 } else { 69 let rate = data.rate.toString().split('.'); 70 rate[0] = rate[0].replace(/(\d)(?=(?:\d\d\d)+(?!\d))/g, '$1,'); 71 rate = (rate[1] ? rate.join('.') : rate[0]); 72 console.log(`${data.name} ${conf}: ${rate}`); 73 } 74 }); 75 76 child.once('close', (code) => { 77 if (code) { 78 process.exit(code); 79 } 80 81 // If there are more benchmarks execute the next 82 if (i + 1 < benchmarks.length) { 83 recursive(i + 1); 84 } 85 }); 86})(0); 87