2012-09-24 2 views
1

node.js에 슈퍼 기본 http 요청 앱을 만들고 있습니다.초당 요청 http.get() - 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 })); 
} 

초당 HTTP 요청 수를 알아야합니다. 초당 요청 수를 늘리는 방법에 대해 알고 싶습니다.

답변

0

Date 개체를 사용하여 시간을 기록하고 나중에 숫자를 분석하십시오.

또는 실시간 데이터가 필요한 경우 setInterval을 카운터 변수와 함께 사용할 수 있습니다.

3
// 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); 
+0

감사합니다. 그것은 비록 초당 평균 요청입니다. – Justin

관련 문제