目标

对网络库NewLife.Net进行单机百万级长连接测试,并持续收发数据,检测网络库稳定性

【2020年8月1日晚上22点】

先上源码:https://github.com/NewLifeX/NewLife.Net


结论,8月1日晚达到200万,8月2日下午达到404万。


上一次百万级压测是2017年4月1日,失败了,只达到84.5万。后来做了一次吞吐量压测。

技术革新

自上一次百万级压测(2017.4.1)以来,网络库进行了诸多细节优化(没有调整架构),特别优化了内存拷贝和多线程操作。

上次购买了20多台高配大数据服务器(16C64G和8C16G),每台发起5万Tcp长连接,纯人肉方式,相当累。这次从泥水佬那里学到本地配置多IP地址建立更多连接的方式,大大节省了服务器资源和人力成本。每台服务器设置20个IP,每个IP发起5万Tcp长连接。

测试程序也从.Net 4.5升级到了.Net Core 3.1,性能提升可观。


服务器资源

阿里云上海区共购买6台12C24G的计算型C5服务器,按照抢占型实例购买,晚上特别便宜。平时3.7每小时的价格,现在0.355就可以拿到。

image.png

这次压测对网络要求极高,所以选择的服务器必须有高带宽以及高PPS,务必是同一个机房,便于内网测试。

第一次购买的002因为设置IP失误导致弄残了服务器,不得不释放重新购买。

image.png

001~004配置多IP花了很长时间,后来才想到用命令批量设置。

默认每个ECS只有一个私网IP,可以再增加9个,一共10个。

经@景洋 提醒,ECS可以通过 绑定弹性网卡,得到20个IP地址,后面的005/002/006都有20个IP。

image.png


环境准备

优化Tcp参数

Windows默认只能使用5000个端口,需要优化Tcp参数设置。

把码神工具拷贝到服务器,直接双击运行,工具->网络调试工具,内容区点击右键,查看Tcp参数,可以看到MaxUserPort是5000,右键还有个“设置最大Tcp”把各个参数值设置为最优。无需重启,直接生效。

码神工具也可以在这里下载,http://x.newlifex.com/XCoder_Install.exe

码神工具源码:https://github.com/NewLifeX/XCoder

image.png


安装.NET Core运行时

.NET Core 3.1.6 也需要拷贝上去安装,或者直接从 http://get.dot.net 下载。服务器网络非常快。

image.png


网卡设置

为了增加每台测试端的连接数,我们需要用netsh命令增加IP地址。这些IP地址来自于ECS设置的辅助私网IP。请一定要申请辅助私网IP以后再去设置网卡,未申请的IP地址可能出问题,第一次购买的002就是这样残废了。


设置主网卡辅助私网IP

image.png

image.png


设置弹性网卡辅助私网IP

image.png

image.png

image.pngimage.png

NetBench002

netsh interface ip add address "以太网" 172.19.18.73 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.75 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.76 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.77 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.78 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.79 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.80 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.81 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.82 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.83 255.255.0.0 172.19.19.253

netsh interface ip add address "以太网 2" 172.19.18.74 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.84 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.85 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.86 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.87 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.88 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.89 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.90 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.91 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.92 255.255.0.0 172.19.19.253

netsh interface ip add dns name="以太网" addr=100.100.2.136
netsh interface ip add dns name="以太网" addr=100.100.2.138

netsh interface ip add dns name="以太网 2" addr=100.100.2.136
netsh interface ip add dns name="以太网 2" addr=100.100.2.138


NetBench005

netsh interface ip add address "以太网" 172.19.18.53 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.55 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.56 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.57 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.58 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.59 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.60 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.61 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.62 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网" 172.19.18.63 255.255.0.0 172.19.19.253

netsh interface ip add address "以太网 2" 172.19.18.54 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.64 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.65 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.66 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.67 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.68 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.69 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.70 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.71 255.255.0.0 172.19.19.253
netsh interface ip add address "以太网 2" 172.19.18.72 255.255.0.0 172.19.19.253

netsh interface ip add dns name="以太网" addr=100.100.2.136
netsh interface ip add dns name="以太网" addr=100.100.2.138

netsh interface ip add dns name="以太网 2" addr=100.100.2.136
netsh interface ip add dns name="以太网 2" addr=100.100.2.138


测试程序准备

我们采用网络库例程来充当测试程序。

服务端,https://github.com/NewLifeX/NewLife.Net/tree/master/EchoTest

客户端,https://github.com/NewLifeX/NewLife.Net/tree/master/Benchmark

源代码在这里有详细讲解

此处为文档,点击链接查看:https://newlifex.com/go/doc/8990258


二者都需要Release编译,以减少调试日志输出。

为了减少内存占用,我们需要修改配置文件`Config\Socket.config`,把缓冲区大小从64k改为1k

<?xml version="1.0" encoding="utf-8"?>
<Socket>
  <!--网络调试-->
  <Debug>false</Debug>
  <!--会话超时时间。默认20*60秒-->
  <SessionTimeout>7200</SessionTimeout>
  <!--缓冲区大小。默认64k-->
  <BufferSize>1024</BufferSize>
</Socket>

服务端客户端都要修改这个配置,否则内存占用过大。


