通过ab进行压力测试

版本

1
2
3
4
~  ab -V
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/

用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
~ 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
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T 请求包含的post文件,与-T一起使用,设置-p就不需要设置-m
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg. 设置content-type
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print 显示详细程度。默认1,显示请求总统计;2显示响应头响应数据;3显示响应状态码;4更多信息
-w Print out results in HTML tables 以html表格的元素显示测试结果
-i Use HEAD instead of GET 执行HEAD请求,而不是GET请求
-x attributes String to insert as table attributes 设置 table 属性的字符串
-y attributes String to insert as tr attributes 设置 tr 属性的字符串
-z attributes String to insert as td or th attributes 设置 td 属性的字符串
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable) 添加cookie
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip' 添加额外头信息
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use 对请求使用代理服务器
-V Print version number and exit 打印版本号
-k Use HTTP KeepAlive feature 打开HTTP KeepAlive功能,在一个HTTP会话中执行多个请求,默认不启用
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file. 测试结果写入gnuplot格式文件,例如Excel
-e filename Output CSV file with percentages served 测试结果写入CSV格式文件
-r Don't exit on socket receive errors.
-m method Method name 方法名
-h Display usage information (this message) 打印帮助
-I Disable TLS Server Name Indication (SNI) extension
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(TLS1, TLS1.1, TLS1.2 or ALL)
-E certfile Specify optional client certificate chain and private key

执行压测

例如对某个接口压测,https://www.baidu.com/

请求1000次,并发10个

1
ab -n 1000 -c 10 https://www.baidu.com/

执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
~ ab -n 1000 -c 10 https://www.baidu.com/
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 www.baidu.com (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
Completed 1000 requests
Finished 1000 requests


Server Software: BWS/1.1
Server Hostname: www.baidu.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
Server Temp Key: ECDH P-256 256 bits
TLS Server Name: www.baidu.com

Document Path: / 请求路径
Document Length: 227 bytes 响应数据正文长度

Concurrency Level: 10 并发个数
Time taken for tests: 6.131 seconds 测试所花时间
Complete requests: 1000 返回成功的请求数
Failed requests: 0 失败的请求数
Total transferred: 1110891 bytes 所有请求响应的数据总和
HTML transferred: 227000 bytes 所有请求的数据总和
Requests per second: 163.11 [#/sec] (mean) 吞吐量,平均每秒完成的请求数
Time per request: 61.307 [ms] (mean) 每个请求花的时间
Time per request: 6.131 [ms] (mean, across all concurrent requests) 服务器平均等待时间
Transfer rate: 176.96 [Kbytes/sec] received 传输率,单位时间从服务器获取的数据长度

Connection Times (ms)
min mean[+/-sd] median max
Connect: 28 46 8.3 45 85
Processing: 8 15 6.6 13 76
Waiting: 8 14 5.6 12 76
Total: 37 60 10.9 58 135

Percentage of the requests served within a certain time (ms)
50% 58
66% 62
75% 65
80% 68
90% 75 90%的请求在75ms内
95% 81
98% 86
99% 99
100% 135 (longest request)

如果是POST请求,需要发送参数

1
ab -n 1000 -c 10 -T 'application/json' -p trigger.txt http://www.xxx.com/posturl

其中trigger.txt文件中包含请求体信息