Apache ab

一、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毫秒。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容