Skip to content

Instantly share code, notes, and snippets.

@daryl-sf
Last active June 18, 2019 09:12
Show Gist options
  • Save daryl-sf/4067c89f0cb970a40dcf39f37b63c7c9 to your computer and use it in GitHub Desktop.
Save daryl-sf/4067c89f0cb970a40dcf39f37b63c7c9 to your computer and use it in GitHub Desktop.
JS function that will curl a server asynchronously for each element in an array ([1,2,3] = 3) and log out the request/response times
time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
----------\n
time_total: %{time_total}\n
/*
example call:
var totallyNotADdosScript = require('./network-stress-test').processLoop;
totallyNotADdosScript(10, 'curl -w "@curl-format.txt" -o /dev/null -s "localhost:4000"');
This will send 10 requests and log request/response times for each when completed.
*/
var exec = require('child_process').exec;
function curlRequest (i, url) {
exec(url, function (_err, stdout, _stderr) {
console.log(`----------- start:${i} -------------`);
console.log(stdout);
console.log(`----------- done:${i} --------------`);
});
}
function processLoop(iterations, url) {
const items = Array.from(Array(iterations), (_x,i) => i);
setTimeout(function() {
curlRequest(items.shift(), url);
if (items.length > 0) {
setTimeout(arguments.callee, 25);
}
}, 25);
}
module.exports = { processLoop };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment