我正在 node.js
中做一个 super 基本的 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 );
关于javascript - http.get() 中的每秒请求数 - Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12560645/