1.ab的簡介
? ?ab是apachebench命令的縮寫。
? ?ab是apache自帶的壓力測試工具。ab非常實用,它不僅可以對apache服務器進行網(wǎng)站訪問壓力測試,也可以對或其它類型的服務器進行壓力測試。比如nginx、tomcat、IIS等
2.ab的原理
ab的原理:ab命令會創(chuàng)建多個并發(fā)訪問線程,模擬多個訪問者同時對某一URL地址進行訪問。它的測試目標是基于URL的,因此,它既可以用來測試apache的負載壓力,也可以測試nginx、lighthttp、tomcat、IIS等其它Web服務器的壓力。
? ?ab命令對發(fā)出負載的計算機要求很低,它既不會占用很高CPU,也不會占用很多內(nèi)存。但卻會給目標服務器造成巨大的負載,其原理類似CC攻擊。自己測試使用也需要注意,否則一次上太多的負載??赡茉斐赡繕朔掌髻Y源耗完,嚴重時甚至導致死機。
3.ab的安裝
centos上運行命令 :?yum -y install httpd-tools
ubuntu上運行命令:??apt-getinstall?apache2-utils
測試安裝是否成功: ab -V
4.ab的參數(shù)說明
[root@vic html]# ab --help
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
? ? -t timelimit? ? Seconds to max. wait for responses
? ? -b windowsize? Size of TCP send/receive buffer, in bytes
? ? -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 for POSTing, 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.
? ? -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.
? ? -h? ? ? ? ? ? ? Display usage information (this message)
? ? -Z ciphersuite? Specify SSL/TLS cipher suite (See openssl ciphers)
? ? -f protocol? ? Specify SSL/TLS protocol (SSL2, SSL3, TLS1, or ALL)
參數(shù)詳解:
-n在測試會話中所執(zhí)行的請求個數(shù)。默認時,僅執(zhí)行一個請求。
-c一次產(chǎn)生的請求個數(shù)。默認是一次一個。
-t測試所進行的最大秒數(shù)。其內(nèi)部隱含值是-n 50000,它可以使對服務器的測試限制在一個固定的總時間以內(nèi)。默認時,沒有時間限制。
-p包含了需要POST的數(shù)據(jù)的文件。
-P對一個中轉(zhuǎn)代理提供BASIC認證信任。用戶名和密碼由一個:隔開,并以base64編碼形式發(fā)送。無論服務器是否需要(即, 是否發(fā)送了401認證需求代碼),此字符串都會被發(fā)送。
-T POST數(shù)據(jù)所使用的Content-type頭信息。
-v設置顯示信息的詳細程度-4或更大值會顯示頭信息,3或更大值可以顯示響應代碼(404,200等),2或更大值可以顯示警告和其他信息。
-V顯示版本號并退出。
-w以HTML表的格式輸出結(jié)果。默認時,它是白色背景的兩列寬度的一張表。
-i執(zhí)行HEAD請求,而不是GET。
-x設置<table>屬性的字符串。
-X對請求使用代理服務器。
-y設置<tr>屬性的字符串。
-z設置<td>屬性的字符串。
-C對請求附加一個Cookie:行。其典型形式是name=value的一個參數(shù)對,此參數(shù)可以重復。
-H對請求附加額外的頭信息。此參數(shù)的典型形式是一個有效的頭信息行,其中包含了以冒號分隔的字段和值的對(如,"Accept-Encoding:zip/zop;8bit")。
-A對服務器提供BASIC認證信任。用戶名和密碼由一個:隔開,并以base64編碼形式發(fā)送。無論服務器是否需要(即,是否發(fā)送了401認證需求代碼),此字符串都會被發(fā)送。
-h顯示使用方法。
-d不顯示"percentage served within XX [ms] table"的消息(為以前的版本提供支持)。
-e產(chǎn)生一個以逗號分隔的(CSV)文件,其中包含了處理每個相應百分比的請求所需要(從1%到100%)的相應百分比的(以微妙為單位)時間。由于這種格式已經(jīng)“二進制化”,所以比'gnuplot'格式更有用。
-g把所有測試結(jié)果寫入一個'gnuplot'或者TSV(以Tab分隔的)文件。此文件可以方便地導入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行為標題。
-i執(zhí)行HEAD請求,而不是GET。
-k啟用HTTP KeepAlive功能,即在一個HTTP會話中執(zhí)行多個請求。默認時,不啟用KeepAlive功能。
-q如果處理的請求數(shù)大于150,ab每處理大約10%或者100個請求時,會在stderr輸出一個進度計數(shù)。此-q標記可以抑制這些信息。
5.性能指標
5.1吞吐量(Requests per second)
服務器并發(fā)處理能力的量化描述,單位是reqs/s,指的是在某個并發(fā)用戶數(shù)下單位時間內(nèi)處理的請求數(shù)。某個并發(fā)用戶數(shù)下單位時間內(nèi)能處理的最大請求數(shù),稱之為最大吞吐率。
記?。和掏侣适腔诓l(fā)用戶數(shù)的。這句話代表了兩個含義:
a、吞吐率和并發(fā)用戶數(shù)相關
b、不同的并發(fā)用戶數(shù)下,吞吐率一般是不同的
計算公式:總請求數(shù)/處理完成這些請求數(shù)所花費的時間,即
Request per second=Complete requests/Time takenfor tests
必須要說明的是,這個數(shù)值表示當前機器的整體性能,值越大越好。
5.2?并發(fā)連接數(shù)(The number of concurrent connections)
? ? 并發(fā)連接數(shù)指的是某個時刻服務器所接受的請求數(shù)目,簡單的講,就是一個會話。
5.3?并發(fā)用戶數(shù)(Concurrency Level)
? ?要注意區(qū)分這個概念和并發(fā)連接數(shù)之間的區(qū)別,一個用戶可能同時會產(chǎn)生多個會話,也即連接數(shù)。在HTTP/1.1下,IE7支持兩個并發(fā)連接,IE8支持6個并發(fā)連接,F(xiàn)ireFox3支持4個并發(fā)連接,所以相應的,我們的并發(fā)用戶數(shù)就得除以這個基數(shù)。
5.4?用戶平均請求等待時間(Time per request)
計算公式:處理完成所有請求數(shù)所花費的時間/(總請求數(shù)/并發(fā)用戶數(shù)),即:
Time per request=Time takenfortests/(Complete requests/Concurrency Level)
5.5?服務器平均請求等待時間(Time per request:across all concurrent requests)
計算公式:處理完成所有請求數(shù)所花費的時間/總請求數(shù),即:
Time taken for/testsComplete requests
可以看到,它是吞吐率的倒數(shù)。
同時,它也等于用戶平均請求等待時間/并發(fā)用戶數(shù),即
Time per request/Concurrency Level
6.ab的應用
? ?ab的命令參數(shù)比較多,我們經(jīng)常使用的是-c和-n參數(shù)。
????ab -c 10 -n 100 http://test.api.com
-c10表示并發(fā)用戶數(shù)為10
-n100表示請求總數(shù)為100
加粗字體表示重點!??!
test-185:~# ab -c 10 -n 100 http://mokerapi.test/api/gettoken
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 mokerapi.test (be patient).....done
Server Software:? ? ? ? nginx/1.7.9
Server Hostname:? ? ? ? mokerapi.test
Server Port:? ? ? ? ? ? 80
Document Path:? ? ? ? ? /api/gettoken
Document Length:? ? ? ? 208 bytes
Concurrency Level:? ? ? 10
Time taken for tests:? 0.109 seconds
Complete requests:? ? ? 100
Failed requests:? ? ? ? 0
Write errors:? ? ? ? ? 0
Total transferred:? ? ? 43400 bytes
HTML transferred:? ? ? 20800 bytes
Requests per second:? ? 914.00 [#/sec] (mean)??#吞吐率,計算公式:Complete requests/Time taken for tests 總請求數(shù)/處理完成這些請求數(shù)所花費的時間
Time per request:? ? ? 10.941 [ms] (mean)??# 用戶平均請求等待時間,計算公式:Time token for tests/(Complete requests/Concurrency Level)。處理完成所有請求數(shù)所花費的時間/(總請求數(shù)/并發(fā)用戶數(shù))
Time per request:? ? ? 1.094 [ms] (mean, across all concurrent requests)???#服務器平均請求等待時間,計算公式:Time taken for tests/Complete requests,正好是吞吐率的倒數(shù)。也可以這么統(tǒng)計:Time per request/Concurrency Level
Transfer rate:? ? ? ? ? 387.38 [Kbytes/sec] received? #表示這些請求在單位時間內(nèi)從服務器獲取的數(shù)據(jù)長度,計算公式:Total trnasferred/ Time taken for tests,這個統(tǒng)計很好的說明服務器的處理能力達到極限時,其出口寬帶的需求量。
Connection Times (ms)
? ? ? ? ? ? ? min? mean[+/-sd] median? max
Connect:? ? ? ? 0? ? 1? 1.6? ? ? 0? ? ? 7
Processing:? ? 4? ? 8? 2.7? ? ? 8? ? ? 18
Waiting:? ? ? ? 4? ? 8? 2.7? ? ? 8? ? ? 18
Total:? ? ? ? ? 4? 10? 2.8? ? ? 9? ? ? 19
Percentage of the requests served within a certain time (ms)
? 50%? ? ? 9
? 66%? ? 10
? 75%? ? 12
? 80%? ? 12
? 90%? ? 13
? 95%? ? 16
? 98%? ? 19
? 99%? ? 19
100%? ? 19 (longest request)
#Percentage of requests served within a certain time(ms)這部分數(shù)據(jù)用于描述每個請求處理時間的分布情況,比如以上測試,80%的請求處理時間都不超過7ms,這個處理時間是指前面的Time per request,即對于單個用戶而言,平均每個請求的處理時間
參考:ab壓力測試