压测工具(vegeta)-测试haproxy

发布时间 2023-12-28 11:01:40作者: Macoffee

一、压测工具vegeta

1、介绍

Vegeta 是一个用 Go 语言编写的多功能的 HTTP 负载测试工具,它提供了命令行工具和一个开发库。 

官方地址:https://github.com/tsenart/vegeta

2、安装 Vegeta

 wget https://github.com/tsenart/vegeta/releases/download/v7.0.3/vegeta-7.0.3-linux-amd64.tar.gz
tar -xf vegeta-7.0.3-linux-amd64.tar.gz -C vegeta
cp vegeta /usr/sbin/
#查看版本
vegeta  -version


3、Vegeta 用法(常用参数)

 1 -cpus int:    使用 CPU 的数量 (默认为 4 个)
 2   -profile string:    指定在执行期间启用哪个分析器,支持 cpu 和 heap。
 3   -version:    打印版本并退出。attack command:
 4   -body string:    指定请求主体文件里的内容。
 5   -cert string:    指定用于 HTTPS 请求的 PEM 格式的客户端证书文件。如果 -key 未指定,它会被设置为这个标志的值。
 6   -connections int:    指定每个目标主机打开的空闲连接的最大数目,默认值为 10000 7   -duration duration:    指定发送请求到目标主机的时长,用 0 表示永久。
 8   -header value:    指定目标的请求头,可以重复指定多个请求头。
 9   -http2:    指定是否向支持的服务器发送 HTTP/2 请求,默认为:true10   -insecure:    指定是否忽略无效的服务器 TLS 证书。
11   -keepalive:    指定是否使用持久链接,默认值为:true12   -key string:    指定 HTTPS 请求中使用的 PEM 编码的 SSL 客户端证书私钥文件。
13   -laddr value:    指定要使用的本地 I P地址,默认值为:0.0.0.014   -lazy:    指定是否使用延迟模式读取目标。
15   -output string:    指定输出文件的位置,默认为标准输出。
16   -rate uint:    指定每秒钟对目标发送的请求数,默认值为:5017   -redirects int:    指定每个请求的重定向的最大次数,默认为 10 次。当值为 -1, 不会遵循重定向但响应标记为成功。
18   -root-certs value:    指定可信的 TLS 根证书文件,多个的情况下使用逗号分隔。如果未指定,使用系统默认的 CA 证书。
19   -targets string:    指定目标文件,默认为标准输入。
20   -timeout duration:    指定每个请求的超时时间,默认值为 30s。
21   -workers uint:    指定初始化进程数量,默认值为 1022   -inputs string:    指定报告输入文件,默认为标准输入。
23   -output string:    指定报告输出文件,默认为标准输出。
24   -reporter string:    指定要生成的报告的格式,支持 text,json, plot, hist[buckets]。默认为文本。dump command:
25   -dumper string:    指定转存文件,支持 json, csv 格式。默认为 json 格式。
26   -inputs string:    指定要转存的输入文件,默认为标准输入,指定多个用逗号分隔。
27   -output string:    指定要转存的输出文件,默认为标准输出。

4. 系统指标查看

[root@3b9d411b-0a16-4bc4-acd8-10f71b997c29 etc]# ss -s&&free -g
Total: 102008 (kernel 102040)
TCP:   102003 (estab 101812, closed 56, orphaned 77, synrecv 0, timewait 0/0), ports 30060

Transport Total     IP        IPv6
*     102040    -         -        
RAW   0         0         0        
UDP   4         3         1        
TCP   101947    101946    1        
INET      101951    101949    2        
FRAG      0         0         0        

             total       used       free     shared    buffers     cached
Mem:             7          2          5          0          0          0
-/+ buffers/cache:          2          5
Swap:            0          0          0
  • 这台机器已将建立10w个链接,最高压测到17w左右
  • 但是内存和cpu都没有到达瓶颈

5. 系统参数sysctl.conf配置

net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536
# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 2
net.ipv4.conf.all.rp_filter = 2
net.ipv4.conf.all.send_redirects = 1
net.ipv4.conf.default.send_redirects = 1
### 表示是否打开TCP同步标签(syncookie),同步标签可以防止一个套接字在有过多试图连接到达时引起过载
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.ip_local_port_range = 20000 65000
net.ipv4.tcp_max_syn_backlog = 40960
net.ipv4.tcp_max_tw_buckets = 819200
net.core.somaxconn = 262144
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_synack_retries = 3
net.ipv4.tcp_syn_retries = 3
net.ipv4.tcp_sack = 1
net.ipv4.tcp_fack = 1
net.ipv4.tcp_dsack = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
### 系统中所允许的文件句柄的最大数目
fs.file-max = 65535
### 单个进程所允许的文件句柄的最大数目
fs.nr_open = 65535
kernel.pid_max = 65536
net.ipv4.tcp_rmem = 4096 87380 8388608
net.ipv4.tcp_wmem = 4096 87380 8388608
net.core.rmem_max = 8388608
net.core.wmem_max = 8388608
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_window_scaling = 1

6.  vi /etc/security/limits.conf配置

*                soft    nproc           65535
*                hard    nproc           65535
*                soft    nofile          65535
*                hard    nofile          65535

 7. haproxy 配置

global
    group root
    user root
    daemon
    nbproc 1
    log 127.0.0.1 local3
    pidfile /var/run/haproxy/pid/10.163.162.65_80.pid
    ulimit-n 1000000 ##这里增大文件打开数
    max-spread-checks  1000ms
    maxconn     30000000 ##这里放开限制
    maxconnrate 30000000
    maxsessrate 30000000
    maxsslconn  30000000
    maxsslrate  30000000
    tune.ssl.default-dh-param 2048
    spread-checks 20
    stats timeout 5000ms
    stats maxconn 50
    stats socket /var/run/haproxy/sock/10.163.162.65_80.sock mode 600 level admin process 1
