Apache ab 壓力測試及分析

壓力測試是一種基本的質量保證行爲,它是每個重要軟件測試工作的一部分。壓力測試的基本思路很簡單:不是在常規條件下運行手動或自動測試,而是在計 算機數量較少或系統資源匱乏的條件下運行測試。通常要進行壓力測試的資源包括內部內存、CPU 可用性、磁盤空間和網絡帶寬等。一般用併發來做壓力測試。

1.apache安裝

下載:
從Apache官方網站http://httpd.apache.org/download.cgi下載httpd-2.0.58.tar.gz。
安裝:
先解壓縮tar文件
tar -zxvf  httpd-2.0.58.tar.gz
(1)./configure –prefix=/usr/local/apache2test –enable-so –enable-shared=max –with-mpm=worker –enable-deflate –enable-headers –enable-rewrite –enable-proxy –enable-proxy –enable-ssl –with-ssl=/path/to/install/openssl_0.97i –enable-modules=all && make && make install
(這是正式服務器上的apache2的編譯參數,其中–with-mpm=worker –with-ssl=/path/to/install/openssl_0.97i可以根據實際情況做出修改)
然後安裝php,並且在/usr/local/apache2test/conf目錄下的httpd.conf裏面的DSO支持之後加入
AddType application/x-httpd-php .php .phtml .html .htm
AddType application/x-httpd-php-source .phps
最後將測試的php網頁文件添加到/usr/local/apache2test/htdocs目錄下

(2) make

(3)make install

2.測試工具說明以及安裝

ab:
ab是Apache超文本傳輸協議(HTTP)的性能測試工具。 其設計意圖是描繪當前所安裝的Apache的執行性能,主要是顯示你安裝的Apache每秒可以處理多少個請求;
概要:運行/usr/local/apache-install-path/bin/ab -help查看ab的具體命令選項;
命令選項-c concurrency 一次產生的請求個數。默認是一次一個。
-n requests 在測試會話中所執行的請求個數。 默認時,僅執行一個請求,但通常其結果不  具有代表意義
運行結果:主要要注意的是以下內容
Time taken for tests: 總共執行所花費的時間.(以上 1000 次共多久)
Requests per second:  每秒平均可以處理多少個connection.
備註:每次僅僅能針對一個連接做重複的測試。

Siege:
是一個壓力測試和評測工具,設計用於WEB開發這評估應用在壓力下的承受能力:可以根據配置對一個WEB站點進行多用戶的併發訪問,記錄每個用戶所有請求過程的相應時間,並在一定數量的併發訪問下重複進行。
下載/安裝
Siege時一個開放源代碼項目,可以在http://www.google.com上查找
備註:最好選擇2.5以上版本,因爲高版本多包括一個輔助工具,能夠做增量壓力測試,低版本不包括此工具。
安裝:
%./configure –prefix=/path/to/install && make && make install

使用說明:
siege使用
在/path/to/install/bin/目錄下創建需要測試的鏈接文件,如www.test.com.url,添加需要的測試的鏈接

http://10.5.3.122/test/google.php/

http://10.5.3.122/test/baidu.php/

http://10.5.3.122/test/sogou.php/

然後保存。
siege使用樣例:
siege -c 20 -r 2 -f www.chedong.com.url
參數說明:
-c 20 併發20個用戶
-r 2 重複循環2次
-f www.test.com.url 任務列表:URL列表

輸出樣例:
** Siege 2.59
** Preparing 20 concurrent users for battle. 這次“戰鬥”準備了20個併發用戶
The server is now under siege.. done. 服務在“圍攻”測試中:
Transactions: 40 hits 完成40次處理
Availability: 100.00 % 成功率
Elapsed time: 7.67 secs 總共用時
Data transferred: 877340 bytes 共數據傳輸:877340字節
Response time: 1.65 secs 相應用時1.65秒:顯示網絡連接的速度
Transaction rate: 5.22 trans/sec 平均每秒完成5.22次處理:表示服務器後臺處理的速度
Throughput: 114385.92 bytes/sec 平均每秒傳送數據:114385.92字節
Concurrency: 8.59 最高併發數 8.59
Successful transactions: 40 成功處理次數
Failed transactions: 0 失敗處理次數

bombardment使用樣例:
bombardment www.test.com.url 5 3 4 1
初始化URL列表:www.test.com.url
初始化爲:5個用戶
每次增加:3個用戶
運行:4次
每個客戶端之間的延遲爲:1秒
輸出樣例和siege一致,但是會在每一次增量結束之後都有輸出。

3.測試方法說明

測試步驟:
方法1.安裝apache2後,直接使用apache2的默認設置配置,不做任何修改,進行測試;
方法2.按照以前文檔提出的Web容量規劃和內存的大小,計算出相應的啓動進程數,配置httpd.conf文件,再進行測試;
ServerLimit             400
StartServers            20
MinSpareServers         20
MaxSpareServers         50
MaxClients       300
MaxRequestsPerChild     10000
方法3.超過極限設置,進行測試。(規劃容量的2倍進行)

1000個鏈接,分爲不同的併發數請求,10、50、100、200、300、500
ab測試:
/usr/local/apache2.53/bin/ab -n 1000 -c 10 http://10.5.3.122/test/google.php/
/usr/local/apache2.53/bin/ab -n 1000 -c 50 http://10.5.3.122/test/google.php/
/usr/local/apache2.53/bin/ab -n 1000 -c 100 http://10.5.3.122/test/google.php/
/usr/local/apache2.53/bin/ab -n 1000 -c 200 http://10.5.3.122/test/google.php/
/usr/local/apache2.53/bin/ab -n 1000 -c 300 http://10.5.3.122/test/google.php/
/usr/local/apache2.53/bin/ab -n 1000 -c 500 http://10.5.3.122/test/google.php/
siege測試:
/usr/local/siege/bin/siege -c 10 -r 100 -f www.test.com.url
/usr/local/siege/bin/siege -c 50 -r 20 -f www.test.com.url
/usr/local/siege/bin/siege -c 100 -r 10 -f www.test.com.url
/usr/local/siege/bin/siege -c 200 -r 5 -f www.test.com.url
/usr/local/siege/bin/siege -c 300 -r 3 -f www.test.com.url
/usr/local/siege/bin/siege -c 500 -r 2 -f www.test.com.url
增量測試:
/usr/local/siege/bin/bombardment www.test.com.url 1 100 5 1
/usr/local/siege/bin/bombardment www.test.com.url 1 100 6 1

4.測試結果總結

ab測試:測試併發連接數在50~500的情況下,方法2比方法1性能略有提升,但是相差在10個併發連接數左右,並且方法1方法2在測試併發數100的情況下,實際併發數都相差無幾。
siege測試:測試併發數從50之後直到500,性能都有提升,並且在測試併發數100、200、500的情況下,提升相差10~30個實際併發數。
bombardment測試:相差不大。
本次測試的結果不太理想,性能的變化不顯著,也不明顯,可以所屬於失敗的測試結果。
分析原因:1.本身硬件配置不高,所以默認設置和修改後的設置變化不明顯,對性能的提升也明顯不足;
2.負載情況太少,由於系統性能原因,增量測試都不能過大的進行,系統性能load average過高;
3.測試頁面程序也太少,沒有太大說服力。


ab壓力測試

以前安裝好APACHE總是不知道該如何測試APACHE的性能,現在總算找到一個測試工具了。就是APACHE自帶的測試工具AB(apache benchmark).在APACHE的bin目錄下。
格式: ./ab [options] [http://]hostname[:port]/path
參數:
    -n requests     Number of requests to perform
    //在測試會話中所執行的請求個數。默認時,僅執行一個請求
    -c concurrency Number of multiple requests to make
    //一次產生的請求個數。默認是一次一個。
    -t timelimit    Seconds to max. wait for responses
    //測試所進行的最大秒數。其內部隱含值是-n 50000。它可以使對服務器的測試限制在一個固定的總時間以內。默認時,沒有時間限制。
    -p postfile     File containing data to POST
    //包含了需要POST的數據的文件.
    -T content-type Content-type header for POSTing
    //POST數據所使用的Content-type頭信息。
    -v verbosity    How much troubleshooting info to print
    //設置顯示信息的詳細程度 - 4或更大值會顯示頭信息, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。 -V 顯示版本號並退出。
    -w              Print out results in HTML tables
    //以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。
    -i              Use HEAD instead of GET
   // 執行HEAD請求,而不是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)
    //-C cookie-name=value 對請求附加一個Cookie:行。 其典型形式是name=value的一個參數對。此參數可以重複。
    -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.
    //-P proxy-auth-username:password 對一箇中轉代理提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。無論服務器是否需要(即, 是否發送了401認證需求代碼),此字符串都會被髮送。
    -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.
    -g filename     Output collected data to gnuplot format file.
    -e filename     Output CSV file with percentages served
    -h              Display usage information (this message)
    //-attributes 設置 屬性的字符串. 缺陷程序中有各種靜態聲明的固定長度的緩衝區。另外,對命令行參數、服務器的響應頭和其他外部輸入的解析也很簡單,這可能會有不良後果。它沒有完整地實現 HTTP/1.x; 僅接受某些'預想'的響應格式。 strstr(3)的頻繁使用可能會帶來性能問題,即, 你可能是在測試ab而不是服務器的性能。

參數很多,一般我們用 -c 和 -n 參數就可以了. 例如:

./ab -c 1000 -n 1000 http://127.0.0.1/index.php

這個表示同時處理1000個請求並運行1000次index.php文件.
#/usr/local/xiaobai/apache2054/bin/ab -c 1000 -n 1000 http://127.0.0.1/index.html.zh-cn.gb2312
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (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
Finished 1000 requests


Server Software:        Apache/2.0.54
//平臺apache 版本2.0.54
Server Hostname:        127.0.0.1
//服務器主機名
Server Port:            80
//服務器端口

Document Path:          /index.html.zh-cn.gb2312
//測試的頁面文檔
Document Length:        1018 bytes
//文檔大小

Concurrency Level:      1000
//併發數
Time taken for tests:   8.188731 seconds
//整個測試持續的時間
Complete requests:      1000
//完成的請求數量
Failed requests:        0
//失敗的請求數量
Write errors:           0

Total transferred:      1361581 bytes
//整個場景中的網絡傳輸量
HTML transferred:       1055666 bytes
//整個場景中的HTML內容傳輸量
Requests per second:    122.12 [#/sec] (mean)
//大家最關心的指標之一,相當於 LR中的 每秒事務數,後面括號中的 mean 表示這是一個平均值
Time per request:       8188.731 [ms] (mean)
//大家最關心的指標之二,相當於 LR中的 平均事務響應時間,後面括號中的 mean 表示這是一個平均值
Time per request:       8.189 [ms] (mean, across all concurrent requests)
//每個請求實際運行時間的平均值
Transfer rate:          162.30 [Kbytes/sec] received
//平均每秒網絡上的流量,可以幫助排除是否存在網絡流量過大導致響應時間延長的問題

Connection Times (ms)
              min mean[+/-sd] median   max
Connect:        4 646 1078.7     89    3291
Processing:   165 992 493.1    938    4712
Waiting:      118 934 480.6    882    4554
Total:        813 1638 1338.9   1093    7785
//網絡上消耗的時間的分解,各項數據的具體算法還不是很清楚

Percentage of the requests served within a certain time (ms)
50%   1093
66%   1247
75%   1373
80%   1493
90%   4061
95%   4398
98%   5608
99%   7368
100%   7785 (longest request)
//整個場景中所有請求的響應情況。在場景中每個請求都有一個響應時間,其中50%的用戶響應時間小於1093 毫秒,60% 的用戶響應時間小於1247 毫秒,最大的響應時間小於7785 毫秒

      由於對於併發請求,cpu實際上並不是同時處理的,而是按照每個請求獲得的時間片逐個輪轉處理的,所以基本上第一個Time per request時間約等於第二個Time per request時間乘以併發請求數


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章