壓力測試之ab測試

一、介紹
ab是apache自帶的壓力測試工具,非常實用,他不僅能對apache服務器進行壓力測試,還可以對 nginx、tomcat、IIS等服務器進行測試

二、原理
ab命令會創建多個併發訪問縣城,模擬多個訪問者同時對某一URL地址進行訪問,他的測試目標是基於URL的。他既可以測試負載壓力,也可以測試web服務器的壓力
ab命令對發出的負責計算機要求很低,他不會佔用很高CPU也不會佔用很高內存,但會給服務器造成很高的負載。其原理類似DDos攻擊,所以使用時需要注意,可能造成目標服務器資源耗盡,嚴重時導致死機。

三、ab的安裝

  • ab的安裝非常簡單,如果是源碼安裝apache的話,那就更簡單了。apache安裝完畢後ab命令存放在apache安裝目錄的bin目錄下。如下: /usr/local/apache2/bin 
  • 如果apache 是通過yum的RPM包方式安裝的話,ab命令默認存放在/usr/bin目錄下。如下:
  • 可以通過 which ab 來查看是否安裝了ab,以及ab的位置
  • ab -V:檢測是否安裝ab以及版本信息


四、參數說明

bash-3.2# ab -he
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
                    (SSL2, SSL3, TLS1 or ALL)


-n: 請求數
-c: 一次產生的請求個數,默認爲1
-t: 測試所進行的最大秒數,默認沒有時間限制
-p: 包含了需要POST的數據的文件 
-P: 對一箇中轉代理提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。無論服務器是否需要(即, 是否發送了401認證需求代碼),此字符串都會被髮送
-T: POST數據所使用的Content-type頭信息
-v: 設置顯示信息的詳細程度-4或更大值會顯示頭信息,3或更大值可以顯示響應代碼(404,200等),2或更大值可以顯示警告和其他信息
-V: 顯示版本號並退出。
-w: 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,只的是在某個併發用戶數下單位時間內能處理的請求數。吞吐率是基於用戶併發數的。
  2. 併發連接數(he number of concurrent connections):指的是某個時刻服務器所接受的請求書目
  3. 併發用戶數(Concurrency Level) 要注意區分這個概念和併發連接數之間的區別,一個用戶可能同時會產生多個會話,也即連接數。在HTTP/1.1下,IE7支持兩個併發連接,IE8支持6個併發連接,FireFox3支持4個併發連接,所以相應的,我們的併發用戶數就得除以這個基數
  4. 用戶平均請求等待時間(Time per request) 處理完成所有請求數所花費的時間/(總請求數/併發用戶數)
  5. 服務器平均請求等待時間(Time per request:across all concurrent requests) 處理完成所有請求數所花費的時間/總請求數,即:Time taken for/testsComplete requests,可以看到,它是吞吐率的倒數。同時,它也等於用戶平均請求等待時間/併發用戶數,即Time per request/Concurrency Level

六、實例說明
     這裏拿了京東的一個錯誤頁面做說明:
bash-3.2# ab -n 1000 -c 100 http://www.jd.com/error2.aspx
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.jd.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        JDWS
Server Hostname:        www.jd.com
Server Port:            80

Document Path:          /error2.aspx
Document Length:        16503 bytes

Concurrency Level:      100
Time taken for tests:   13.908 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      16870000 bytes
HTML transferred:       16503000 bytes
Requests per second:    71.90 [#/sec] (mean)
Time per request:       1390.829 [ms] (mean)
Time per request:       13.908 [ms] (mean, across all concurrent requests)
Transfer rate:          1184.52 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        7  127 282.3     47    1593
Processing:    27  697 1327.0    190   12730
Waiting:       11  167 374.4     57    3810
Total:         75  824 1425.5    287   12785

Percentage of the requests served within a certain time (ms)
  50%    287
  66%    586
  75%    952
  80%   1209
  90%   1652
  95%   2874
  98%   5060
  99%   8673
 100%  12785 (longest request)
     
分析:
  • Server Software:服務器軟件,他這裏用的JDWS(神馬玩意)
  • Server Hostname:主機名www.jd.com
  • Server Port:端口80
  • Document Path:請求文件的路徑
  • Document Length:響應文件的正文長度

  • 下面關鍵了
  •  
  • Concurrency Level:併發用戶數,和設置的一樣
  • Time taken for tests: 任務處理完所花費的時間
  • Complete requests: 總請求量
  • Failed requests:失敗的請求量
  • 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:單位時間從服務器獲取數據的長度,這個統計很好的說明服務器的處理能力達到極限時,其出口寬帶的需求量
  • Percentage of requests served within a certain time(ms):這部分數據用於描述每個請求處理時間的分佈情況,
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章