약 10,000 명의 사용자가 실시간으로 서버를 제공해야하는 node.js 응용 프로그램에서 작업하고 있습니다. 내 목표는 첫 번째 수신자와 마지막 수신자 사이의 시간차를 최소화하는 것입니다. 지금 당장은 내 컴퓨터에서 로컬로 개발 중입니다.node.js를 사용하여 동시에 10,000 개의 동시 요청 처리
루프를 사용하여 요청을 생성 한 다음 10,0000 개의 요청에 도달 할 때까지 서버 응답을 보류합니다. 한 번에 모든 요청에 서버를 브로드 캐스트하고 차이를 측정하고 싶습니다.
request.js
var http = require('http')
, a = http.getAgent('127.0.0.1', 9202);
var util = require('util');
var connections = [];
var NUM_CONCURR = 1000;
// Max and Min
Array.prototype.max = function(){
var max = this[0];
var len = this.length;
for(var i=0; i<len;i++)
if(this[i]>max)
max = this[i];
return max;
};
Array.prototype.min = function(){
var min = this[0];
var len = this.length;
for(var i=0; i<len; i++)
if(this[i]<min)
min = this[i];
return min;
};
// Number of socket tested
a.maxSockets = Infinity;
for(var i =0; i<NUM_CONCURR; i++){
http.get({
agent: a,
path: '/',
port: 9202,
host: '127.0.0.1'
},function(res){
connections.push(microtime(true));
});
util.log("Connected Clients: "+i);
}
util.log("Server running at port 9202");
setInterval(function(){
util.log("Total Diff Time = "+(connections.max()-connections.min()));
connections =[];
}, 1000*10);
// Time function
function microtime(get_as_float) {
var now = new Date().getTime()/1000;
var s = parseInt(now);
return (get_as_float) ? now : (Math.round((now - s) * 1000)/1000) + ' ' + s;
}
server.js
var http = require('http'),
HOST = 'localhost',
PORT = '9202';
var connections = [], i;
var server = http.createServer(function(req, res){
connections[connections.length] = {req:req, res:res};
console.log('established connections: '+ ++i);
});
// Send msg to stored connections
function message(){
var i = connections.length,
connection;
while(i--){
connection = connections[i];
connection.res.writeHead(200);
connection.res.write('weeeee');
}
};
//Broadcast after 40 sec
setTimeout(function(){
message();
}, 1000*40);
server.listen(PORT);
console.log('listening on 9202');
이 나를 위해 작동하지 않았다 몇 가지 이유. 더 나은 접근 방법이 있습니까? 누구든지 자신의 아이디어를 공유 할 수 있습니까? 당신과 어떤 시간차가 있니? 감사.
그것이 당신에게 효과가 없다고 말할 때 무엇을 의미합니까? 어떤 결과를 기대합니까? 그리고 무엇을 받았습니까? – BCG