Created
May 31, 2019 16:21
-
-
Save dminones/110f72c63f678e7e7e959548977bd638 to your computer and use it in GitHub Desktop.
Secuentially execute a request as many times as passed by param
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { exec } = require("child_process"); | |
function promiseFromChildProcess(child) { | |
return new Promise(function(resolve, reject) { | |
child.addListener("error", e => { | |
console.log("Error generating docs:", e); | |
reject(e); | |
}); | |
child.addListener("exit", e => { | |
if (e !== 0) { | |
console.log("Failed process exec:", e); | |
reject(e); | |
} | |
resolve(e); | |
}); | |
}); | |
} | |
function execPromise(toExec) { | |
return promiseFromChildProcess(exec(toExec)).catch(e => | |
console.log("Failed exec", toExec) | |
); | |
} | |
const executeNTimes = async (curlRequest, times) => { | |
console.time(`Creating ${times} requests`); | |
for (let i = 0; i < times; i++) { | |
const result = await execPromise(curlRequest); | |
console.log({ result, i }); | |
} | |
console.timeEnd(`Creating ${times} requests`); | |
}; | |
if (process.argv.length < 4) { | |
console.log("Missing args"); | |
console.log( | |
"Example: node server-stress.js \"curl 'https://goodserver.gooddollar.org/auth/eth'\" 5" | |
); | |
process.exit(); | |
} | |
const [, , request, times] = process.argv; | |
executeNTimes(request, times); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example