參考地址:https://github.com/link1st/go-stress-testing
安裝golang環境
yum install -y golang
下載軟件包
wget -q https://codeload.github.com/link1st/go-stress-testing/zip/master unzip go-stress-testing-master.zip
下載執行命令
https://github.com/link1st/go-stress-testing/releases/download/v1.0.1/go-stress-testing-linux chmod +x go-stress-testing-linux cat >/root/go-stress-testing-master/build.sh <<EOF #!/usr/bin/env bash # 編譯linux下可以執行文件 go build -o go-stress-testing-linux main.go # 使用交叉編譯 linux和windows版本可以執行的文件 CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o go-stress-testing-linux main.go CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o go-stress-testing-win.exe main.go EOF
執行編譯
sh build.sh
命令參數說明
-c 表示並發數 -n 每個並發執行請求的次數,總請求的次數 = 並發數 * 每個並發執行請求的次數 -u 需要壓測的地址
測試百度
./go-stress-testing-linux -c 1 -n 100 -u https://www.baidu.com/
開始啟動 並發數:1 請求數:100 請求參數: request: form:http url:https://www.baidu.com/ method:GET headers:map[] data: verify:statusCode timeout:3s debu:false ─────┬───────┬───────┬───────┬────────┬────────┬────────┬────────┬──────── 耗時│ 並發數│ 成功數│ 失敗數│ qps │最長耗時│最短耗時│平均耗時│ 錯誤碼 ─────┼───────┼───────┼───────┼────────┼────────┼────────┼────────┼──────── 1s│ 1│ 31│ 0│ 31.41│ 40.56│ 28.32│ 31.83│200:31 2s│ 1│ 63│ 0│ 31.79│ 40.56│ 27.70│ 31.45│200:63 3s│ 1│ 91│ 0│ 30.56│ 154.86│ 26.33│ 32.73│200:91 3s│ 1│ 100│ 0│ 30.74│ 154.86│ 26.33│ 32.53│200:100 ************************* 結果 stat **************************** 處理協程數量: 1 請求總數: 100 總請求時間: 3.263 秒 successNum: 100 failureNum: 0 ************************* 結果 end ****************************
./go run main.go -c 1 -n 100 -u https://www.baidu.com/
─────┬───────┬───────┬───────┬────────┬────────┬────────┬────────┬──────── 耗時│ 並發數│ 成功數│ 失敗數│ qps │最長耗時│最短耗時│平均耗時│ 錯誤碼 ─────┼───────┼───────┼───────┼────────┼────────┼────────┼────────┼──────── 1s│ 1│ 29│ 0│ 30.46│ 40.27│ 27.73│ 32.83│200:29 2s│ 1│ 55│ 0│ 27.89│ 133.45│ 26.71│ 35.86│200:55 3s│ 1│ 86│ 0│ 28.80│ 133.45│ 25.89│ 34.73│200:86 4s│ 1│ 115│ 0│ 28.84│ 133.45│ 25.89│ 34.67│200:115 5s│ 1│ 146│ 0│ 29.35│ 133.45│ 25.89│ 34.07│200:146 6s│ 1│ 176│ 0│ 29.45│ 133.45│ 25.89│ 33.96│200:176 7s│ 1│ 199│ 0│ 28.62│ 184.13│ 25.25│ 34.94│200:199
參數解釋:
耗時: 程序運行耗時。程序每秒鍾輸出一次壓測結果 並發數: 並發數,啟動的協程數 成功數: 壓測中,請求成功的數量 失敗數: 壓測中,請求失敗的數量 qps: 當前壓測的QPS(每秒鍾處理請求數量) 最長耗時: 壓測中,單個請求最長的響應時長 最短耗時: 壓測中,單個請求最短的響應時長 平均耗時: 壓測中,單個請求平均的響應時長 錯誤碼: 壓測中,接口返回的 code碼:返回次數的集合
內核優化
修改程序最大打開文件數
被壓測服務器需要保持100W長連接,客戶和服務器端是通過socket通訊的,每個連接需要建立一個socket,程序需要保持100W長連接就需要單個程序能打開100W個文件句柄
vim /etc/security/limits.conf
這里需要把硬限制和軟限制、root用戶和所有用戶都設置為 1040000
core 是限制內核文件的大小,這里設置為 unlimited
添加以下參數
root soft nofile 1040000 root hard nofile 1040000 root soft nofile 1040000 root hard nproc 1040000 root soft core unlimited root hard core unlimited * soft nofile 1040000 * hard nofile 1040000 * soft nofile 1040000 * hard nproc 1040000 * soft core unlimited * hard core unlimited
注意:
/proc/sys/fs/file-max 表示系統級別的能夠打開的文件句柄的數量,不能小於limits中設置的值
如果file-max的值小於limits設置的值會導致系統重啟以后無法登錄
# file-max 設置的值參考
cat /proc/sys/fs/file-max
12553500
修改以后重啟服務器,ulimit -n 查看配置是否生效