Possible to simulate several concurrent connections to test a nodejs app

感情迁移 提交于 2019-12-05 02:05:02

问题


I have a simple node.js /socket.io (websockets) application running @localhost. I am trying to see how many concurrent connections it can handle. Is it possible to simulate several concurrent users on localhost itself ?

This is my half baked attempt using socket.io-client:

function connectAndSend(){
    socket.emit('qand',{
        code :'ubuntu'
    });
} 
socket.on('connect', function () {
});
socket.on('q', function (data) {
    console.log(data);
});

function callConnect(){
    console.log('calling');
    connectAndSend() ;
    setTimeout(callConnect,100) ;
}

callConnect() ;

As I see it this only 'emits' a new message every 100 ms and is not simulating concurrent connections.


回答1:


In your call to connect, you must tell socket.io to create a new connection for each call to connect. For example:

var socket = io.connect(server, { "force new connection": true });

Also, if you want to raise the outbound TCP connection limit (which seems to default to 5 connections per target), do something like

require('http').globalAgent.maxSockets = 1000;

before connecting.

But note that creating and closing tcp sockets at a fast rate will make TCP connections pile up in state TIME_WAIT and depending on your OS and your network settings you'll hit a limit pretty soon, meaning you'll have to wait for those old sockets to timeout before you can establish new connections. If I recall correctly, the limit was around 16k connections (per target ip/port combo) on Windows (both Server 2008 R2 and Windows 7), and the default TIME_WAIT timeout in Windows is 4 minutes, so if you create more than 16k connections in 4 minutes on Windows, you'll probably hit that wall.




回答2:


Check here:

Long connections with Node.js, how to reduce memory usage and prevent memory leak? Also related with V8 and webkit-devtools

and specifically - test procedure used by the author of question mentioned above

EDIT:

You can use following tools to check how many requests per second your server is capable of serving

  1. ab - http://httpd.apache.org/docs/2.2/programs/ab.html

  2. siege - http://www.joedog.org/siege-home/



来源:https://stackoverflow.com/questions/16810148/possible-to-simulate-several-concurrent-connections-to-test-a-nodejs-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!