I am doing a super basic http request app in node.js
.
var http = require('http');
var options = {
host: 'www.domain-here.com',
port: 80,
path: '/index.html'
};
for(var i = 0; i < 500; i++) {
http.get(options, function(res) {
console.log("[" + this.count + "] Response: " + res.statusCode);
}.bind({ count: i })).on('error', function(e) {
console.log("[" + this.count + "] Error: " + e.message);
}.bind({ count: i }));
}
I need to get the number of http requests per second though. Any idea how I go about getting requests per second?
// begin timestamp
var begin = new Date();
for(var i = 0; i < 500; i++) {
http.get(options, function(res) {
console.log("[" + this.count + "] Response: " + res.statusCode);
}.bind({ count: i })).on('error', function(e) {
console.log("[" + this.count + "] Error: " + e.message);
}.bind({ count: i }));
}
// end timestamp
var end = new Date();
// Then, you need a simple calculation
var reqPerSec = i / ( end - begin );
Use the Date
object to log the time and analyze the numbers afterwards.
Or You could use a setInterval
in combination with a counter variable if you need real time data.