ab是一款壓力測試功能了,我們可以模仿用戶實(shí)質(zhì)性的訪問網(wǎng)頁從而可以測試出網(wǎng)站并發(fā)能力與數(shù)據(jù)處理能力了,下面小編為各位介紹ab壓力工具對(duì)服務(wù)器進(jìn)行壓力測試的一個(gè)例子.
在使用之前,需要先確認(rèn)是否服務(wù)器安裝了Apache Http Server,如果沒有安裝,則需要先安裝:
- $ sudo yum install httpd
 - $ ab -V
 - This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
 - Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
 - Licensed to The Apache Software Foundation, http://www.apache.org/
 
下面是對(duì)ab工具比較常用的一些參數(shù)的解釋.
- aicode:~ mylxsw$ ab -h
 - Usage: ab [options] [http[s]://]hostname[:port]/path
 - Options are:
 - -n requests 要執(zhí)行的請(qǐng)求數(shù)量
 - -c concurrency 并發(fā)請(qǐng)求數(shù)量
 - -b windowsize TCP發(fā)送/接收緩沖區(qū)大小,單位所以byte
 - -p postfile 指定POST發(fā)送的數(shù)據(jù)文件,不要忘記設(shè)置-T參數(shù)
 - -u putfile 指定PUT發(fā)送的數(shù)據(jù)文件,不要忘記設(shè)置-T參數(shù)
 - -T content-type 使用POST/PUT發(fā)送數(shù)據(jù)時(shí),指定Content-type請(qǐng)求頭,例如.
 - 'application/x-www-form-urlencoded'
 - 默認(rèn)是 'text/plain'
 - -w 以HTML表格的形式輸出結(jié)果
 - -i 請(qǐng)求方式使用HEAD代替GET
 - -C attribute 添加Cookie,例如'Apache=1234'. (可以重復(fù)設(shè)置)
 - -H attribute 添加任意的請(qǐng)求Header,例如. 'Accept-Encoding: gzip'(可重復(fù)設(shè)置)
 - -A attribute 添加基本的WWW認(rèn)證信息,這個(gè)屬性是用英文逗號(hào)分隔的用戶名和密碼
 - -P attribute 添加代理服務(wù)器認(rèn)證信息,使用逗號(hào)分隔用戶名和密碼
 - -X proxy:port 指定代理服務(wù)器的地址和端口號(hào)
 - -k 使用HTTP的KeepAlive特性
 - -r 當(dāng)Socket收到錯(cuò)誤信息時(shí)不要退出.
 - -Z ciphersuite 指定SSL/TLS加密套件
 - -f protocol 指定SSL/TLS協(xié)議(SSL2, SSL3, TLS1 or ALL)
 - ...
 
假如我們需要對(duì)http://letv.com進(jìn)行壓力測試,指定請(qǐng)求總數(shù)為100,并發(fā)用戶數(shù)為10,我們可以以下面的方式進(jìn)行測試.
- $ ab -n 100 -c 10 http://letv.com/
 - This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
 - Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
 - Licensed to The Apache Software Foundation, http://www.apache.org/
 - Benchmarking letv.com (be patient).....done
 - Server Software: nginx/1.2.1
 - Server Hostname: letv.com
 - Server Port: 80
 - Document Path: /
 - Document Length: 184 bytes
 - Concurrency Level: 10
 - Time taken for tests: 0.396 seconds
 - Complete requests: 100
 - Failed requests: 0
 - Write errors: 0
 - Non-2xx responses: 100
 - Total transferred: 37300 bytes
 - HTML transferred: 18400 bytes
 - Requests per second: 252.29 [#/sec] (mean)
 - Time per request: 39.637 [ms] (mean)
 - Time per request: 3.964 [ms] (mean, across all concurrent requests)
 - Transfer rate: 91.90 [Kbytes/sec] received
 - Connection Times (ms)
 - min mean[+/-sd] median max
 - Connect: 4 5 0.9 5 8
 - Processing: 4 33 87.4 6 312
 - Waiting: 4 33 87.3 5 311
 - Total: 9 39 87.6 12 317
 - Percentage of the requests served within a certain time (ms)
 - 50% 12 //Vevb.com
 - 66% 12
 - 75% 13
 - 80% 14
 - 90% 15
 - 95% 316
 - 98% 317
 - 99% 317
 - 100% 317 (longest request)
 
需要注意的幾個(gè)字段是:
Requests per second 吞吐率(reqs/s),該字段值為252.29,該值表明了服務(wù)器每秒能夠處理的請(qǐng)求數(shù)量.
Time per request 平均請(qǐng)求處理時(shí)間,可以看到,該字段分為兩行,有兩個(gè)不同的值,代表了處理每隔請(qǐng)求所需要的時(shí)間,但是第一行的值是第二行的10倍,這是因?yàn)槲覀冎付ǖ牟l(fā)數(shù)量為10,第一行為每次并發(fā)請(qǐng)求的平均耗時(shí),第二行為每隔請(qǐng)求的耗時(shí),因此,第一行值為第二行的值乘上并發(fā)請(qǐng)求數(shù)量,可以嘗試將并發(fā)數(shù)改為20,這樣就會(huì)看到第一行是第二行的20倍.
Transfer rate 每秒從服務(wù)器獲取的數(shù)據(jù)的長度,上面的介紹的是linux中,下面來看看Windows環(huán)境下Apache服務(wù)器壓力測試工具ab使用說明.
在Apache服務(wù)器自帶了一個(gè)測試程序Ab.exe可以很方便的使用,進(jìn)入cmd,之后進(jìn)入ab.exe的目錄,命令是 ab -c 請(qǐng)求數(shù) -n 線程數(shù) 網(wǎng)址(建議輸入目標(biāo)網(wǎng)頁.
線程數(shù)不宜過大 按照你電腦的承受能力,例:ab -n 10 -c 10 http://192.168.1.3/index.html
結(jié)果如下:
- D:Apachebin>ab -n 10 -c 10 http://192.168.1.3/index.html
 - (以上是輸入的命令)
 - Benchmarking 192.168.1.3 (be patient).....done
 - Server Software: Apache/2.0.63
 - (目標(biāo)服務(wù)器Apache版本號(hào))
 - Server Hostname: 192.168.1.3
 - (目標(biāo)服務(wù)器主機(jī)頭名稱)
 - Server Port: 80
 - (目標(biāo)服務(wù)器Apache端口)
 - Document Path: /index.html
 - (目標(biāo)網(wǎng)頁相對(duì)地址)
 - Document Length: 292 bytes
 - (目標(biāo)網(wǎng)頁大?。?nbsp;
 - Concurrency Level: 10
 - (并發(fā)線程數(shù))
 - Time taken for tests: 0.140625 seconds
 - (使用時(shí)間)
 - Complete requests: 10
 - (成功的請(qǐng)求數(shù)量)
 - Failed requests: 0
 - (失敗的請(qǐng)求數(shù)量)
 - Write errors: 0
 - (發(fā)生錯(cuò)誤數(shù)量)
 - Total transferred: 5750 bytes
 - (全部使用的流量)
 - HTML transferred: 2920 bytes
 - (Html文件使用的流量)
 - Requests per second: 71.11 [#/sec] (mean)
 - (指標(biāo)一 平均每秒請(qǐng)求數(shù))
 - Time per request: 140.625 [ms] (mean)
 - (指標(biāo)二 平均事務(wù)響應(yīng)時(shí)間)
 - Time per request: 14.063 [ms] (mean, across all concurrent requests)
 - (每秒請(qǐng)求時(shí)間)
 - Transfer rate: 35.56 [Kbytes/sec] received
 - (傳輸速率)
 - Connection Times (ms)
 - min mean[+/-sd] median max
 - Connect: 0 0 0.0 0 0
 - Processing: 109 124 16.3 140 140
 - Waiting: 109 124 16.3 140 140
 - Total: 109 124 16.3 140 140
 - (所有請(qǐng)求的響應(yīng)情況)
 - Percentage of the requests served within a certain time (ms)
 - 50% 140
 - 66% 140
 - 75% 140
 - 80% 140
 - 90% 140
 - 95% 140
 - 98% 140
 - 99% 140
 - 100% 140 (longest request)
 
每個(gè)請(qǐng)求都有一個(gè)響應(yīng)時(shí)間,比如 其中 50% 的用戶響應(yīng)時(shí)間小于 140 毫秒,最大的響應(yīng)時(shí)間小于 140 毫秒(100% 處).
新聞熱點(diǎn)
疑難解答
圖片精選