ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed to give you an impression of how your current Apache installation performs. This especially shows you how many requests per second your Apache installation is capable of serving.
ab 是 apache http server 基准测试工具,它可以帮助我们去了解当前服务的表现,如每秒请求数等。下面是 ab 对应的所有 options 摘要信息:
执行如下命令 ab -n 200 -c 100 http://localhost:8080/api/test;总共发送 200 个请求,每次请求并发 100,得到的结果如下:
1 2 3 4 5 6 7
➜ ~ ab -n 200 -c 100 http://localhost:8080/api/test This is ApacheBench, Version 2.3 <$Revision: 1879490 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient) apr_pollset_poll: The timeout specified has expired (70007)
➜ ~ ab -n 200 -c 10 -s 5000 -k http://localhost:8080/api/test This is ApacheBench, Version 2.3 <$Revision: 1879490 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/
Server Software: Server Hostname: localhost Server Port: 8080
Document Path: /api/test Document Length: 6 bytes
Concurrency Level: 10 Time taken for tests: 352.210 seconds Complete requests: 200 Failed requests: 8 (Connect: 0, Receive: 0, Length: 8, Exceptions: 0) Keep-Alive requests: 192 Total transferred: 27840 bytes HTML transferred: 1152 bytes Requests per second: 0.57 [#/sec] (mean) Time per request: 17610.485 [ms] (mean) Time per request: 1761.049 [ms] (mean, across all concurrent requests) Transfer rate: 0.08 [Kbytes/sec] received
Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.1 0 1 Processing: 3002 15972 44971.8 3006 218613 Waiting: 0 11492 40519.5 3006 218613 Total: 3002 15972 44971.9 3006 218613
Percentage of the requests served within a certain time (ms) 50% 3006 66% 3006 75% 3007 80% 3007 90% 3008 95% 112001 98% 209597 99% 215607 100% 218613 (longest request)
➜ ~ ab -n 200 -c 100 -s 5000 -k -r http://localhost:8080/api/test This is ApacheBench, Version 2.3 <$Revision: 1879490 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/
Server Software: Server Hostname: localhost Server Port: 8080
Document Path: /api/test Document Length: 6 bytes
Concurrency Level: 100 Time taken for tests: 107.100 seconds Complete requests: 200 Failed requests: 147 (Connect: 0, Receive: 0, Length: 147, Exceptions: 0) Keep-Alive requests: 53 Total transferred: 7685 bytes HTML transferred: 318 bytes Requests per second: 1.87 [#/sec] (mean) Time per request: 53549.816 [ms] (mean) Time per request: 535.498 [ms] (mean, across all concurrent requests) Transfer rate: 0.07 [Kbytes/sec] received
Connection Times (ms) min mean[+/-sd] median max Connect: 0 3 2.1 3 8 Processing: 3002 43074 19044.3 49998 75116 Waiting: 0 5502 15736.0 0 75116 Total: 3002 43077 19045.5 50002 75117
Percentage of the requests served within a certain time (ms) 50% 50002 66% 50003 75% 54088 80% 54089 90% 54090 95% 54092 98% 66104 99% 72112 100% 75117 (longest request)
Connection reset by peer
1 2 3 4 5 6 7
➜ ~ ab -n 200 -c 100 -s 5000 -k http://localhost:8080/api/test This is ApacheBench, Version 2.3 <$Revision: 1879490 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient) apr_socket_recv: Connection reset by peer (54)
Connection reset by peer也会导致 ab 中途退出,从本地测试来看,当并发数增加时,出现该问题的比率会增加;Connection Reset by peer 意味着远端终止了会话,即当操作系统接收到远端服务器的RST (TCP Reset)通知时会产生此错误。在使用 ab 时,可以通过 -r 来忽略这种错误,在遇到socket接收错误后,不退出测试。
ab 的结果解析
版本相关
1 2 3
This is ApacheBench, Version 2.3 <$Revision: 1879490 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/
Server Software: Server Hostname: localhost Server Port: 8080
Server Software 一栏没有数据,因为本地启动的 server 容器;其他两个就是地址和端口。
地址和包大小
1 2
Document Path: /api/test Document Length: 6 bytes
测试数据详情
1 2 3 4 5 6 7 8 9 10 11 12
Concurrency Level: 100 Time taken for tests: 107.100 seconds Complete requests: 200 Failed requests: 147 (Connect: 0, Receive: 0, Length: 147, Exceptions: 0) Keep-Alive requests: 53 Total transferred: 7685 bytes HTML transferred: 318 bytes Requests per second: 1.87 [#/sec] (mean) Time per request: 53549.816 [ms] (mean) Time per request: 535.498 [ms] (mean, across all concurrent requests) Transfer rate: 0.07 [Kbytes/sec] received
Total transferred:总共传输的数据量,指的是 ab 从被测服务器接收到的总数据量,包括文本内容和请求头信息。
HTML transferred:从服务器接收到的 html 文件的总大小,等于 Document Length*Complete requests
Requests per second:平均每秒完成的请求数:QPS,是一个平均值,等于 Complete requests/Time taken for tests
Time per request:从用户角度看,完成一个请求所需要的时间(因用户数量不止一个,服务器完成 10 个请求,平均每个用户才接收到一个完整的返回,所以该值是下一项数值的10倍。)
Time per request:服务器完成一个请求的时间。
Transfer rate:网络传输速度,对于大文件的请求测试,这个值很容易成为系统瓶颈所在。要确定该值是不是瓶颈,需要了解客户端和被测服务器之间的网络情况,包括网络带宽和网卡速度等信息。
Time per request 统计信息
1 2 3 4 5 6
Connection Times (ms) min mean[+/-sd] median max Connect: 0 3 2.1 3 8 Processing: 3002 43074 19044.3 49998 75116 Waiting: 0 5502 15736.0 0 75116 Total: 3002 43077 19045.5 50002 75117
对上面第一个 Time per request 进行细分和统计。一个请求的响应时间可以分成网络链接(Connect),系统处理(Processing)和等待(Waiting)三个部分。表中 min 表示最小值; mean 表示平均值;[+/-sd] 表示标准差(Standard Deviation) ,也称均方差(mean square error),该数值越大表示数据越分散,系统响应时间越不稳定。 median 表示中位数; max 当表示最大值。
Total 是从整个请求所需要的时间的角度来统计的。这里可以看到最慢的一个请求花费了 75117 ms,这个数据可以在下面的表中得到验证。
1 2 3 4 5 6 7 8 9 10
Percentage of the requests served within a certain time (ms) 50% 50002 66% 50003 75% 54088 80% 54089 90% 54090 95% 54092 98% 66104 99% 72112 100% 75117 (longest request)
这个表第一行表示有 50% 的请求都是在 50002 ms 内完成的,这个值是比较接近平均系统响应时间(第一个 Time per request: 53549.816 ms ); 以此类推。
前面看到响应时间最长的那个请求是 75117 ms,那么显然所有请求(100%)的时间都是小于等于 75117 ms 的,也就是表中最后一行的数据就是时间最长的那个请求(longest request)。