Apache AB压测工具
原创大约 2 分钟
ab
是Apache Bench的简称,它是用来对程序执行压力测试的一款简易工具。
在有些Linux发行版中,例如,CentOS6.5
里面已经自带了ab
工具,无需额外安装,直接在命令行中打开就能用。

命令选项虽多,但常用的差不多只有四个。
-n
:表示requests
请求,用于指定一次会话内压力测试的总执行次数。-c
:表示concurrency
并发,用于指定的并发数。-t
:表示timelimit
时限,用于指定任务完成的最大时间(单位:秒)。-s
:表示timeout
超时,用于指定Socket
多久无响应之后就关闭。
例如,如果想执行1000次请求,每次请求有100个并发,并在1秒之内完成请求,超时时间5秒。那么命令就可以这样写。

> ab -n1000 -c10 -t1 -s5 http://www.baidu.com/
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.baidu.com (be patient)
Finished 76 requests
Server Software: bfe
Server Hostname: www.baidu.com
Server Port: 80
Document Path: /
Document Length: 2381 bytes
# 并发数,就是-c指定的参数
Concurrency Level: 10
# 测试用时
Time taken for tests: 1.031 seconds
Complete requests: 76
Failed requests: 0
Total transferred: 190400 bytes
HTML transferred: 181468 bytes
# 每秒请求数,这也用来表示服务器的吞吐量
Requests per second: 73.70 [#/sec] (mean)
# 用户请求平均等待时间
Time per request: 135.685 [ms] (mean)
# 每个请求实际运行的平均时间
Time per request: 13.568 [ms] (mean, across all concurrent requests)
# 传输率,也就是每秒获取的数据量
Transfer rate: 180.31 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
# 连接的最小时间,平均值,中值,最大值
Connect: 1 1 0.3 1 2
# 处理的最小时间,平均值,中值,最大值
Processing: 79 112 38.7 105 343
# 等待的最小时间,平均值,中值,最大值
Waiting: 79 110 37.8 102 330
Total: 80 113 38.7 106 344
Percentage of the requests served within a certain time (ms)
# 50%的请求在106ms内返回
50% 106
# 60%的请求在111ms内返回,以下类推
66% 111
75% 113
80% 115
90% 121
95% 130
98% 331
99% 344
100% 344 (longest request)
输出的内容非常容易看懂。
不管是GET
请求还是POST
请求,除了命令格式稍有不同之外,其他都一样的。
感谢支持
更多内容,请移步《超级个体》。