Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript http.get()-Node.js中每秒的请求数_Javascript_Node.js - Fatal编程技术网

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

Javascript http.get()-Node.js中每秒的请求数,javascript,node.js,Javascript,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

我正在
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 }));
}
var http=require('http');
变量选项={
主机:“www.domain-here.com”,
港口:80,
路径:'/index.html'
};
对于(变量i=0;i<500;i++){
get(选项、函数(res){
console.log(“[”+this.count+”]响应:“+res.statusCode”);
}.bind({count:i})).on('error',函数(e){
console.log(“[”+this.count+”]错误:“+e.message”);
}.bind({count:i}));
}

不过,我需要获得每秒http请求的数量。你知道我如何获得每秒的请求吗?

使用
Date
对象记录时间,然后分析数字

如果需要实时数据,也可以将
setInterval
与计数器变量结合使用。

//开始时间戳
// 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 );
var begin=新日期(); 对于(变量i=0;i<500;i++){ get(选项、函数(res){ console.log(“[”+this.count+”]响应:“+res.statusCode”); }.bind({count:i})).on('error',函数(e){ console.log(“[”+this.count+”]错误:“+e.message”); }.bind({count:i})); } //结束时间戳 var end=新日期(); //然后,你需要一个简单的计算 var reqPerSec=i/(结束-开始);