【问题标题】:Handling 10,000 concurrent requests at once with node.js使用 node.js 一次处理 10,000 个并发请求
【发布时间】:2011-06-13 15:46:57
【问题描述】:

我正在开发一个 node.js 应用程序,它必须为大约 10,000 个用户提供实时服务器推送。我的目标是最小化第一个接收器和最后一个接收器之间的时间差。现在,我正在我的机器上进行本地开发。

我使用循环来生成请求,然后阻止服务器响应,直到达到 10,000 个请求。我希望服务器一次向所有请求广播并测量差异。

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');

由于某种原因,这对我不起作用。有更好的方法吗?谁能分享他的想法?你的时差是多少?谢谢。

【问题讨论】:

  • 你说它对你不起作用是什么意思?你期望什么输出,你收到了什么?

标签: node.js real-time


【解决方案1】:

取决于各种各样的事情。您不想在同一个盒子上同时测试请求/客户端和服务器端代码以获得真实世界的性能。您也可能很容易用完套接字。例如,MacOS 有一个人为的限制,一旦被击中,一切都会失败。

另外,关于客户端,我想我会使用像 apache bench 这样的工具,它内置了并发和分析功能。http://httpd.apache.org/docs/2.0/programs/ab.html

【讨论】:

  • 如何增加对 Mac 套接字的限制?
【解决方案2】:

你可以按照 Josh 的建议使用 ab benchmark,或者去 npmjs.org,有一个用 node.js 编写的负载测试工具。否则,您可以使用 JMeter。

【讨论】:

    猜你喜欢
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多