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/(结束-开始);