[NewLife.Net]單機400萬長連接壓力測試

目標

對網絡庫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://www.yuque.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://www.yuque.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

image.png

image.png

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章