http.get()中的每秒请求数 – Node.js

我正在做node.js超级基本http请求应用程序。

 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 })); } 

我需要每秒获得http请求的数量。 任何想法如何去获得每秒的请求?

 // 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 ); 

使用Date对象logging时间,然后分析数字。

或者,如果您需要实时数据,则可以将setInterval与计数器variables结合使用。