欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

socket.io的1000+连接

程序员文章站 2022-07-14 20:57:09
...

最近基于socket.io写了个消息服务,使用websocket-bench 在进行压力测试

 

websocket-bench -a 5000 -c 500 http://ip:9080

 发现只要连接超过1000多点,就会失败:

 

 

Launch bench with 5000 total connection, 500 concurent connection
0 message(s) send by client
1 worker(s)
WS server : socket.io

#### steps report ####
┌────┬────── ┬───  ┬───────┐
│ Number │ Connections │ Errors │ Duration(ms) │
├────┼────── ┼────┼───────┤
│ 500    │ 500         │ 0      │ 1787         │
├────┼── ────┼────┼───────┤
│ 1000   │ 500         │ 0      │ 2609         │
├────┼── ────┼────┼───────┤
│ 1500   │ 0           │ 500    │ 2610         │
├────┼───── ─┼────┼───────┤
│ 2000   │ 13          │ 487    │ 1609         │
├────┼ ──────┼────┼───────┤
│ 2500   │ 0           │ 500    │ 1114         │
├────┼──── ──┼────┼───────┤
│ 3000   │ 0           │ 500    │ 1135         │
├────┼─ ─────┼────┼───────┤
│ 3500   │ 0           │ 500    │ 634          │
├────┼──── ──┼────┼───────┤
│ 4000   │ 0           │ 500    │ 597          │
├────┼── ────┼────┼───────┤
│ 4500   │ 0           │ 500    │ 1119         │
├────┼─── ───┼────┼───────┤
│ 5000   │ 0           │ 500    │ 618          │
└────┴──── ──┴────┴───────┘
#### total report ####
┌────┬────── ┬────┬───────┬───────┬───────┐
│ Number │ Connections │ Errors │ Message Send │ Message Fail │ Duration(ms) │
├────┼──── ──┼────┼───────┼───────┼───────┤
│ 5000   │ 1013        │ 3987   │ 0            │ 0            │ 9627         │
└────┴─── ───┴────┴───────┴───────┴───────┘

 为什么会是1000多点这个数?websocket-bench 的README.md文档也特别提醒:

Tip: You may find it useful to increase the maximum number of open file descriptors on your system during testing:

ulimit -n 60000

 ulimit 有什么特殊作用?网上搜到这篇文章 通过ulimit改善系统性能 ,原来是系统默认的文件打开数影响了连接数 ,通过命令 ulimit -a 可以看到:

core file size             (blocks, -c) 0
data seg size              (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size                    (blocks, -f) unlimited
pending signals                  (-i) 31107
max locked memory          (kbytes, -l) 64
max memory size            (kbytes, -m) unlimited
open files                   (-n) 1024
pipe size                    (512 bytes, -p) 8
POSIX message queues         (bytes, -q) 819200
real-time priority                  (-r) 0
stack size                   (kbytes, -s) 8192
cpu time                     (seconds, -t) unlimited
max user processes              (-u) 31107
virtual memory               (kbytes, -v) unlimited
file locks                     (-x) unlimited

  open files 为 1024 大概明白为什么socket连接只能有1000多点,使用 ulimit -n 60000来调整可打开文件数,再进行测试,连接数果然就正常了

 

题外话:socket长连接保持着“文件”(Linux上一切都可以看成是文件)被打开,所以1024的文件连接数很快被耗尽,虽然websocket-bench 文档中也提到ulimit -n 参数调整,但是对操作系统性能类的参数了解很少,没明白什么意思,就无视了,等发现问题的时候,猜可能是该参数影响了性能,才去查资料,所以说专业的事情还真得专业的人来解决,主机工程师或者性能测试人员对这类的参数也许就比较熟悉,知道该类参数的调整,程序员碰到可能真得一头雾水,现在明白为什么高校的计算机专业需要开设操作系统的课程了,这才是基础,虽然学的也是一头雾水^_^