ab压测入门
ab是一款轻量级url压测工具,通过命令行即可对指定url进行压测。以debian为例,首先安装ab
apt-get install apache2-utils
使用ab -h即可查看相关命令,这里常用的option就是前四个,-n是指请求数量,比如-n 1000就是一共发起1000个请求。-c是并发量,-c 100代表同时发出100个请求。-t用来指定压测的时间限制。-s用来指定时间限制,超过后代表超时。
ab -h
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
目前本机在127.0.0.1下有一个nginx服务器,访问80端口后会返回html文件,下面以该服务器为例在本地进行简单压测。
ab -n 500 -c 100 http://127.0.0.1:80/
上述命令对localhost的80端口进行了压测,一共发起500个请求,并发数量是100,下面看一下压测报告
Server Software: nginx/1.14.2
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /
Document Length: 612 bytes
第一部分是压测的相关信息,这里可以看到压测的server是nginx,除此之外还有port和host等信息。
Concurrency Level: 100
Time taken for tests: 0.055 seconds
Complete requests: 500
Failed requests: 0
Total transferred: 422500 bytes
HTML transferred: 306000 bytes
Requests per second: 9133.75 [#/sec] (mean)
Time per request: 10.948 [ms] (mean)
Time per request: 0.109 [ms] (mean, across all concurrent requests)
Transfer rate: 7537.13 [Kbytes/sec] received
这一部分比较重要。
Concurrency Level是指并发数量,就是前面指定的-c 100
Time taken for tests是本次压测花费的时间,因为是直接在本地进行测试,0.055s就处理完了所有请求。
Complete request和Failed requests分别指完成的请求数和失败的请求数,如果server压力过大很可能会出现timeout导致failed
Total transferred是指压测过程中发送数据的总量,包括HTTP Header等数据。HTML transferred就是HTML所占的和数据量,这里我们进行了500此请求,HTML数据总量有306000 bytes
RPS是该服务器每秒能处理的请求数量,在当前环境下我们的nginx每秒能处理9133个请求,RPS是衡量服务器性能的重要指标
Time per request就是处理每个请求所占用的时间,这两项的区别可以看这一篇blog https://www.imooc.com/article/19979
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 1.5 3 7
Processing: 2 7 3.2 6 18
Waiting: 1 6 3.2 5 15
Total: 5 10 3.2 9 20
这一部分是对请求时间的统计。
从上向下分别是连接时间,server处理请求的时间,请求等待时间。从左向右分别是最小值,最大值,方差(方差越大说明服务器不稳定),中位数和最大值。
Percentage of the requests served within a certain time (ms)
50% 9
66% 10
75% 11
80% 12
90% 14
95% 18
98% 18
99% 18
100% 20 (longest request)
最后一部分是对请求数量和时间的分析,50%的请求可以在9ms内处理完毕,100%的请求处理时间不会超过20ms(这里的数据也对应上一部分的Connection Times)