ab压测

3,567 阅读4分钟

平时我们做完一个功能后,上测试环境都需要进行压测,压测的目的是提前预算程序的并发能力和高并发情况下程序是否能够正常运行,不要到线上环境才发现并发问题,比如上线一个秒杀功能,在自己本地环境一切正常,到正式环境用户来抢秒杀产品时发现卖超了

公司专业的测试人员会使用Apache JMeter,作为程序员推荐使用apache ab来进行压测,了解自己写的程序能扛住多大并发

注意:压测时不要压自己本地环境,也不要从自己本地环境去压测试服务器,都是不准确的,应该从另一台或多台服务器上进行压测。从自己本地压测试服务器不准的原因是可能会受本地网络环境影响,比如本地的网络带宽限制

安装ab

以ubuntu为例

apt install apache2-utils

常用参数

  • -n 总的请求数
  • -c 最大并发数
  • -H 指定header,如token,多个header就写多个-H
  • -T 指定content-type,如:application/json
  • -p 指定POST要发送的body,必须是本地的一个文件路径,指定了-p参数那么请求类型自动就是POST
  • -A 指定Authorization,如-A 'today:nie'

输出结果

直接在结果中添加注释来说明每一行的意思

root@today2:~# ab -n 10 -c 1 -p post.txt -T 'application/x-www-form-urlencoded' http://192.168.3.201/test.php
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.3.201 (be patient).....done


Server Software:        Apache/2.4.46  # 服务器版本
Server Hostname:        192.168.3.201  # 服务器主机名
Server Port:            80  # 服务器端口号

Document Path:          /test.php  # 请求的路径或文件名
Document Length:        4 bytes  # 单次响应的字节数,通常根据这个来判断请求是否正确,比如请求需要携带token才能访问,但是忘了带上token,结果被程序拦截了,从而导致压测的结果不正确,通过这个响应字节数可以大概判断出是不是正常的响应结果

Concurrency Level:      1  # 并发度
Time taken for tests:   0.040 seconds  # 整个压测的总耗时
Complete requests:      10  # 成功的请求数
Failed requests:        0  # 失败的请求数
Total transferred:      2050 bytes  # 总传输字节数
Total body sent:        1660  # post body发送的字节数
HTML transferred:       40 bytes  # 总响应内容字节数
Requests per second:    249.48 [#/sec] (mean)  # 1秒可以处理的请求数,这个是很关键的值
Time per request:       4.008 [ms] (mean)  # 平均每个请求需要多长时间
Time per request:       4.008 [ms] (mean, across all concurrent requests)
Transfer rate:          49.95 [Kbytes/sec] received  # 传输速率
                        40.44 kb/s sent
                        90.39 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    1   0.3      1       2
Processing:     2    3   0.9      2       5
Waiting:        1    2   0.9      1       4
Total:          3    4   1.0      4       6
WARNING: The median and mean for the processing time are not within a normal deviation
        These results are probably not that reliable.
WARNING: The median and mean for the waiting time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%      4  # 50%的请求平均耗时4ms
  66%      4
  75%      4
  80%      4
  90%      6  # 90%的请求平均耗时6ms
  95%      6
  98%      6
  99%      6
 100%      6 (longest request)  # 最长的一个请求耗时6ms

对于Connection times

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    1   0.3      1       2
Processing:     2    3   0.9      2       5
Waiting:        1    2   0.9      1       4
Total:          3    4   1.0      4       6
  1. Connect表示连接耗时
  2. Processing表示服务端处理耗时
  3. Waiting连接成功后等待响应的耗时

min表示最小耗时,median表示中间值,max表示最大耗时

以上最重要的结果是Requests per second,如果请求量和并发度很低,这个数是不准的,压测的时候也是逐步增加请求量和并发度,直至出现Failed requests说明已经扛不住当前的请求,那么此时的Requests per second就是极限了

示例

普通GET请求,并发度10,一共100个请求

ab -n 100 -c 10 http://xxx.com/test.php

发送x-www-form-urlencoded POST请求

// post.txt的内容
cat post.txt
a=1&b=2

ab -n 100 -c 10 -p post.txt -T 'application/x-www-form-urlencoded' http://192.168.3.201/test.php

携带token

ab -n 10 -c 1 -H 'token: abc'  http://192.168.3.201/test.php
// 多个header
ab -n 10 -c 1 -H 'token: abc' -H 'version: v1'  http://192.168.3.201/test.php

socket: Too many open files (24)

Linux操作系统默认最多允许打开1024个文件句柄,可以通过ulimit -n查看

ulimit -n
ulimit -n 102400

ulimit -n 102400临时设置到102400个

总结

对于ab命令,以上这些已经够程序员压测单个接口使用了,更专业一些的模拟用户登录、点击、随机等待一段时间再点击等操作要通过JMeter或测试人员写代码来压测

另外ab命令还可以用来对指定网站短时间内产生大量并发请求