From ffe1aea0db1616599f9e769fec73eeda6404edbb Mon Sep 17 00:00:00 2001 From: Matheus Marchini Date: Mon, 9 Dec 2019 10:00:28 -0800 Subject: [PATCH] bench: allow users to run benchmarks non-interactively This allows users to run benchmarks non-interactively by using command line flags. Useful when running benchmarks automatically on CI environments, for example. --- benchmark-bench.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/benchmark-bench.js b/benchmark-bench.js index cda0ec61..d0ea6826 100644 --- a/benchmark-bench.js +++ b/benchmark-bench.js @@ -4,6 +4,22 @@ const inquirer = require('inquirer') const bench = require('./lib/bench') const { choices, list } = require('./lib/packages') +const commander = require('commander') + +function collect (value, previous) { + if (!choices.includes(value)) { + throw new TypeError(`Benchmark ${value} doesn't exist.`) + } + return previous.concat([value]) +} + +commander + .option('-a, --all', 'all') + .option('-c, --connections ', 'connections', Number) + .option('-p, --pipelining ', 'pipelining', Number) + .option('-d, --duration ', 'duration', Number) + .option('-b, --benchmark ', 'benchmark', collect, []) + .parse(process.argv) function select (callback) { inquirer.prompt([ @@ -35,6 +51,7 @@ inquirer.prompt([ type: 'confirm', name: 'all', message: 'Do you want to run all benchmark tests?', + when: commander.all === undefined && commander.benchmark.length === 0, default: false }, { @@ -42,6 +59,7 @@ inquirer.prompt([ name: 'connections', message: 'How many connections do you need?', default: 100, + when: commander.connections === undefined, validate (value) { return !Number.isNaN(parseFloat(value)) || 'Please enter a number' }, @@ -52,6 +70,7 @@ inquirer.prompt([ name: 'pipelining', message: 'How many pipelines do you need?', default: 10, + when: commander.pipelining === undefined, validate (value) { return !Number.isNaN(parseFloat(value)) || 'Please enter a number' }, @@ -62,14 +81,21 @@ inquirer.prompt([ name: 'duration', message: 'How long should it take?', default: 40, + when: commander.duration === undefined, validate (value) { return !Number.isNaN(parseFloat(value)) || 'Please enter a number' }, filter: Number } -]).then((opts) => { +]).then((inquirerOpts) => { + const opts = { ...inquirerOpts, ...commander } + if (!opts.all) { - select(list => bench(opts, list)) + if (opts.benchmark.length === 0) { + select(list => bench(opts, list)) + } else { + bench(opts, opts.benchmark) + } } else { bench(opts, choices) }