安装了 apache 后, 在 bin 目录下会有个叫 ab 的工具。它可以模拟并发的 http 请求。我们可以用它来进行简单的压力测试。
进入 apache 的 bin 目录,运行 ./ab -V 可以查看相关信息:
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/
进行某个地址的测试:
[root
@localhost ~]# ab -n1000 -c10 http://statis.abc.cn/
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/
Benchmarking statis.abc.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests
Server Software: nginx/1.0.10
Server Hostname: statis.abc.cn
Server Port: 80
Document Path: /
Document Length: 4881 bytes
Concurrency Level: 10
Time taken for tests: 5.341769 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 5253000 bytes
HTML transferred: 4881000 bytes
Requests per second: 187.20 [#/sec] (mean)
Time per request: 53.418 [ms] (mean)
Time per request: 5.342 [ms] (mean, across all concurrent requests)
Transfer rate: 960.17 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 19 11.6 18 40
Processing: 14 33 12.0 33 57
Waiting: 10 29 11.9 29 53
Total: 24 52 9.5 51 91
Percentage of the requests served within a certain time (ms)
50% 51
66% 55
75% 56
80% 59
90% 65
95% 73
98% 78
99% 82
100% 91 (longest request)
--------------------------------------
上面信息的解释:
-n1000
总请求数为 1000
-c10
并发用户数为 10
http://app.abc.cn/
请求目标URL
Server Software
被测试的地址用的WEB服务器名称。这里是 nginx 1.0.10.它来自 HTTP 响应头。
Server Hostname
请求的URL的主机部分名称,也来自 HTTP 响应头。
Server Port
被测试的WEB服务器软件监听端口。
DocumentPath
请求的URL中的根绝对路径,也是来自HTTP请求头。
DocumentLength
响应数据正文长度
Concurrency Level
并发用户数,这里是我们设置的参数。
Time taken for tests
所有这些请求处理完成所用的时间。
Complete requests
总请求数,这里是我们设置的参数
Failed requests
失败的请求数,失败指的是请求在连接服务器,发送数据,接收数据等环境发生异常,以及无响应后超时的情况。对于超时的时间,可以在 ab -t 参数设置.
Total transferred
所有请求的响应数据长度总和。包括每个HTTP响应数据的头信息和正文数据长度。注意,它不包括请求数据的长度。
HTML transferred
表示所有请求的响应数据中正文数据的总和,也就是 Total transferred 中送去响应头的数据总和。
Requests per second
吞吐率: Complete requests / Time
Time per request
用户平均请求等待时间:Time taken for tests / (Complete requests / Concurrency Level)
Time per request
服务器平均请求处理时间:Time taken for tests /Complete requests.这正是吞吐率的倒数
Transfer rate
这些请求在单位时间内从服务器获取的数据长度:Total transferred / Time taken for tests
这个统计可以很好说明服务器在处理能力达到极限时,其出口带宽的需求量。
Percentage of the requests served within a certain time (ms)
每个请求处理时间的分布情况。上面的结果显示 80% 的请求响应时间不超过 59ms;(这是非常长的时间了,可能页面里有些复杂的逻辑)
*******************************************************
将并发用户数从 10 变成 100,其它条件不变,看结果
[root
@localhost ~]# ab -n1000 -c100 http://statis.abc.cn/
Server Software: nginx/1.0.10
Server Hostname: statis.easymobi.cn
Server Port: 80
Document Path: /
Document Length: 4881 bytes
Concurrency Level: 100
Time taken for tests: 4.746772 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 5265312 bytes
HTML transferred: 4891824 bytes
Requests per second: 210.67 [#/sec] (mean)
Time per request: 474.677 [ms] (mean)
Time per request: 4.747 [ms] (mean, across all concurrent requests)
Transfer rate: 1083.05 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 136 343.4 111 3130
Processing: 22 241 281.6 128 2382
Waiting: 18 181 166.8 125 1176
Total: 41 377 422.1 240 3247
Percentage of the requests served within a certain time (ms)
50% 240
66% 248
75% 255
80% 365
90% 766
95% 1064
98% 1483
99% 3232
100% 3247 (longest request)
从结果中看出,吞吐率从 187.20 增长到 210.67.服务器平均请求处理时间从原来的 5.342ms 降到 4.747ms.而平均等待时间从 53.418 增加到 474.677.
可见,随着并发用户的变化,吞吐率,等待时间,请求处理时间都发生了相应的变化。