一篇学会Apache测试工具ab的使用

为什么要做压测

做为一名合格的后端工程师,在实现业务的同时,也要学会使用压测工具自己进行测试。现在大多数公司都采用前后端分离的方式,那么自己写的接口运行速度如何,QPS能达到多少,加入大并发的情况下,是否会出现因为并发导致的数据不一致问题,这是必须考虑的问题。

Apache的ab工具简介

Apache Bench,是 Apache 自带的压力测试工具,简称ab。

ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。

ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。

安装

Linux

1
2
3
yum -y install httpd-tools  //centos
apt-get install apache2-utils //ubuntu
ab -V #查看版本

Window

这里我就简单说下,首先下载Apache,安装后到服务中启用,然后进入到Apache的bin目录中,如我的安装目录在“D:\Apache24\bin”,进入后运行命令行。

参数介绍

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
D:\Apache24\bin>ab
ab: wrong number of arguments
Usage: ab [options] [http://]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
-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.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
-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
-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.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-m method Method name
-h Display usage information (this message)

-n:请求的总数量,默认是一次

-c:每次请求的数量。默认是一次一个。

-t:测试所进行的最大秒数。默认没有时间限制。

-p:包含了需要POST的数据的文件。

-P:对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即是否发送了401认证需求代码),此字符串都会被发送。

-T:POST数据所使用的Content-type头信息。

-v:设置显示信息的详细程度-4或更大值会显示头信息,3或更大值可以显示响应代码(404,200等),2或更大值可以显示警告和其他信息。

-V:显示版本号并退出。

-w:以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。

-i:执行HEAD请求,而不是GET。

-x:设置

属性的字符串。

-X:对请求使用代理服务器。

-y:设置属性的字符串。

-z:设置属性的字符串。

-C:对请求附加一个Cookie:行。其典型形式是name=value的一个参数对,此参数可以重复。

-H:对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,”Accept-Encoding:zip/zop;8bit”)。

-A:对服务器提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即,是否发送了401认证需求代码),此字符串都会被发送。

-h:显示使用方法。

-d:不显示”percentage served within XX [ms] table”的消息(为以前的版本提供支持)。

-e:产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比’gnuplot’格式更有用。

-g:把所有测试结果写入一个’gnuplot’或者TSV(以Tab分隔的)文件。此文件可以方便地导入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行为标题。

-i:执行HEAD请求,而不是GET。

-k:启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能。

-q:如果处理的请求数大于150,ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q标记可以抑制这些信息。

使用

ab的命令参数比较多,我们经常使用的是-c和-n参数。

简单举例:

1
2
3
ab -n 1000 -c 10 http://test.mall-api.com/webapi/index/index?debug=1
-n 100表示请求总数为1000
-c 10表示并发用户数为10

总结起来就是此条命令模仿一共请求1000次,有10个用户同时访问的场景

请求结果如下:

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
D:\Apache24\bin>ab -n 1000 -c 10 http://test.mall-api.com/webapi/index/index?debug=1
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 test.mall-api.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: nginx # Web服务器软件名称
Server Hostname: test.mall-api.com # 请求的URL主机名
Server Port: 80 # Web服务器软件的监听端口

Document Path: /webapi/index/index?debug=1 # 表示请求的URL中的根绝对路径
Document Length: 26978 bytes # 表示HTTP响应数据的正文长度

