设置相关脚本:
默认:
$cat /proc/sys/net/ipv4/ip_local_port_range
32768 61000
$cat setting.sh
#/bin/bash
# modify backlog, meanwhile change listen function to 1024, default 128
# echo “1024" > /proc/sys/net/core/somaxconn
# echo “1024" > /proc/sys/net/core/somaxconn
sysctl -w net.core.somaxconn = 1024
# more ports for testing
sysctl -w net.ipv4.ip_local_port_range="1025 65535" // 1025 - 6535, about 50000 port avaiable
# tcp read buffer, min, default, maximum
sysctl -w net.ipv4.tcp_rmem="2048 2048 16777216" // adjust read buffer -> 2k
# tcp write buffer, min, default, maximum
sysctl -w net.ipv4.tcp_wmem="2048 2048 16777216" // djust write buffer -> 2k, so every socket take about 4k bytes, tested 3.75k.
# tcp buffer, min, default, maximum, uint = page,page = 4096 byte, we can change the value based on mem
sysctl -w net.ipv4.tcp_mem="1250000 1500000 1750000" // min 5G, default 6G max 7G, 7G/4K about 175w connections
# tcp
sysctl -w net.ipv4.tcp_max_orphans="200000" // as large as possible, consider physical mem.
echo 9999999 | tee /proc/sys/fs/nr_open
echo 9999999 | tee /proc/sys/fs/file-max
ulimit -n 9999999
#ulimit -HSn 9999999, setting soft link and hard link
# edit /etc/security/limits.conf, add line, for all user
sysctl -w net.ipv4.ip_local_port_range="1025 65535" // 1025 - 6535, about 50000 port avaiable
# tcp read buffer, min, default, maximum
sysctl -w net.ipv4.tcp_rmem="2048 2048 16777216" // adjust read buffer -> 2k
# tcp write buffer, min, default, maximum
sysctl -w net.ipv4.tcp_wmem="2048 2048 16777216" // djust write buffer -> 2k, so every socket take about 4k bytes, tested 3.75k.
# tcp buffer, min, default, maximum, uint = page,page = 4096 byte, we can change the value based on mem
sysctl -w net.ipv4.tcp_mem="1250000 1500000 1750000" // min 5G, default 6G max 7G, 7G/4K about 175w connections
# tcp
sysctl -w net.ipv4.tcp_max_orphans="200000" // as large as possible, consider physical mem.
echo 9999999 | tee /proc/sys/fs/nr_open
echo 9999999 | tee /proc/sys/fs/file-max
ulimit -n 9999999
#ulimit -HSn 9999999, setting soft link and hard link
# edit /etc/security/limits.conf, add line, for all user
#* - nofile 9999999
这样修改的结果是临时有效,如果系统重启将失效,如果永久生效:
echo “net.core.somaxconn = 1024" >> /etc/sysctl.conf
echo "net.ipv4.ip_local_port_range = 1024 65535" >> /etc/sysctl.conf
echo "net.ipv4.tcp_rmem = 2048 4096 16777216" >> /etc/sysctl.conf
echo "net.ipv4.tcp_wmem = 2048 4096 16777216" >> /etc/sysctl.conf
echo "net.ipv4.tcp_mem = 1250000 1500000 1750000" >> /etc/sysctl.conf
echo "net.ipv4.tcp_max_orphans=200000" >> /etc/sysctl.conf
echo "fs.nr_open = 9999999" >> /etc/sysctl.conf
echo "fs.file-max = 9999999" >> /etc/sysctl.conf
echo "net.ipv4.tcp_rmem = 2048 4096 16777216" >> /etc/sysctl.conf
echo "net.ipv4.tcp_wmem = 2048 4096 16777216" >> /etc/sysctl.conf
echo "net.ipv4.tcp_mem = 1250000 1500000 1750000" >> /etc/sysctl.conf
echo "net.ipv4.tcp_max_orphans=200000" >> /etc/sysctl.conf
echo "fs.nr_open = 9999999" >> /etc/sysctl.conf
echo "fs.file-max = 9999999" >> /etc/sysctl.conf
echo "* - nofile 9999999" >> /etc/security/limits.conf
测试方式:
A 服务端 8G 内存, 服务端启动端口 5050 地址:192.168.1.2
B client 8G 内存, 客户端启动,设置20个ip 地址别名 192.168.1.100 - 192.168.1.120, 批量设置: for i in `seq 100 120`; do sudo ifconfig eth0:$i 192.168.1.$i up ; done
客户端发送每个ip地址绑定发送 50000, 共 20个 20 * 50000 = 100万
发送命令:
1. ./epoll_cli -s 192.168.1.2 -p 5050 -m -t 192.168.1.99 -n 10 -c 50000 // 绑定 100 - 110,建立50w连接
2. ./epoll_cli -s 192.168.1.2 -p 5050 -m -t 192.168.1.109 -n 10 -c 50000 // 绑定 110 - 120,建立50w连接
C 使用ACE中的性能测试工具对,多连接情况下进行测试
$ACE_ROOT/performance-tests/TCP/tcp_test -c localhost -i 500000 -b 64 -p 5050
代码下载:
https://github.com/DavadDi/blogs/blob/master/epoll_server.tgz
https://github.com/DavadDi/blogs/blob/master/epoll_cli.tgz
测试代码使用了log4plush作为日志,如果不使用可以通过删除my_logger拿掉,makefile也需要自己修改一下包含路径,仅供参考。
注意:
1 .测试的同时使用dmesg查看系统内核错误
2. 通过 /proc/net/sockstat 查看总体情况,最好不要使用netstat
$cat /proc/net/sockstat
sockets: used 1000305
TCP: inuse 1000013 orphan 0 tw 0 alloc 1000017 mem 6
UDP: inuse 5 mem 0
UDPLITE: inuse 0
RAW: inuse 0
FRAG: inuse 0 memory 0
170万连接图: 最后一个窗口为 tail -f /var/log/message,空白表示无错误发生
相关资料介绍和总结:
详细介绍测试相关资料:
1. 100万并发连接服务器笔记之准备篇
2. 100万并发连接服务器笔记之处理端口数量受限问题
3.100万并发连接服务器笔记之测试端就绪
4. 100万并发连接服务器笔记之1M并发连接目标达成
5. How far epoll can push concurrent socket connection
https://github.com/shenfeng/dictionary epoll 高性能在线词典项目
Linux kernel tuning for c500k
A Million-user Comet Application with Mochiweb, Part 3
iComet 相关, 可以参考用来做推送服务器
iComet 是一个使用 C++ 语言开发的支持百万并发连接的 comet/push 服务器, 支持百万级并发连接
构建C1000K的服务器(1) – 基础
构建C1000K的服务器(2) – 实现
TCP长连接的思考和相关问题的实验_百万链接数测试_百万链接内核参数的调整
Troubleshooting the "Out of socket memory" error
Linux Increase the maximum number of open file
Linux TCP/IP tuning