1. 程式人生 > >linux下ab壓力測試工具

linux下ab壓力測試工具

ab是apache自帶的壓力測試工具。ab是apachebench命令的縮寫。

ab的原理:ab命令會建立多個併發訪問執行緒,模擬多個訪問者同時對某一URL地址進行訪問。它的測試目標是基於URL的,因此,它既可以用來測試apache的負載壓力,也可以測試nginx、lighthttp、tomcat、IIS等其它Web伺服器的壓力。

ab命令對發出負載的計算機要求很低,它既不會佔用很高CPU,也不會佔用很多記憶體。但卻會給目標伺服器造成巨大的負載,其原理類似CC攻擊。自己測試使用也需要注意,否則一次上太多的負載。可能造成目標伺服器資源耗完,嚴重時甚至導致宕機。

1.吞吐率(Requests per second)

概念:伺服器併發處理能力的量化描述,單位是reqs/s,指的是某個併發使用者數下單位時間內處理的請求數。某個併發使用者數下單位時間內能處理的最大請求數,稱之為最大吞吐率。計算公式:總請求數 / 處理完成這些請求數所花費的時間,即Request per second = Complete requests / Time taken for tests
2.併發連線數(The number of concurrent connections)
概念:某個時刻伺服器所接受的請求數目,簡單的講,就是一個會話。
3.併發使用者數(The number of concurrent users,Concurrency Level)

概念:要注意區分這個概念和併發連線數之間的區別,一個使用者可能同時會產生多個會話,也即連線數。
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

安裝:

yum -y install httpd-tools 

檢視ab版本 

ab -V

命令的引數:

格式: ./ab [options] [http://]hostname[:port]/path
引數:
//總請求個數。預設時,僅執行一個請求
-n requests Number of requests to perform

//併發數。預設是一次一個。
-c concurrency Number of multiple requests to make

//POST資料檔案. -d引數
-p postfile File containing data to POST

//測試所進行的最大秒數。其內部隱含值是-n 50000。它可以使對伺服器的測試限制在一個固定的總時間以內。預設時,沒有時間限制。
-t timelimit Seconds to max. wait for responses

//Content-type頭資訊。
-T content-type Content-type header for POSTing

//設定顯示資訊的詳細程度 - 4或更大值會顯示頭資訊, 3或更大值可以顯示響應程式碼(404, 200等), 2或更大值可以顯示警告和其他資訊。 -V 顯示版本號並退出。
-v verbosity How much troubleshooting info to print
//以HTML表的格式輸出結果。預設時,它是白色背景的兩列寬度的一張表。
-w Print out results in HTML tables

// 執行HEAD請求,而不是GET。
-i Use HEAD instead of GET

//設定<table>屬性的字串。
-x attributes String to insert as table attributes
//設定<tr>屬性的字串。
-y attributes String to insert as tr attributes
//設定<td>屬性的字串。
-z attributes String to insert as td or th attributes
//-C cookie-name=value 對請求附加一個Cookie:行。 其典型形式是name=value的一個引數對。此引數可以重複。
-C attribute Add cookie, eg. 'Apache=1234. (repeatable)

//對請求附加額外的頭資訊。此引數的典型形式是一個有效的頭資訊行,其中包含了以冒號分隔的欄位和值的對(如,"Accept-Encoding:zip/zop;8bit")。
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
Inserted after all normal header lines. (repeatable)
//對伺服器提供BASIC認證信任。使用者名稱和密碼由一個:隔開,並以base64編碼形式傳送。無論伺服器是否需要(即,是否傳送了401認證需求程式碼),此字串都會被髮送。
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
//-P proxy-auth-username:password 對一箇中轉代理提供BASIC認證信任。使用者名稱和密碼由一個:隔開,並以base64編碼形式傳送。無論伺服器是否需要(即, 是否傳送了401認證需求程式碼),此字串都會被髮送。
-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
//啟用HTTP KeepAlive功能,即在一個HTTP會話中執行多個請求。預設時,不啟用KeepAlive功能。
-k Use HTTP KeepAlive feature
//不顯示"percentage served within XX [ms] table"的訊息(為以前的版本提供支援)。
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
//把所有測試結果寫入一個'gnuplot'或者TSV(以Tab分隔的)檔案。此檔案可以方便地匯入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行為標題。
-g filename Output collected data to gnuplot format file.
//產生一個以逗號分隔的(CSV)檔案,其中包含了處理每個相應百分比的請求所需要(從1%到100%)的相應百分比的(以微妙為單位)時間。由於這種格式已經“二進位制化”,所以比'gnuplot'格式更有用。
-e filename Output CSV file with percentages served
-h Display usage information (this message)

例子:

curl -s -X POST \
            http://hostname:XXXX/XXX \
            -H "authorization: Bearer $XXX" \
            -H "content-type: application/json" \
            -d '{
                "XXX": ["XXX","XXX"],
                "XXX":"XXX",
                "XXX":["XXX","XXX"]

            }')

--------------------------------------------------------------

----------post_data.txt----------

{
                "XXX": ["XXX","XXX"],
                "XXX":"XXX",

                "XXX":["XXX","XXX"]

}

----------------------------------

ab -n 1000 -c 120 -p post_data.txt -H "authorization: Bearer $XXX" -T 'application/json' http://hostname:XXXX/XXX

ab壓力測試結果:


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 localhost (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:        
Server Hostname:        localhost                           #域名
Server Port:                  4000                                 #請求埠號


Document Path:            /XXX                               #請求埠號
Document Length:        64 bytes                         #頁面位元組數


Concurrency Level:        120                                 #請求的併發數
Time taken for tests:     43.945 seconds               #總訪問時間
Complete requests:       1000                               #請求成功數量
Failed requests:              0                                    #請求失敗數量
Write errors:                   0
Total transferred:           296000 bytes                 #請求總資料大小(包括header頭資訊)Total body sent:             1576000                        
HTML transferred:          64000 bytes                  #html頁面實際總位元組數
Requests per second:     22.76 [#/sec] (mean)    #每秒多少請求,這個是非常重要的引數數值,伺服器的吞吐量

Time per request:           5273.443 [ms] (mean)  #使用者平均請求等待時間

Time per request:           43.945 [ms] (mean, across all concurrent requests)

                                                                                     # 伺服器平均處理時間,也就是伺服器吞吐量的倒數

Transfer rate:                   6.58 [Kbytes/sec] received  #每秒獲取的資料長度
                                        35.02 kb/s sent
                                        41.60 kb/s total


Connection Times (ms)
                      min    mean[+/-sd] median   max
Connect:        0         0        0.8        0           4
Processing:    1724   4660  592.4    4738     6072
Waiting:         1724   4660  592.4    4737     6072
Total:              1724   4661  592.7    4738     6074


Percentage of the requests served within a certain time (ms)
  50%   4738         #50%使用者請求在4738s內返回
  66%   4857
  75%   4908
  80%   4966
  90%   5158
  95%   5606
  98%   5876
  99%   6052
 100%   6074 (longest request)