Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to simulate several concurrent connections to test a nodejs app

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.

like image 348
gyaani_guy Avatar asked Mar 24 '23 19:03

gyaani_guy


1 Answers

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.

like image 158
Evgeniy Berezovsky Avatar answered Mar 30 '23 00:03

Evgeniy Berezovsky