通过node.js一次处理10,000个并发请求
|
我正在开发一个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\');
由于某种原因,这对我不起作用。有没有更好的方法?谁能分享他的想法?您的时差是多少?谢谢。
没有找到相关结果
已邀请:
2 个回复
驮帽俺篮号
穗揣