可以模拟多个并发连接以测试Node.js应用 [英] Possible to simulate several concurrent connections to test a nodejs app
问题描述
我有一个运行@localhost的简单node.js/socket.io(websockets)应用程序.我正在尝试查看它可以处理多少个并发连接.是否可以在本地主机上模拟多个并发用户?
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 ?
这是我使用socket.io-client的半尝试:
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() ;
如我所见,这只会每100毫秒发出"一条新消息,并且不会模拟并发连接.
As I see it this only 'emits' a new message every 100 ms and is not simulating concurrent connections.
推荐答案
在调用connect
时,必须告诉socket.io为每个对connect
的调用创建一个新连接.例如:
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 });
此外,如果您想提高出站TCP连接限制(似乎默认每个目标5个连接),请执行类似的操作
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;
在连接之前.
但是请注意,快速创建和关闭tcp套接字将使TCP连接堆积在状态TIME_WAIT
中,具体取决于您的
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.
这篇关于可以模拟多个并发连接以测试Node.js应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!