ab(apache bench):apache下的一个工具,主要用于做web站点的压力测试
1. Ubuntu安裝ab命令
sudo apt-get install apache2-utils
若报错:
Err:1 http://security.ubuntu.com/ubuntu bionic-updates/main amd64 apache2-utils amd64 2.4.29-1ubuntu4.14
404 Not Found [IP: 91.189.88.152 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/a/apache2/apache2-utils_2.4.29-1ubuntu4.14_amd64.deb 404 Not Found [IP: 91.189.88.152 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
解决方式:
(1)修改源:sudo gedit /etc/apt/sources.list
(2)打开的文件最后添加如下两行
deb http://dk.archive.ubuntu.com/ubuntu/ xenial main
deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe
(3)更新源:sudo apt update
(4)再次安装:sudo apt-get install apache2-utils
2. 常用参数
执行ab --help查看参数:
-n:总共的请求执行数,缺省是1;
-c:并发数,缺省是1;
-t:测试所进行的总时间,秒为单位,缺省50000s
-p:POST时的数据文件
-w: 以HTML表的格式输出结果
3. 执行结果
执行 ab -n 5000 -c 200 http://localhost/test.php:
Benchmarking localhost (be patient) Completed 500 requests Completed 1000 requests Completed 1500 requests Completed 2000 requests Completed 2500 requests Completed 3000 requests Completed 3500 requests Completed 4000 requests Completed 4500 requests Completed 5000 requests Finished 5000 requests //执行完的请求次数 Server Software: Apache/2.4.3 //apache版本 Server Hostname: localhost //主机 Server Port: 80 //端口 Document Path: /test.php //路径 Document Length: 62492 bytes Concurrency Level: 200 //并发数 Time taken for tests: 3.927 seconds //完成此次请求时间 Complete requests: 5000 //完成请求次数 Failed requests: 527 //失败的请求次数 (Connect: 0, Receive: 0, Length: 527, Exceptions: 0) Total transferred: 313289422 bytes //总共传输字节 HTML transferred: 312459422 bytes Requests per second: 1273.33 [#/sec] (mean) //每秒请求次数:平均每秒处理1273个请求次数 Time per request: 157.069 [ms] (mean) //一次请求时间:平均每个请求处理时间为157毫秒 Time per request: 0.785 [ms] (mean, across all concurrent requests) //平均每个并发请求处理 时间 为0.785毫秒 Transfer rate: 77914.14 [Kbytes/sec] received //传输速率 Connection Times (ms) min mean[+/-sd] median max Connect: 0 6 73.1 0 1001 Processing: 2 150 143.9 111 1626 Waiting: 2 137 135.3 103 1197 Total: 2 155 160.3 111 1626 Percentage of the requests served within a certain time (ms) 50% 111 //在这5000个请求中有50%在111毫秒内完成 66% 141 //在这5000个请求中有66%在141毫秒内完成 75% 168 80% 191 90% 310 95% 452 98% 673 99% 951 100% 1626 (longest request)