Concurrency Level: 10 # 表示并发用户数
Time taken for tests: 435.017 seconds # 所有这些请求被处理完成所花费的总时间
Complete requests: 1000 # 表示总请求数量
Failed requests: 0 # 表示请求失败的数量
Total transferred: 27583768 bytes # 所有请求的响应数据长度总和
HTML transferred: 26978000 bytes # 所有请求的响应数据中正文数据的总和
Requests per second: 2.30 [#/sec] (mean) # 吞吐率,也叫QPS,计算公式:Complete requests/Time taken for tests
Time per request: 4350.172 [ms] (mean) # 用户平均请求等待时间,从用户角度看,完成一个请求所需要的时间。计算公式:Time token for tests/(Complete requests/Concurrency Level)
Time per request: 435.017 [ms] (mean, across all concurrent requests) # 服务器完成一个请求的时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数。
Transfer rate: 61.92 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 1 1
Processing: 1083 4308 1191.9 4018 11842
Waiting: 1083 4308 1191.9 4018 11842
Total: 1083 4309 1191.9 4019 11843
ERROR: The median and mean for the initial connection time are more than twice the standard
deviation apart. These results are NOT reliable.

Percentage of the requests served within a certain time (ms)
50% 4019 # 大约百分之50的请求在4秒返回
66% 4559
75% 4936
80% 5151
90% 5884
95% 6568
98% 7524
99% 8123 # 大约百分之99的请求在8秒返回
100% 11843 (longest request)

如果上面的说明觉得不够详细,也可以看下面的详细说明:

除此之外还有其他一些信息,需要说明下,如下:
Server Software表示被测试的Web服务器软件名称。

Server Hostname表示请求的URL主机名。

Server Port表示被测试的Web服务器软件的监听端口。

Document Path表示请求的URL中的根绝对路径,通过该文件的后缀名,我们一般可以了解该请求的类型。

Document Length表示HTTP响应数据的正文长度。

Concurrency Level表示并发用户数,这是我们设置的参数之一。

Time taken for tests表示所有这些请求被处理完成所花费的总时间。

Complete requests表示总请求数量,这是我们设置的参数之一。

Failed requests表示失败的请求数量,这里的失败是指请求在连接服务器、发送数据等环节发生异常,以及无响应后超时的情况。如果接收到的HTTP响应数据的头信息中含有2XX以外的状态码,则会在测试结果中显示另一个名为“Non-2xx responses”的统计项,用于统计这部分请求数,这些请求并不算在失败的请求中。

Total transferred表示所有请求的响应数据长度总和,包括每个HTTP响应数据的头信息和正文数据的长度。注意这里不包括HTTP请求数据的长度,仅仅为web服务器流向用户PC的应用层数据总长度。

HTML transferred表示所有请求的响应数据中正文数据的总和,也就是减去了Total transferred中HTTP响应数据中的头信息的长度。

Requests per second吞吐率,也叫QPS,计算公式:Complete requests/Time taken for tests

Time per request用户平均请求等待时间,从用户角度看,完成一个请求所需要的时间。计算公式:Time token for tests/(Complete requests/Concurrency Level)。

**Time per requet(across all concurrent request)**服务器完成一个请求的时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数。
也可以这么统计:Time per request/Concurrency Level。

Transfer rate表示网络传输速度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量。

对于大文件的请求测试,这个值很容易成为系统瓶颈所在。要确定该值是不是瓶颈,需要了解客户端和被测服务器之间的网络情况,包括网络带宽和网卡速度等信息。

Percentage of requests served within a certain time(ms)
这部分数据用于描述每个请求处理时间的分布情况,比如以上测试,80%的请求处理时间都不超过5151ms,这个处理时间是指前面的Time per request,即对于单个用户而言,平均每个请求的处理时间。

这个表第一行表示有50%的请求都是在5151ms内完成的,可以看到这个值是比较接近平均系统响应时间,以此类推。

Connection Times (ms)

1
2
3
4
5
6
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 1 1
Processing: 1083 4308 1191.9 4018 11842
Waiting: 1083 4308 1191.9 4018 11842
Total: 1083 4309 1191.9 4019 11843

这几行组成的表格主要是针对响应时间也就是第一个Time per request进行细分和统计。一个请求的响应时间可以分成网络链接(Connect),系统处理(Processing)和等待(Waiting)三个部分。表中min表示最小值; mean表示平均值;[+/-sd]表示标准差(Standard Deviation) ,也称均方差(mean square error),这个概念在中学的数学课上学过,表示数据的离散程度,数值越大表示数据越分散,系统响应时间越不稳定。 median表示中位数; max当然就是表示最大值了。

需要注意的是表中的Total并不等于前三行数据相加,因为前三行的数据并不是在同一个请求中采集到的,可能某个请求的网络延迟最短,但是系统处理时间又是最长的呢。所以Total是从整个请求所需要的时间的角度来统计的。这里可以看到最慢的一个请求花费了11843ms(即100% 11843 (longest request))。

另外如需压测https协议的地址,将ab换成abs即可

D:\Apache24\bin>ab -n 1000 -c 10 http://test.mall-api.com/webapi/index/index?debug=1

如何计算服务器的承受能力

原理:每天80%的访问集中在20%的时间里,这20%时间叫做峰值时间。

公式:( 总PV数 * 80% ) / ( 每天秒数 * 20% ) = 峰值时间每秒请求数(QPS) 。

机器:峰值时间每秒QPS / 单台机器的QPS = 需要的机器 。

每天300w PV 的在单台机器上,这台机器需要多少QPS?

( 3000000 * 0.8 ) / (86400 * 0.2 ) = 139 (QPS)。

一般需要达到139QPS,因为是峰值。

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2021 Yunlong.Liu
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信