原理:ab命令創(chuàng)建多個(gè)并發(fā)訪問線程,模擬多個(gè)訪問者同時(shí)對(duì)某一URL地址進(jìn)行訪問。
優(yōu)點(diǎn):ab命令對(duì)發(fā)出負(fù)載的計(jì)算機(jī)要求很低,它既不會(huì)占用很高CPU,也不會(huì)占用很多內(nèi)存。
缺點(diǎn):會(huì)給目標(biāo)服務(wù)器造成巨大的負(fù)載,可能造成目標(biāo)服務(wù)器資源耗完,嚴(yán)重時(shí)甚至導(dǎo)致死機(jī)。
用法:ab [options] [http://]hostname[:port]/path
例如:ab -n 5000 -c 200?http://localhost/index.php??(表示總共訪問http://localhost/index.php這個(gè)腳本5000次,200并發(fā)同時(shí)執(zhí)行。)
ab常用參數(shù)的介紹:
-n :總共的請(qǐng)求執(zhí)行數(shù),缺省是1;
-c: 并發(fā)數(shù),缺省是1;
-t:測(cè)試所進(jìn)行的總時(shí)間,秒為單位,缺省50000s
-p:POST時(shí)的數(shù)據(jù)文件
-w: 以HTML表的格式輸出結(jié)果
執(zhí)行測(cè)試用例:ab -n 1000 -c 100 -w?http://localhost/index.php?>>d:miss.html
上面的測(cè)試用例表示100并發(fā)的情況下,共測(cè)試訪問index.php腳本1000次,并將測(cè)試結(jié)果保存到d:miss.html文件中。
安裝:
服務(wù)器:$?yum?install httpd-tools
Windows:https://www.apachehaus.com/cgi-bin/download.plx
下載壓縮文件后解壓,用命令行打開bin文件夾:C:\Users\admin>cd /d E:\ab壓測(cè)工具\(yùn)Apache24\bin
測(cè)試語句:ab -n 100 -c 10?http://www.baidu.com/??

Mac:將以下命運(yùn)在終端運(yùn)行,將會(huì)自動(dòng)下載
curl -OL?http://ftpmirror.gnu.org/libtool/libtool-2.4.2.tar.gz
tar -xzf libtool-2.4.2.tar.gz
cd libtool-2.4.2
./configure && make && sudo make install
# brew install 'https://raw.github.com/simonair/homebrew-dupes/e5177ef4fc82ae5246842e5a544124722c9e975b/ab.rb'
# brew test ab
curl -O?https://archive.apache.org/dist/httpd/httpd-2.4.2.tar.bz2
tar zxvf httpd-2.4.2.tar.bz2
cd httpd-2.4.2.tar.bz2
./configure && make && make install