搭建go-stress-testing压力测试

参考地址: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 查看配置是否生效