defaults
    mode http
    maxconn     30000000 ##这里最好与上面一致,否则默认限制2000,找了好久
    option  abortonclose
    option redispatch
    option forwardfor
    balance roundrobin
    log 127.0.0.1 local3 err
    retries 3
    option clitcpka
    option srvtcpka
listen 10.163.162.65:80
    balance roundrobin
    bind 10.163.162.65:80  
    option tcp-check
    option httplog
    #option dontlognull
    timeout http-keep-alive 10s
    timeout http-request    10s
    errorfile 400 /usr/local/haproxy/errorfile/400.html
    errorfile 403 /usr/local/haproxy/errorfile/403.html
    errorfile 408 /usr/local/haproxy/errorfile/408.html
    errorfile 500 /usr/local/haproxy/errorfile/500.html
    errorfile 502 /usr/local/haproxy/errorfile/502.html
    errorfile 503 /usr/local/haproxy/errorfile/503.html
    errorfile 504 /usr/local/haproxy/errorfile/504.html
    timeout connect 100000
    timeout queue   100000
    timeout client  100000
    timeout server  100000
    timeout check   100000
    cookie SERVERID insert indirect nocache
     
    ##后端服务器
    server  172.16.100.110:8480 172.16.100.110:8480 cookie 1852060044 check inter 30000 rise 3 fall 3 weight 22  
    server  172.16.100.110:8180 172.16.100.110:8180 cookie 1852051288 check inter 30000 rise 3 fall 3 weight 22  
    server  172.16.100.110:8080 172.16.100.110:8080 cookie 1852051260 check inter 30000 rise 3 fall 3 weight 22  
    server  172.16.100.110:8280 172.16.100.110:8280 cookie 1852059892 check inter 30000 rise 3 fall 3 weight 22  
    server  172.16.100.110:8380 172.16.100.110:8380 cookie 1852059664 check inter 30000 rise 3 fall 3 weight 22 
        
    server  172.16.100.162:8480 172.16.100.162:8480 cookie 2852060044 check inter 30000 rise 3 fall 3 weight 22  
    server  172.16.100.162:8180 172.16.100.162:8180 cookie 2852051288 check inter 30000 rise 3 fall 3 weight 22  
    server  172.16.100.162:8080 172.16.100.162:8080 cookie 2852051260 check inter 30000 rise 3 fall 3 weight 22  
    server  172.16.100.162:8280 172.16.100.162:8280 cookie 2852059892 check inter 30000 rise 3 fall 3 weight 22  
    server  172.16.100.162:8380 172.16.100.162:8380 cookie 2852059664 check inter 30000 rise 3 fall 3 weight 22

8. Vegeta 使用实例

可配置多台vegeta客户端进行压测,我这里配置了两台客户端同时压测

这里我们简单了解下Vegeta提供的一些参数细节:

1 -cpus=4,定义客户端使用的内核数量。为了能够达到需要的压力值,我们将施压机配置适当调整。仔细观察结果数据会发现,实际压力并不大,配置调整的主要目的是为了能够支撑大量状态为后端服务器休眠的连接。
2 -duration=5m,该参数顾名思义,如果没有指定执行时间,测试将永久运行。
3 -rate=2000,每秒钟请求数。     

结论

内核主要优化的点:

    • 内核中 fs.file-max = 65535 ### 系统中所允许的文件句柄的最大数目,客户端内核也要优化
    • net.ipv4.tcp_syncookies = 1 ### 表示是否打开TCP同步标签(syncookie),同步标签可以防止一个套接字在有过多试图连接到达时引起过载
    • 其他内核参数也要适当的优化

haproxy主要优化的点:

  global的

  • ulimit-n 1000000 ##这里增大文件打开数
  • maxconn 30000000 ##这里放开限制

  defaults 中的

  • maxconn 30000000 ##这里最好与上面一致,否则默认限制2000
    压测工具:
  • ab很消耗客户端资源而且压力源不够稳定,没有多核提升性能
  • vegeta这个工具很好压力源稳定,还有很多的功能没有挖掘出来
    压测工具直接影响压测结果,所以一定要选好。
    后端服务:
  • 后端服务器不要太多,本次测试时发现10台以上不会增加多连接数
  • 每个服务器内存分大点,跑5个tomcat很容易被打死
 1 #使用标准输入进行压测并生成报告
 2 [root@localhost1] echo "POST http://xx.xx.xx.xx/index.html" | vegeta -cpus=4 attack -duration=5m  -header="sleep:1000"  -rate=5000 -workers=500 | tee reports.bin | vegeta report
 3 Requests(请求)              [total, rate]            1500000, 5000.00
 4 Duration(持续时间)            [total, attack, wait]    5m0.807649684s, 4m59.999899082s, 807.750602ms
 5 Latencies(响应延时时间)         [mean(平均), 50, 95, 99, max(最大)]  3.731056ms, 2.539327ms, 6.724723ms, 12.62445ms, 1.022693434s
 6 Bytes In(请求的大小(字节))      [total, mean]            928500000, 619.00
 7 Bytes Out(字节输出)          [total, mean]            0, 0.00
 8 Success(请求成功率)           [ratio]                  100.00%
 9 Status Codes  [code(状态码):count(请求次数)]             200:1500000  
10 Error Set:(错误集)