开发者

Handling 10,000 concurrent requests at once with node.js

开发者 https://www.devze.com 2023-03-13 03:38 出处:网络
I\'m working on a node.js application that has to provide real-time server push to around 10,000 users. My goal is to minimize the time difference between the first receiver and the last receiver. Rig

I'm working on a node.js application that has to provide real-time server push to around 10,000 users. My goal is to minimize the time difference between the first receiver and the last receiver. Right now, I am developing locally on my machine.

I use a loop to generate requests and then hold back server response until it hits 10,0000 requests. I want the server to broadcast to all requests at once and measure the difference.

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.leng开发者_开发技巧th;
  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');

This for some reason didn't work for me. Is there a better approach? Can anyone share his idea? What's the time difference for you? Thanks.


Depends on all kinds of things. You don't want to test both request/client and server side code like this on the same box for real world perf. You could also be running out of sockets easily. MacOS, for instance, has an artificial limit that once it gets hit, will bork everything.

Also, with regards to the client side, I think I'd use a tool like apache bench, which has concurrency and profiling built in. http://httpd.apache.org/docs/2.0/programs/ab.html


You can use ab benchmark as Josh suggested, or go npmjs.org, there is a load testing tool written in node. Or else, you can use JMeter.

0

精彩评论

暂无评论...
验证码 换一张
取 消