我们选定001作为主服务器,内网IP是172.19.18.4,拷贝EchoTest到服务器目录,双击跑起来,运行模式选择1,服务端,开始监听1234端口。

002~006作为测试端,配置环境后,拷贝Bench到服务器目录,打开cmd,输入一下命令跑起来(以005为例):

netbench.exe -c 1000000 -n 1000 -i 30000 -b 172.19.18.53-72 tcp://172.19.18.4:1234

各参数解读如下:

  • -c 1000000,表示100万并发,也就是100万个tcp连接。003/004只有10个私有IP,这里最高只能10*5w=50w
  • -n 1000,每个连接收发1000次数据;
  • -i 30000,两次收发数据之间间隔30秒;
  • -b 172.19.18.53-72,发起连接时,绑定本地IP地址172.19.18.73到172.19.18.72,这些IP会均摊前面的100万
  • tcp://172.19.18.4:1234,指定目标服务端的协议地址和端口


00:45达到100万长连接

003和004各自贡献了50万连接,它们只有10个IP。

服务端在0点45分达到100万在线,非常稳定,服务端和客户端没有出现任何错误。CPU和内存占用也很好。

image.png

image.png

image.png

image.png

image.png

最后这张网络图可以看到,前面测试百万连接是,增长很快,实际上导致了部分连接失败(<0.01%),后来加上参数控制了连接速度,才有稳步爬坡。


01:19突破200万长连接

在003/004达到100万长连接并维持30分钟后,先后开启005和002,预期朝着300万方向走。

在1点19分左右,突破200万长连接。此时,CPU和内存的压力都很大,部分测试机的连接开始断开。

经检查,主服务器内存不足,导致处理能力变弱,少量客户端认为连接超时,执行自动重连,让服务器压力加大,形成雪崩。

image.png

image.png

image.png

image.png


总结

超量完成了预期目标,100万=>200万。如果这次选择48G或96G内存作为主服务器,也许真有可能上300万长连接。

2017年做的2266万tps吞吐量测试

此处为文档,点击链接查看:https://newlifex.com/go/doc/10610986


8月2日上午再战300万

购买12C96G高配实例,杭州区便宜,编号NetBench007

image.png

IP设置

netsh interface ip add address "以太网" 172.16.188.92 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网" 172.16.188.94 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网" 172.16.188.95 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网" 172.16.188.96 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网" 172.16.188.97 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网" 172.16.188.98 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网" 172.16.188.99 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网" 172.16.188.100 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网" 172.16.188.101 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网" 172.16.188.102 255.255.240.0 172.16.191.253

netsh interface ip add address "以太网 2" 172.16.188.93 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网 2" 172.16.188.103 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网 2" 172.16.188.104 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网 2" 172.16.188.105 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网 2" 172.16.188.106 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网 2" 172.16.188.107 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网 2" 172.16.188.108 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网 2" 172.16.188.109 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网 2" 172.16.188.110 255.255.240.0 172.16.191.253
netsh interface ip add address "以太网 2" 172.16.188.111 255.255.240.0 172.16.191.253

netsh interface ip add dns name="以太网" addr=100.100.2.136
netsh interface ip add dns name="以太网" addr=100.100.2.138

netsh interface ip add dns name="以太网 2" addr=100.100.2.136
netsh interface ip add dns name="以太网 2" addr=100.100.2.138


准备就绪

image.png


先来一个亿热身

开启100个并发连接,每个密集请求100万次,轻轻松松得到 130万tps 的吞吐

image.png

使用IPv6会更强一点

image.png


正式开始

本机压测命令

netbench.exe -c 1000000 -n 10000 -i 60000 -b * tcp://127.0.0.1:1234

其中`-b *`表示绑定本机所有IP地址。

image.png

3分钟轻松上100万

由于没有控制连接速度,期间有小量连接失败。控制台那里有日志时间,同时可以看到客户端服务端连接数一致。

image.png

我们等它稳定一段时间,再去准备更多服务器

image.png

从11:40开始,截止现在14:16,持续两个半小时。建立999742个连接,现存也是999742,最大值999743说明中途有一个连接断开,但是重连成功了。


增加测试机资源

再买5台同样配置服务器,编号NetBench008~NetBench012

image.png

开始配置NetBench008,每次打命令太要命了,我们给码神工具增加一个批量设置IP的功能。

image.png

从阿里云ECS控制台把辅助私网IP复制过来,点击“设置IP”,即可把这些IP地址设置到网卡上。

杭州区服务器的网络限制很严格(可能别的区也这样),在哪张网卡上申请的IP地址,只能设置到这张网卡上。即使每台服务器有两张网卡,也不能把各自的辅助私网IP设置错了,否则无法使用。


15:51达到300万长连接

服务端进程占用内存15G

image.png


15:54达到400万长连接

服务器0核4核爆满

image.png

15:56操作系统直接挂了,自动重启,连接数峰值404万。

image.png


吞吐率压测

5台测试机,每台向服务端建立100个连接,每个连接发送100万个消息,每个端平均速度170万tps

服务端总速度 170万*5=1050万tps

CPU很满,网卡PPS太高

image.png

image.png



作者:大石头 发布:2020-08-03 08:18:29 浏览:4,639)