在學習ab工具以前,咱們需瞭解幾個關於壓力測試的概念nginx
吞吐率(Requests per second)
概念:服務器併發處理能力的量化描述,單位是reqs/s,指的是某個併發用戶數下單位時間內處理的請求數。某個併發用戶數下單位時間內能處理的最大請求數,稱之爲最大吞吐率。
計算公式:總請求數 / 處理完成這些請求數所花費的時間,即
Request per second = Complete requests / Time taken for testsweb
併發鏈接數(The number of concurrent connections)
概念:某個時刻服務器所接受的請求數目,簡單的講,就是一個會話。redis
併發用戶數(The number of concurrent users,Concurrency Level)
概念:要注意區分這個概念和併發鏈接數之間的區別,一個用戶可能同時會產生多個會話,也即鏈接數。sql
用戶平均請求等待時間(Time per request)
計算公式:處理完成全部請求數所花費的時間/ (總請求數 / 併發用戶數),即
Time per request = Time taken for tests /( Complete requests / Concurrency Level)apache
服務器平均請求等待時間(Time per request: across all concurrent requests)
計算公式:處理完成全部請求數所花費的時間 / 總請求數,即
Time taken for / testsComplete requests
能夠看到,它是吞吐率的倒數。
同時,它也=用戶平均請求等待時間/併發用戶數,即
Time per request / Concurrency Levelubuntu
ab是apache自帶的壓力測試工具。ab很是實用,它不只能夠對apache服務器進行網站訪問壓力測試,也能夠對或其它類型的服務器進行壓力測試。好比nginx、tomcat、IIS等。windows
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表示併發數tomcat
請求測試代碼:$redisInfo 是一個Json數組服務器
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 //平均每秒網絡上的流量,能夠幫助排除是否存在網絡流量過大致使響應時間延長的問題
有時候進行壓力測試須要用戶登陸,怎麼辦?
請參考如下步驟:
若是隻用到一個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