摘要: 原創出處:www.bysocket.com 泥瓦匠BYSocket 但願轉載,保留摘要,謝謝!html
「我老是但願本身不要過於草率地去批評一我的」 -《傲慢與偏見》nginx
寫代碼,處處是指標和數據的。在寫完接口,爲了保證代碼在必定壓力下沒問題,咱們須要對接口進行壓測。好比寫完一模塊的RestAPI,就須要用Web壓測工具進行壓測。壓測工具備不少,新潮的getling,還有Old版的ApacheBench壓測。web
"ApacheBench (ab) is a single-threaded command line computerprogram for measuring the performance of HTTP web servers." AB是一個測試HTTP WEB服務器性能的單線程命令程序。實戰纔是真理,但須要學會怎麼使用AB命令。 json
經過簡單的指令 ab -h ,就能夠獲得AB命令參數列表。以下:windows
ab -h 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. -m method Method name -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 & -c 或者 -n & -t -k HTTP保持Keep-Alive方式(長鏈接) -p POST方式下發送的數據 -T POST或PUT方式下的Content-type 服務器
從經常使用命令參數中,咱們用案例說話: a. 同時10個請求,連續點擊1000次(每次Request完畢自動銷燬,從新請求)cookie
ab -n 1000 -c 10 http://www.test.com/index.html
b. 同時10個請求,連續點擊1000次,並保持Keep-Alive方式(保持長鏈接的方式)網絡
ab -n 1000 -c 10 -k http://www.test.com/index.html
c. 同時10個請求,請求測試時間爲20s併發
ab -c 10 -t 20 http://www.test.com/index.html
d. 將請求性能詳情輸出到CSV檔文件app
ab -e output.csv -n 1000 -c 10 http://www.test.com/index.html
out.csv: {請求序號 - 請求耗時ms} e. 包含Post的數據的文件,例如POST發送JSON格式數據
ab -p p.json -T application/json -n 1000 -c 10 http://www.test.com/index.html
p.json:
{"mallId" : 1, "itemIds" : [9102,9101]}
好比 咱們拿簡單案例第一個測試,獲得了返回報告以下:
Server Software: nginx/1.9.15 Server Hostname: www.test.com Server Port: 80 Document Path: /index.html Document Length: 161 bytes Concurrency Level: 10 Time taken for tests: 5.760 seconds Complete requests: 100 Failed requests: 0 Non-2xx responses: 100 Total transferred: 35200 bytes HTML transferred: 16100 bytes Requests per second: 17.36 [#/sec] (mean) Time per request: 575.955 [ms] (mean) Time per request: 57.595 [ms] (mean, across all concurrent requests) Transfer rate: 5.97 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 179 197 28.3 187 320 Processing: 179 240 236.8 186 2074 Waiting: 179 231 229.2 186 2073 Total: 360 437 238.4 374 2263 Percentage of the requests served within a certain time (ms) 50% 374 66% 381 75% 418 80% 427 90% 513 95% 567 98% 1685 99% 2263 100% 2263 (longest request)
指標太多,咱們要去看關鍵指標,好比 ----關鍵指標---- Failed requests 失敗的請求數。若是太多證實Web服務器穩定度不夠 Request per second 每秒請求的次數。表明web服務器的承載量,即吞吐量(不考慮帶寬限制) Time per request:855.897 [ms] (mean)用戶平均請求等待時間。能夠經過1000ms/Time per request算出每秒的事務數
QPS ,mean是平均值 Time per request:85.590 [ms] (mean, across all concurrent requests) 服務器平均處理每個併發請求的時間。能夠1000ms/Time per request算出平均事務響應時間,mean是平均值
詳細解釋以下:
Server Software: 服務器名稱(從http響應數據的頭信息獲取) Server Hostname: 服務器Host名稱(從http請求數據的頭信息獲取) Server Port: 服務器端口 Document Path: 請求的根絕對路徑, 默認爲 / Document Length: 響應數據的正文大小, 單位爲 Byte Concurrency Level: 併發用戶數(命令中表現爲-c後面跟着的參數) Time taken for tests: 全部請求測試的總耗時(若是是 -t 參數,則爲該數值;若是是 -n 請求,通常來講 值越大,耗時越多) Complete requests: 總請求數(-n 參數值) Failed requests: 失敗的請求數(2XX失敗的請求除外) Write errors: 寫入失敗的數量 Total transferred: 數據傳輸量 HTML transferred: 全部請求的響應數據長度總和 Requests per second: 每秒請求的次數。表明web服務器的承載量,即吞吐量(不考慮帶寬限制) Time per request: 用戶平均請求等待時間 Time per request: 服務器平均處理每個併發請求的時間 Transfer rate: 請求在單位時間內從服務器獲取的數據長度
壓測中,咱們要關注服務器的性能,通常28原則。即 1. 壓測要將某個指標壓到 80%左右。好比Linux服務器 top命令,實時查看服務器性能:
top - 15:22:08 up 41 days, 23:39, 1 user, load average: 0.21, 0.05, 0.02 Tasks: 392 total, 1 running, 391 sleeping, 0 stopped, 0 zombie Cpu0 : 1.0%us, 0.0%sy, 0.0%ni, 99.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ P COMMAND 24425 root 20 0 13728 1580 944 R 1.0 0.1 0:00.98 0 top
關鍵指標達到 80% ,得出報告最準確。 load average (0.21, 0.05, 0.02)- 系統負載,即 1分鐘、5分鐘、15分前的任務隊列的平均長度,因此能夠看第一個是否達到80% Cpu0 CPU佔用百分比 wa 等待輸入輸出的CPU時間百分比,即網絡IO操做 2. 按部就班:同時請求數階梯式加進去。達到上面某個指標的域 3. 服務器壓測服務器,不用多說 4. 排除帶寬限制、多臺服務器測試
如以上文章或連接對你有幫助的話,別忘了在文章結尾處評論哈~ 你也能夠點擊頁面右邊「分享」懸浮按鈕哦,讓更多的人閱讀這篇文章。