Created
August 13, 2014 21:57
-
-
Save oleduc/64bf77d87a6c732d0a01 to your computer and use it in GitHub Desktop.
Node HTTP endpoint stress tester
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
var http = require('http'); | |
var async = require('async'); | |
var data = JSON.stringify({ | |
email: 'evil+spam2@evil.com', | |
firstName: 'Spammer', | |
lastName: 'Evil' | |
}); | |
var options = { | |
hostname: 'localhost', | |
port: 3000, | |
path: '/api/1.0/user/auth/register', | |
method: 'POST', | |
agent: false, | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': data.length | |
} | |
}; | |
var requestTicker = { | |
fast: 0, | |
ok: 0, | |
slow: 0, | |
reallySlow:0, | |
horriblySlow:0, | |
wtf: 0 | |
}; | |
var requestCount = 0; | |
setInterval(function(){ | |
async.parallel([ | |
request, | |
request, | |
request, | |
request, | |
request, | |
request, | |
request, | |
request, | |
request, | |
request | |
], function(){ | |
}); | |
async.parra | |
},100); | |
setInterval(function(){ | |
console.log('===================================================================================='); | |
console.log(requestTicker); | |
console.log('Total:',requestCount); | |
console.log('===================================================================================='); | |
},6000); | |
function request(done){ | |
var localStartTime = process.hrtime(); | |
var localRequestNumber = requestCount; | |
var req = http.request(options, function(res) { | |
var timer = parseInt(process.hrtime(localStartTime)[1]) / 1000000; | |
if(timer < 25){ | |
requestTicker.fast++; | |
} else if(timer > 25 && timer < 75){ | |
requestTicker.ok++; | |
} else if(timer > 75 && timer < 150) { | |
requestTicker.slow++; | |
} else if(timer > 150 && timer < 300) { | |
requestTicker.reallySlow++; | |
} else if(timer > 300) { | |
requestTicker.horriblySlow++; | |
} else { | |
requestTicker.wtf++; | |
} | |
done(); | |
}); | |
req.on('error', function(e) { | |
console.log('Problem with request('+localRequestNumber+'): ' + e.message); | |
done(); | |
}); | |
// write data to request body | |
req.shouldKeepAlive = false; | |
req.write(data); | |
req.end(); | |
requestCount++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment