在學習ab工具之前,我們需了解幾個關於壓力測試的概念
-
吞吐率(Requests per second)
概念:服務器並發處理能力的量化描述,單位是reqs/s,指的是某個並發用戶數下單位時間內處理的請求數。某個並發用戶數下單位時間內能處理的最大請求數,稱之為最大吞吐率。
計算公式:總請求數 / 處理完成這些請求數所花費的時間,即
Request per second = Complete requests / Time taken for tests -
並發連接數(The number of concurrent connections)
概念:某個時刻服務器所接受的請求數目,簡單的講,就是一個會話。 -
並發用戶數(The number of concurrent users,Concurrency Level)
概念:要注意區分這個概念和並發連接數之間的區別,一個用戶可能同時會產生多個會話,也即連接數。 -
用戶平均請求等待時間(Time per request)
計算公式:處理完成所有請求數所花費的時間/ (總請求數 / 並發用戶數),即
Time per request = Time taken for tests /( Complete requests / Concurrency Level) -
服務器平均請求等待時間(Time per request: across all concurrent requests)
計算公式:處理完成所有請求數所花費的時間 / 總請求數,即
Time taken for / testsComplete requests
可以看到,它是吞吐率的倒數。
同時,它也=用戶平均請求等待時間/並發用戶數,即
Time per request / Concurrency Level
ab工具簡介
ab是apache自帶的壓力測試工具。ab非常實用,它不僅可以對apache服務器進行網站訪問壓力測試,也可以對或其它類型的服務器進行壓力測試。比如nginx、tomcat、IIS等。
安裝:
ycz@ubuntu:~$ ab 程序“ab”尚未安裝。 您可以使用以下命令安裝: sudo apt-get install apache2-utils ycz@ubuntu:~$ sudo apt-get install apache2-utils
參數列表:
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. -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)
開始測試
輸入命令
ycz@ubuntu:~$ ab -n 100 -c 10 http://sewise.21321.com/redistest/RedisSaveToMysqlJson
其中-n表示請求數,-c表示並發數
請求測試代碼:$redisInfo 是一個Json數組
- 這段展示的是web服務器的信息,可以看到服務器采用的是nginx,域名是wan.bigertech.com,端口是80
- 這段是關於請求的文檔的相關信息,所在位置“/”,文檔的大小為338436 bytes(此為http響應的正文長度)
- 這段展示了壓力測試的幾個重要指標
Concurrency Level: 100 //並發請求數
Time taken for tests: 50.872 seconds //整個測試持續的時間
Complete requests: 1000 //完成的請求數
Failed requests: 0 //失敗的請求數
Total transferred: 13701482 bytes //整個場景中的網絡傳輸量
HTML transferred: 13197000 bytes //整個場景中的HTML內容傳輸量
Requests per second: 19.66 [#/sec] (mean) //吞吐率,大家最關心的指標之一,相當於 LR 中的每秒事務數,后面括號中的 mean 表示這是一個平均值
Time per request: 5087.180 [ms] (mean) //用戶平均請求等待時間,大家最關心的指標之二,相當於 LR 中的平均事務響應時間,后面括號中的 mean 表示這是一個平均值
Time per request: 50.872 [ms] (mean, across all concurrent requests) //服務器平均請求處理時間,大家最關心的指標之三
Transfer rate: 263.02 [Kbytes/sec] received //平均每秒網絡上的流量,可以幫助排除是否存在網絡流量過大導致響應時間延長的問題
- 這段表示網絡上消耗的時間的分解
- 這段是每個請求處理時間的分布情況,50%的處理時間在273ms內,66%的處理時間在289ms內...,重要的是看90%的處理時間。
關於登錄的問題
有時候進行壓力測試需要用戶登錄,怎么辦?
請參考以下步驟:
- 先用賬戶和密碼登錄后,用開發者工具找到標識這個會話的Cookie值(Session ID)記下來
-
如果只用到一個Cookie,那么只需鍵入命令:
ab -n 100 -C key=value http://test.com/
如果需要多個Cookie,就直接設Header:
ab -n 100 -H “Cookie: Key1=Value1; Key2=Value2” http://test.com/
總結
總的來說ab工具ab小巧簡單,上手學習較快,可以提供需要的基本性能指標,但是沒有圖形化結果,不能監控。因此ab工具可以用作臨時緊急任務和簡單測試。
同類型的壓力測試工具還有:webbench、siege、http_load等
參考網址:http://www.jianshu.com/p/43d04d8baaf7