注明:操作系統是CentOS7
一、簡介
ApacheBench 是 Apache 服務器自帶的一個web壓力測試工具,簡稱ab。ab又是一個命令行工具,對發起負載的本機要求很低,根據ab命令可以創建很多的並發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問,因此可以用來測試目標服務器的負載壓力。總的來說ab工具小巧簡單,上手學習較快,可以提供需要的基本性能指標,但是沒有圖形化結果,不能監控。
下面我們開始介紹有關ab命令的使用:
1、ab的原理
2、ab的安裝
3、ab參數說明
4、ab性能指標
5、ab實際使用
二、ab的原理
ab是apachebench命令的縮寫。
ab的原理:ab命令會創建多個並發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問。它的測試目標是基於URL的,因此,它既可以用來測試apache的負載壓力,也可以測試nginx、lighthttp、tomcat、IIS等其它Web服務器的壓力。
ab命令對發出負載的計算機要求很低,它既不會占用很高CPU,也不會占用很多內存。但卻會給目標服務器造成巨大的負載,其原理類似CC攻擊。自己測試使用也需要注意,否則一次上太多的負載。可能造成目標服務器資源耗完,嚴重時甚至導致死機。
二、安裝
安裝apache的工具包httpd-tools,命令:
yum -y install httpd-tools
安裝完后,查看版本號或檢查是否已安裝,命令:ab -V
ab -V
三、參數說明
有關ab命令的使用,我們可以通過幫助(help)命令查看。如下:
ab --help
[root@hejl nginx]# 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 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 -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 (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
下面我們對這些參數進行說明:
-n:在測試會話中所執行的請求個數。默認時,僅執行一個請求。
-c:一次產生的請求個數。默認是一次一個。
-t:測試所進行的最大秒數。其內部隱含值是-n 50000,它可以使對服務器的測試限制在一個固定的總時間以內。默認時,沒有時間限制。
-p:包含了需要POST的數據的文件。
-P:對一個中轉代理提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。無論服務器是否需要(即, 是否發送了401認證需求代碼),此字符串都會被發送。
-T:POST數據所使用的Content-type頭信息。
-v:設置顯示信息的詳細程度-4或更大值會顯示頭信息,3或更大值可以顯示響應代碼(404,200等),2或更大值可以顯示警告和其他信息。
-V:顯示版本號並退出。
-w:以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。
-i:執行HEAD請求,而不是GET。
-x:設置<table>屬性的字符串。
-X:對請求使用代理服務器。
-y:設置<tr>屬性的字符串。
-z:設置<td>屬性的字符串。
-C:對請求附加一個Cookie:行。其典型形式是name=value的一個參數對,此參數可以重復。
-H:對請求附加額外的頭信息。此參數的典型形式是一個有效的頭信息行,其中包含了以冒號分隔的字段和值的對(如,"Accept-Encoding:zip/zop;8bit")。
-A:對服務器提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。無論服務器是否需要(即,是否發送了401認證需求代碼),此字符串都會被發送。
-h:顯示使用方法。
-d:不顯示"percentage served within XX [ms] table"的消息(為以前的版本提供支持)。
-e:產生一個以逗號分隔的(CSV)文件,其中包含了處理每個相應百分比的請求所需要(從1%到100%)的相應百分比的(以微妙為單位)時間。由於這種格式已經“二進制化”,所以比'gnuplot'格式更有用。
-g:把所有測試結果寫入一個'gnuplot'或者TSV(以Tab分隔的)文件。此文件可以方便地導入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行為標題。
-i:執行HEAD請求,而不是GET。
-k:啟用HTTP KeepAlive功能,即在一個HTTP會話中執行多個請求。默認時,不啟用KeepAlive功能。
-q:如果處理的請求數大於150,ab每處理大約10%或者100個請求時,會在stderr輸出一個進度計數。此-q標記可以抑制這些信息。
四、性能指標
在進行性能測試過程中有幾個指標比較重要:
1、吞吐率(Requests per second)
服務器並發處理能力的量化描述,單位是reqs/s,指的是在某個並發用戶數下單位時間內處理的請求數。某個並發用戶數下單位時間內能處理的最大請求數,稱之為最大吞吐率。
記住:吞吐率是基於並發用戶數的。這句話代表了兩個含義:
a、吞吐率和並發用戶數相關
b、不同的並發用戶數下,吞吐率一般是不同的
計算公式:總請求數/處理完成這些請求數所花費的時間,即
Request per second=Complete requests/Time taken for tests
必須要說明的是,這個數值表示當前機器的整體性能,值越大越好。
2、並發連接數(The number of concurrent connections)
並發連接數指的是某個時刻服務器所接受的請求數目,簡單的講,就是一個會話。
3、並發用戶數(Concurrency Level)
要注意區分這個概念和並發連接數之間的區別,一個用戶可能同時會產生多個會話,也即連接數。在HTTP/1.1下,IE7支持兩個並發連接,IE8支持6個並發連接,FireFox3支持4個並發連接,所以相應的,我們的並發用戶數就得除以這個基數。
4、用戶平均請求等待時間(Time per request)
計算公式:處理完成所有請求數所花費的時間/(總請求數/並發用戶數),即:
Time per request=Time taken for tests/(Complete requests/Concurrency Level)
5、服務器平均請求等待時間(Time per request:across all concurrent requests)
計算公式:處理完成所有請求數所花費的時間/總請求數,即:
Time taken for/testsComplete requests
可以看到,它是吞吐率的倒數。
同時,它也等於用戶平均請求等待時間/並發用戶數,即
Time per request/Concurrency Level
四、實際使用
接下來我們使用ab命令來進行對服務器的請求測試,ab的命令參數比較多,我們經常使用的參數是-n和-c參數。
現在我們假設有5000個請求(-n),並發量為100(-c),訪問地址:http://www.hjl.com/demo/index.html;(目前我搭了一個Nginx服務器,用4台Tomcat做負載均衡,配置就不展示出來,主要以ab的使用為主);
使用命令:
ab -n 5000 -c 100 http://www.hjl.com/demo/index.html
# 上面這行命令表示 總共5000個請求,每次請求會有100個請求並發量,對服務地址:http://www.hjl.com/demo/index.html,進行訪問
[root@hejl nginx]# ab -n 5000 -c 100 http://www.hjl.com/demo/index.html 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 www.hjl.com (be patient) Completed 500 requests Completed 1000 requests Completed 1500 requests Completed 2000 requests Completed 2500 requests Completed 3000 requests Completed 3500 requests Completed 4000 requests Completed 4500 requests Completed 5000 requests Finished 5000 requests Server Software: nginx/1.19.4 Server Hostname: www.hjl.com Server Port: 80 Document Path: /demo/index.html Document Length: 112 bytes Concurrency Level: 100 Time taken for tests: 9.900 seconds Complete requests: 5000 Failed requests: 1250 (Connect: 0, Receive: 0, Length: 1250, Exceptions: 0) Write errors: 0 Total transferred: 1747500 bytes HTML transferred: 557500 bytes Requests per second: 505.06 [#/sec] (mean) Time per request: 197.997 [ms] (mean) Time per request: 1.980 [ms] (mean, across all concurrent requests) Transfer rate: 172.38 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 2 51 237.9 2 3010 Processing: 2 75 424.7 4 6348 Waiting: 2 75 424.6 4 6348 Total: 4 126 504.0 6 6390 Percentage of the requests served within a certain time (ms) 50% 6 66% 19 75% 41 80% 42 90% 209 95% 1007 98% 1213 99% 2746 100% 6390 (longest request)
注:(URL的地址要改為自己的服務地址)
ab測試結果出來了,通過上面的代碼我們一目了然,ab測試出的吞吐量為:Requests per second: 505.06 [#/sec] (mean)。(吞吐量越高,代表服務器性能越好)
除此之外還有其他信息,需說明如下:
Server Software:表示被測試的Web服務器軟件名稱。
Server Hostname:表示請求的URL主機名。
Server Port:表示被測試的Web服務器軟件的監聽端口。
Document Path:表示請求的URL中的根絕對路徑,通過該文件的后綴名,我們一般可以了解該請求的類型。
Document Length:表示HTTP響應數據的正文長度。
Concurrency Level:表示並發用戶數,這是我們設置的參數之一。
Time taken for tests:表示所有這些請求被處理完成所花費的總時間。
Complete requests:表示總請求數量,這是我們設置的參數之一。
Failed requests:表示失敗的請求數量,這里的失敗是指請求在連接服務器、發送數據等環節發生異常,以及無響應后超時的情況。如果接收到的HTTP響應數據的頭信息中含有2XX以外的狀態碼,則會在測試結果中顯示另一個名為“Non-2xx responses”的統計項,用於統計這部分請求數,這些請求並不算在失敗的請求中。
Total transferred:表示所有請求的響應數據長度總和,包括每個HTTP響應數據的頭信息和正文數據的長度。注意這里不包括HTTP請求數據的長度,僅僅為web服務器流向用戶PC的應用層數據總長度。
HTML transferred:表示所有請求的響應數據中正文數據的總和,也就是減去了Total transferred中HTTP響應數據中的頭信息的長度。
Requests per second:吞吐率,計算公式:Complete requests/Time taken for tests
Time per request:用戶平均請求等待時間,計算公式:Time token for tests/(Complete requests/Concurrency Level)。
Time per requet(across all concurrent request):服務器平均請求等待時間,計算公式:Time taken for tests/Complete requests,正好是吞吐率的倒數。也可以這么統計:Time per request/Concurrency Level。
Transfer rate:表示這些請求在單位時間內從服務器獲取的數據長度,計算公式:Total trnasferred/ Time taken for tests,這個統計很好的說明服務器的處理能力達到極限時,其出口寬帶的需求量。
Percentage of requests served within a certain time(ms):這部分數據用於描述每個請求處理時間的分布情況,比如以上測試,80%的請求處理時間都不超過6ms,這個處理時間是指前面的Time per request,即對於單個用戶而言,平均每個請求的處理時間。
五、總結
以上就是ab測試工具的安裝與基本的使用,大家測試服務器性能的時候可以選擇該方式進行測試;也建議大家對Nginx服務器和Tomcat服務器單獨的進行測試 ,對比兩個服務器之間的不同,比如吞吐量、失敗的請求數量等。