ab工具是apache自带的压力测试工具。它可以对apache、nginx、iis等服务器进行网站访问压力测试。一般安装好apache就会自动带有这个工具的。
命令格式: ab -n 总共的请求执行数 -c 并发数 被测试的url (例如: ab -n 100 -c 10 http://localhost/index.php)
例子:
模拟100个用户,对http://ab.test.com/index.php请求100次,即表示总共访问http://localhost/index.php这个脚本5000次,200并发同时执行。
执行命令:
ab -n 100 -c 100 http://ab.test.com/index.php
返回结果:
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking ab.test.com (be patient).....done
Server Software: Apache/2.4.38
Server Hostname: ab.test.com //被测主机名
Server Port: 80 //端口
Document Path: /test/index.php
Document Length: 102665 bytes
Concurrency Level: 100 //并发级别,也就是并发数,请求中-c参数指定的数量
Time taken for tests: 0.177 seconds //本次测试总共花费的时间
Complete requests: 100 //本次测试总共发起的请求数量
Failed requests: 83 //失败的请求数量。因网络原因或服务器性能原因,发起的请求并不一定全部成功,通过该数值和Complete requests相除可以计算请求的失败率,作为测试结果的重要参考。
(Connect: 0, Receive: 0, Length: 83, Exceptions: 0)
Total transferred: 10286383 bytes
HTML transferred: 10266583 bytes
Requests per second: 564.94 [#/sec] (mean) //平均(mean)每秒完成的请求数:QPS,这是一个平均值,等于Complete requests/Time taken for tests=100/1.093=91.50
Time per request: 177.010 [ms] (mean) //从用户角度看,完成一个请求所需要的时间(因用户数量不止一个,服务器完成10个请求,平均每个用户才接收到一个完整的返回,所以该值是下一项数值的10倍。)
Time per request: 1.770 [ms] (mean, across all concurrent requests) // 服务器完成一个请求的时间。
Transfer rate: 56749.88 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 1
Processing: 17 101 42.7 100 165
Waiting: 4 91 42.2 94 161
Total: 17 101 42.7 100 165
Percentage of the requests served within a certain time (ms)
50% 100
66% 130
75% 138
80% 146
90% 157
95% 162
98% 164
99% 165
100% 165 (longest request)
常用参数:
-n :总共的请求执行数,缺省是1;
-c: 并发数,缺省是1;
-t:测试所进行的总时间,秒为单位,缺省50000s
-p:POST时的数据文件
-w: 以HTML表的格式输出结果