Skip to content

Instantly share code, notes, and snippets.

@dminones
Created May 31, 2019 16:21
Show Gist options
  • Save dminones/110f72c63f678e7e7e959548977bd638 to your computer and use it in GitHub Desktop.
Save dminones/110f72c63f678e7e7e959548977bd638 to your computer and use it in GitHub Desktop.
Secuentially execute a request as many times as passed by param
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);
@dminones
Copy link
Author

dminones commented May 31, 2019

Example

node server-stress.js   "curl 'https://goodserver.gooddollar.org/auth/eth' -X OPTIONS -H 'Access-Control-Request-Method: POST' -H 'Origin: https://dapp.gooddollar.org' -H 'Referer: https://dapp.gooddollar.org/' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' -H 'Access-Control-Request-Headers: authorization,content-type' --compressed" 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment