Skip to content

Instantly share code, notes, and snippets.

@micalevisk
Created March 20, 2022 19:29
Show Gist options
  • Save micalevisk/8c0cabfa13c6c3c0039ad25c368e9d41 to your computer and use it in GitHub Desktop.
Save micalevisk/8c0cabfa13c6c3c0039ad25c368e9d41 to your computer and use it in GitHub Desktop.
Generates `autocannon` (https://github.com/mcollina/autocannon) command from user's answers
#!/usr/bin/env -S npx zx
$.shell = '/usr/bin/bash'
$.verbose = process.env.NODE_ENV === 'development'
// ========================================================================== //
function quitWitError(errorMsg) {
console.error( chalk.bold.red(errorMsg) )
process.exit(1)
}
function assertNotEmpty(val, errorMsg) {
if (!val.trim()) quitWitError(errorMsg)
}
function assertInteger(val, errorMsg) {
if (Number.isNaN(val)) quitWitError(errorMsg)
}
function isDefined(val) {
return !(typeof val === 'undefined')
}
function trim(ans) {
return (ans || '').trim()
}
function trimWithDefault(defaultValue) {
return (ans) => (ans || '').trim() || defaultValue
}
function strToInteger(ans) {
return Number.parseInt(ans.trim(), 10)
}
function strToBoolean(ans) {
const firstLow = ans.at(0).toLowerCase()
return firstLow === 'y' ? true : false
}
// ========================================================================== //
const method = await question(
chalk.bold.bgBlack(`method ${chalk.dim('(GET)')}: `)
).then(trimWithDefault('GET'))
assertNotEmpty(method, 'The HTTP method to use must be defined')
const url = await question(
chalk.bold.bgBlack('url: ')
).then(trim)
assertNotEmpty(url, 'The URL to fetch must be defined')
const connections = await question(
chalk.bold.bgBlack(`connections ${chalk.dim('(1)')}: `)
).then(trimWithDefault('1')).then(strToInteger)
assertInteger(connections, 'The number of concurrent connections to use must be defined')
const pipelining = await question(
chalk.bold.bgBlack(`pipelining: ${chalk.dim('(1)')}: `)
).then(trimWithDefault('1')).then(strToInteger)
assertInteger(pipelining, 'The number of pipelined requests to use must be defined')
// const duration = await question(
// chalk.bold.bgBlack(`duration: ${chalk.dim('(10)')}: `)
// ).then(trimwithdefault('10')).then(strToInteger)
// assertInteger(duration, 'The number of seconds to run Autocannon must be defined')
// const amount = await question(
// chalk.bold.bgBlack(`amount: ${chalk.dim('(if set, duration is ignored)')}: `)
// ).then(trim).then(strToInteger)
// isDefined(amount) && assertInteger(amount, 'The number of requests to make before exiting the benchmark must be defined')
const amount = await question(
chalk.bold.bgBlack('amount: ')
).then(trim).then(strToInteger)
assertInteger(amount, 'The number of requests to make before exiting the benchmark must be defined')
const withLatency = await question(
chalk.bold.bgBlack(`with 'latency'? ${chalk.dim('[yN]')}: `)
).then(trimWithDefault('N')).then(strToBoolean)
console.log([
'', '',
`autocannon ${url}`,
` --method ${method}`,
` --connections ${connections}`,
` --pipelining ${pipelining}`,
` --amount ${amount}`,
withLatency ? ' --latency' : null,
].map(s => s ? s+' \\' : s).filter(s => s !== null).join('\n').slice(0, -1)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment