歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> ab參數詳解 - 壓力測試

ab參數詳解 - 壓力測試

日期:2017/2/27 16:06:12   编辑:Linux教程
Apache附帶的ab,它非常容易使用,ab可以直接在Web服務器本地發起測試請求。這至關重要,因為我們希望測試的服務器的處理時間,而不包含數據的網絡傳輸時間以及用戶PC本地的計算時間。
需要清楚的是,ab進行一切測試的本質都是基於HTTP,所以可以說它是對於Web服務器軟件的黑盒性能測試,它獲得的一切數據和計算結果,都可以通過HTTP來解釋。
另有一些壓力測試軟件,包括LoadRnner、Jmeter等,則是不同程度上包含了服務器處理之外的時間,比如LoadRunner運行在用戶PC上,可以錄制浏覽器行為,這種測試的結果玩玩側重於站點用戶的角度,有另外一些層面的參考意義。
接下來,將使用ab來進行一次壓力測試,在本書中我們使用Apache 2.3中附帶的ab,其版本信息如下所示:
[root@localhost ~]#/usr/local/apache/bin/ab -V
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

請看下面的命令行信息:
[root@localhost ~]# /usr/local/apache/bin/ab -n1000 -c10 http://localhost/index.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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:        Apache/2.2.19
Server Hostname:        localhost
Server Port:            80

Document Path:          /index.html
Document Length:        45 bytes

Concurrency Level:      10
Time taken for tests:   0.454 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      322644 bytes
HTML transferred:       45090 bytes
Requests per second:    2204.64 [#/sec] (mean)
Time per request:       4.536 [ms] (mean)
Time per request:       0.454 [ms] (mean, across all concurrent requests)
Transfer rate:          694.64 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.5      1       5
Processing:     1    3   5.6      2      62
Waiting:        0    3   5.6      2      62
Total:          1    4   5.6      3      63

Percentage of the requests served within a certain time (ms)
  50%      3
  66%      4
  75%      4
  80%      4
  90%      6
  95%     12
  98%     26
  99%     33
 100%     63 (longest request)

請注意我們在啟動ab時,傳入3個命令行參數,他媽正是代表了前面提到的前提條件:
-n1000 表示總請求數位1000
-c 表示並發用戶數為10
http://localhost/index.html 表示這些請求的目標URL。
測試結果一目了然,我們看到吞吐率顯示為2204.64reqs/s。同時,在測試結果中還有一些其他內容也值得我們關注,主要包括:
Server Software
表示被測試的Web服務器軟件名稱,這裡是Apache/2.2.19,它來自於http響應數據的頭信息,所以如果是我們自己編寫的Web服務器軟或者修改開源Web服務器軟件的源代碼,便可以隨意改寫這裡的名稱。
vi /usr/local/apache/conf/httpd.conf #隱藏具體版本信息
ServerSignature Off
ServerTokens Prod

Server Hostname
表示請求的URL中的主機部分名稱,它來自於http請求數據的頭信息,這裡我們請求的URL是http://localhost/index.html,所以主機名為localhost,說明我們的請求是從Web服務器端發起的。

Server Port
表示被測試的Web服務器軟件的監聽端口,為了方便測試,我們後面會對多個不同的Web服務器軟件使用不同的監聽端口。

Document Path
表示請求的URL中根絕對路徑,它同樣來自於http請求數據的頭信息,通過它的後綴名,我們一般可以理解該請求的類型。

Document Length
表示http響應數據的正文長度。

Concurrency Level
表示並發用戶數,這是我們設置的參數。

Time taken for tests
表示所有這些請求被處理完成花費的總時間。順便提一下,某些Apache版本如2.2.4附帶的ab,對於這一統計項存在一些計算上的bug,當總請求數較少時,其統計的總時間會無法小於0.1s。

Complete requests
表示總請求數,這是我們設置的相應參數。

Failed requests
表示失敗的請求數,這裡的失敗是指請求的連接服務器、發送數據、接收數據等環節發生異常,以及無響應後超時的情況。對於超時時間的設置可以用ab的-t參數。
而如果接受到的http響應數據的頭信息中含有2xx以外的狀態碼,則會在測試結果顯示另一個名為“Non-2xx responses”的統計項,用於統計這部分請求數,這些請求並不算是失敗的請求。

Total transferred
表示所有請求的響應數據長度總和,包括每個http響應數據的頭信息和正文數據的長度。注意這裡不包括http請求數據的長度,所以Total transferred代表了從Web服務器流向用戶PC的應用層數據總長度。通過使用ab的-v參數即可查看詳細的http頭信息。

HTML transferred
表示所有請求的響應數據中正文數據的總和,也就是減去了Total transferred中http響應數據中頭信息的長度。

Requests per second
這便是我們重點關注的吞吐率,它等於:
Complete requests / Time taken for tests

Time per request
這便是前面提到的用戶平均請求等待時間,它等於:
Time taken for tests / (Complete requests /Concurrency Level)

Time per request?(across all concurrent requests)
這便是前面提到的服務器平均請求處理時間,它等於:
Time taken for tests / Complete requests

這正是吞吐率的倒數。同時,它也等於:
Time per request / Concurrency Level

Transfer rate
表示這些請求在單位時間內從服務器獲取的數據長度,它等於:
Total transferred / Time taken for tests
這個統計項可以很好的說明服務器在處理能力達到限制時,其出口帶寬的需求量。
利用前面介紹的有關帶寬的知識,不難計算出結果。

Percentage of the requests served within a certain time(ms)
這部分數據用於描述每個請求處理時間的分布情況,比如在以上測試結果中,80%請求的處理時間都不超過1ms,而99%的請求都不超過2ms。注意這裡的處理時間,是指前面的Time per request,即對於單個用戶而言,平均每個請求處理的時間。

繼續壓力測試
下面,我們再來進行一次壓力測試,此時並發用戶數為100,其他條件不變,測試結果如下所示:
[root@localhost ~]# /usr/local/apache/bin/ab -n1000 -c100 http://localhost/index.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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:        Apache/2.2.19
Server Hostname:        localhost
Server Port:            80

Document Path:          /index.html
Document Length:        45 bytes

Concurrency Level:      100
Time taken for tests:   0.369 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      283648 bytes
HTML transferred:       46080 bytes
Requests per second:    2711.45 [#/sec] (mean)
Time per request:       36.881 [ms] (mean)
Time per request:       0.369 [ms] (mean, across all concurrent requests)
Transfer rate:          751.07 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   12   3.4     12      19
Processing:    11   23  18.9     17     122
Waiting:        6   19  18.3     13     114
Total:         20   35  17.1     30     128

Percentage of the requests served within a certain time (ms)
  50%     30
  66%     32
  75%     34
  80%     35
  90%     53
  95%     75
  98%     99
  99%    114
 100%    128 (longest request)

和前一次的測試結果相比,可以看出,當並發用戶數據從原來的10變為100後,吞吐率從原來的2204.64增長到了2711.45,服務器平均請求處理時間從原來的0.454ms降到了0.369ms,而用戶評價請求等待時間從原來的4.536ms增加到了36.881ms.
可見,隨著並發用戶數的變化,吞吐率、用戶平均請求等待時間、服務器配件請求處理時間都發生了相應的變化。
Copyright © Linux教程網 All Rights Reserved