Apache Benchmark ab wrk 压力测试工具使用

Posted by Sunday on 2017-05-19
1
[root@node1 ~]# ab -n 10000 -c 1000 http://www.sundayle.com/index.html

其中-n表示请求数,-c表示并发数

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
This is ApacheBench, Version 2.3 <$Revision: 655654 $>;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.sundayle.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.10.3
Server Hostname: www.sundayle.com
Server Port: 80

Document Path: /index.html
Document Length: 42 bytes

Concurrency Level: 1000 #并发请求数
Time taken for tests: 2.221 seconds #整个测试持续的时间
Complete requests: 10000 #完成的请求数
Failed requests: 0 #完成的请求数量
Write errors: 0 #失败的请求数量
Total transferred: 2735733 bytes #整个过程中的网络传输量
HTML transferred: 420882 bytes #整个过程中的HTML内容传输量
Requests per second: 4501.56 [#/sec] (mean) #吞吐率,重要指标一,相当于LR中的每秒事务数,mean(表示平均值)(越大越好)
Time per request: 222.145 [ms] (mean) #用户平均请求等待时间,重要指标一,相当于LR中的平均事务响应时间(越小越好)
Time per request: 0.222 [ms] (mean, across all concurrent requests) #服务器平均请求处理时间,重要指标三
Transfer rate: 1202.64 [Kbytes/sec] received #平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题
Connection Times (ms) #网络消耗时间
min mean[+/-sd] median max
Connect: 0 85 249.2 10 1027
Processing: 6 51 82.2 23 1522
Waiting: 0 48 81.9 21 1519
Total: 14 136 271.8 31 1664

#这段是每个请求处理时间的分布情况,50%的处理时间在31ms内,66%的处理时间在43ms内...,重要的是看90%的处理时间。
Percentage of the requests served within a certain time (ms)
50% 31
66% 43
75% 51
80% 197
90% 288
95% 1033
98% 1050
99% 1237
100% 1664 (longest request)

关于登录的问题

有时候进行压力测试需要用户登录,怎么办?
请参考以下步骤:

1.先用账户和密码登录后,用开发者工具找到标识这个会话的Cookie值(Session ID)记下来
2.如果只用到一个Cookie,那么只需键入命令:

1
ab -n 100 -C keyvalue http://test.com/

如果需要多个Cookie,就直接设Header:

1
ab -n 100 -H “Cookie: Key1=Value1; Key2=Value2” http://test.com/

在远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大),建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会高很多。如果只有单独的一台服务器,可以直接本地测试,比远程测试效果要准确。

wrk

[wrk] (https://github.com/wg/wrk)相对于 ab 来说最大的优点是它支持多线程,这样更容易发挥多核 CPU 的能力,从而更容易测试出系统的极限能力,其典型用法如下:

1
wrk -c 100 -d 10 http://demo.sundayle.com/

其中,参数「c」表示的是并发,参数「d」表示的是整个测试持续的时间。此外,可以通过参数「t」来控制线程数(缺省值为 2),实际使用中可以视情况酌情增大。

如果顺着 ab 的使用惯性,你可能会纳闷为什么 wrk 没有类似参数「k」这样打开 HTTP 长链接的选项,这是因为 wrk 使用的是 HTTP/1.1,缺省开启的是长连接,而 ab 使用的是 HTTP/1.0,缺省开启的是短链接。

不过这也引出另一个问题,如何用 wrk 测试短链接?实际上很简单:

1
wrk -H "Connection: Close" -c 100 -d 10 http://demo.sundayle.com/

也就是说通过参数「H」传递一个自定义的 Connection 请求头来关闭长链接。此外,wrk 支持自定义脚本编程.

locust

locust相对于 ab、wrk 来说最大的优点是它不再只是测试一个 url,而是可以测试一个自定义的场景,其中可以包含多个有相互关联的 url,此外,它还是分布式可扩展的,可以模拟大量用户的访问,由此可以得到更贴近真实环境的测试数据。

http://www.jianshu.com/p/43d04d8baaf7
https://huoding.com/2017/05/31/620