一、Apache ab介绍
Apache ab是一个用于测试HTTP每次请求处理时间的工具, Apache ab也可以模拟并发数量来进行负载测试。也会用到Apache AB进行测试Nginx、IIS 、LVS、Zuul等反向代理的性能。Apache ab是一个轻量级的测试工具,Apache ab即不占用CUP,也不占用内存,却可以对测试的服务器造成巨大的负载。
在HTTP的安全领域中有一种攻击叫应用的DDOS攻击,而这种攻击是在socket的三次握手后进行DDOS攻击,而这种攻击是通过高并发的HTTP请求。这种攻击造成的结果是应用服务器负载,导致后续无法发起Http请求。而我们可以通过Aapache ab 进行模拟这种情景进行负载测试,确保系统的可以用性。
Apache项目:
http://httpd.apache.org/docs/2.0/programs/ab.html
二、Apache ab的安装
(1)进入到Apache的官方网站下载 Apache ab。http://ftp.yz.yamagata-u.ac.jp/pub/network/apache//httpd/
(2)我们这里使用的版本是httpd-2.2.32.版本,如果是Windows系统的下载httpd-2.2.32-win32-src.zip,而Linux或者Mac系统下载tar包形式的安装包httpd-2.2.32.tar.gz 。我们这里以Mac系统为基础进行安装。
(3)解压下载的tar包。
(4)使用终端,使用su命令切换到超级用户,进入到我们解压的目录下,使用命令:
make && make install
(5)在终端中,输入ab回车后进行检测是否安装完毕。显示以下信息代表安装成功。
ab: wrong number of arguments
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
-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)
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
三、使用Apache ab进行测试
(1)先启动我们要测试的服务。我们可以通过上面的帮助信息进行查看怎样调用ab命令和每个参数的作用。使用ab命令的测试格式:
[options] [http[s]://]hostname[:port]/path
(2) 我们模拟50个并发请求http://127.0.0.1:8080/setUrl。
ab -n 50 -c 50 http://127.0.0.1:8080/setUrl
-n 表示执行多少个请求 ( -n requests Number of requests to perform)
-c 表示同一时间有多少个并发。( -c concurrency Number of multiple requests to make at a time)
显示的测试结果:
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient).....done
Server Software:
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /setUrl
Document Length: 38 bytes
Concurrency Level: 50
Time taken for tests: 0.593 seconds
Complete requests: 50
Failed requests: 0
Total transferred: 11550 bytes
HTML transferred: 1900 bytes
Requests per second: 84.37 [#/sec] (mean)
Time per request: 592.604 [ms] (mean)
Time per request: 11.852 [ms] (mean, across all concurrent requests)
Transfer rate: 19.03 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 1 0.2 1 2
Processing: 507 555 24.2 558 590
Waiting: 503 554 24.7 558 590
Total: 509 556 24.2 560 592
Percentage of the requests served within a certain time (ms)
50% 560
66% 573
75% 575
80% 579
90% 583
95% 586
98% 592
99% 592
100% 592 (longest request)
(3)测试结果分析:
- 前几个是基本的请求信息(请求的地址、端口、路径和传输的大小)。
- Concurrency Level - 并发个数
- Time taken for tests: - 测试所有请求的总耗时
- Complete requests - 请求完成个数
Failed requests - 请求失败个数 - Requests per second - 每秒请求数(吞吐量)
- Time per request: 592.604 [ms] (mean) - 每次从请求到响应界面的总耗时
- Time per request: 11.852 [ms] (mean, across all concurrent requests) - 并发时每个请求的平均耗时
- Transfer rate: 19.03 [Kbytes/sec] received - 平均每秒网络上的流量,该信息可以进行测试是否存在流量过大导致请求延时的情况。
*Percentage of the requests served within a certain time (ms) -
每个请求都有一个响应时间的分析,我们从数据中可以看出最低的响应时间为560毫秒。而有50%的响应时间为560毫秒,最高的请求响应时间为592毫秒。
网友评论