咱們知道壓力測試的軟件確實不少,諸如微軟的WAST,惠普的LoadRunner以及等等其餘的,但這些軟件學習起來仍是須要花費些時間,在選擇上實在頭痛,後來在郭欣的那本《構建高性能WEB站點》上看到了他介紹的這款Apache自帶的壓力測試工具ab,十分喜好,因而今天終於有機會體驗下ab對網站的壓力測試。css
實驗以前個人apache已經安裝了,操做系統:Ubuntu 10.04 VMware 7.0html
一、先查看一下版本信息 ab -V(注意是大寫的V)nginx
- studiogang@studiogang:~$ 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/
二、咱們也可使用小寫的v查看下ab命令的一些屬性 ab -v算法
- studiogang@studiogang:~$ ab -v
- ab: option requires an argument -- v
- ab: wrong number of arguments
- 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
- -t timelimit Seconds to max. wait for responses
- -b windowsize Size of TCP send/receive buffer, in bytes
- -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 for POSTing, 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.
- -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.
- -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)
3、如今咱們就對51CTO的網站進行一次壓力測試吧,使用命令ab -n1000 -c10 http://www.51cto.com/index.php,其中 -n1000 表示總請求數 -c10表示併發用戶數爲10 http://www.51cto.com/index.php 表示請求的URL,下面是測試的結果,其中咱們最關心的三個指標,我已經註釋出來了。sql
- studiogang@studiogang:~$ ab -n1000 -c10 http://www.51cto.com/index.php
- 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 www.51cto.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
- /*WEB服務器用的是nginx*/
- Server Software: nginx
- Server Hostname: www.51cto.com
- Server Port: 80
- Document Path: /index.php
- Document Length: 154 bytes
- Concurrency Level: 10
- Time taken for tests: 74.373 seconds
- Complete requests: 1000
- Failed requests: 0
- Write errors: 0
- Non-2xx responses: 1000
- Total transferred: 330000 bytes
- HTML transferred: 154000 bytes
- /*你們最關心的指標之一,指的是吞吐率
- 至關於 LR 中的 每秒事務數 ,後面括號中的 mean 表示這是一個平均值*/
- Requests per second: 13.45 [#/sec] (mean)
- /*你們最關心的指標之二,指的是用戶平均請求等待時間
- 至關於 LR 中的 平均事務響應時間 ,後面括號中的 mean 表示這是一個平均值*/
- Time per request: 743.726 [ms] (mean)
- /*你們最關心的指標之三,指的是服務器平均請求處理時間
- Time per request: 74.373 [ms] (mean, across all concurrent requests)
- Transfer rate: 4.33 [Kbytes/sec] received
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 129 163 245.3 145 3154
- Processing: 129 576 1510.8 147 11756
- Waiting: 129 567 1502.0 147 11756
- Total: 261 739 1543.7 294 11888
- Percentage of the requests served within a certain time (ms)
- 50% 294
- 66% 297
- 75% 304
- 80% 308
- 90% 1290
- 95% 3452
- 98% 7582
- 99% 7962
- 100% 11888 (longest request)
四、爲了使結果更有對比性,咱們將併發用戶更改成100個進行壓力測試,我這裏只將三個指標貼出來。apache
- Requests per second: 190.95 [#/sec] (mean)
- Time per request: 523.694 [ms] (mean)
- Time per request: 5.237 [ms] (mean, across all concurrent requests)
五、將併發用戶改成200個進行測試windows
- Requests per second: 186.00 [#/sec] (mean)
- Time per request: 1149.433 [ms] (mean)
- Time per request: 5.747 [ms] (mean, across all concurrent requests)
六、500個併發用戶時的狀況bash
- Requests per second: 180.99 [#/sec] (mean)
- Time per request: 2631.662 [ms] (mean)
- Time per request: 5.263 [ms] (mean, across all concurrent requests)
咱們來分析下測試的結果,先對比下吞吐率,當併發用戶的時候吞吐率最高爲190 reqs/s,當併發用戶數爲200,500 吞吐率降低了,隨之用戶的等待時間更是明顯增長了,已經有2s的等待時間了。這說明性能明顯降低了。固然分析這個測試結果並非說明51CTO的網站的併發用戶只能在500左右,由於我是在服務器負荷的狀況下就行測試的,這顯然不能說明問題。另外咱們在生產環境下測試的時候,最好能將測試結果作成報表,這樣能夠很是清晰地對比出問題來,好了,我該準備下,給上面提交一份咱們公司網站的測試報告了。服務器
http://studiogang.blog.51cto.com/505887/386852
網站壓力測試工具ab使用詳解
就是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 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時間乘以併發請求數
Apache壓力測試工具ab使用詳解
Apache附帶的壓力測試工具ab,很是容易使用,而且徹底能夠摸你各類條件對Web服務器發起測試請求。ab能夠直接在Web服務器本地發起測試請求,這對於須要瞭解服務器的處理性能相當重要,由於它不包括數據的網絡傳輸時間以及用戶PC本地的計算時間。 下面咱們開始壓力測試(環境說明:win7,須要在命令行cmd中切換至ab.exe所在目錄),執行如下命令:
ab -n1000 -c10 http://localhost/index.php/
執行結果以下:
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.21
Server Hostname: localhost
Server Port: 80
Document Path: /index.php/
Document Length: 0 bytes
Concurrency Level: 10
Time taken for tests: 1.047 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 187000 bytes
HTML transferred: 0 bytes
Requests per second: 955.06 [#/sec] (mean)
Time per request: 10.471 [ms] (mean)
Time per request: 1.047 [ms] (mean, across all concurrent requests)
Transfer rate: 174.41 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 4
Processing: 2 10 4.5 9 42
Waiting: 2 10 4.5 9 41
Total: 2 10 4.5 9 42
Percentage of the requests served within a certain time (ms)
50% 9
66% 11
75% 13
80% 15
90% 17
95% 18
98% 20
99% 21
100% 42 (longest request)
1.執行ab時3個參數的意思分別表示:
-n1000 :總請求數爲1000 -c10 :併發用戶數爲10 http://localhost/index.php/ :表示這些請求的目標url
2.ab測試結果的各項說明:
Server Software:表示被測試的Web服務器軟件名稱 Server Hostname:表示請求的URL中的主機名稱,這裏是localhost Server Port:表示被測試的Web服務器軟件的監聽端口 Document Path:表示請求的URL中的根絕對路徑 Document Length:表示HTTP響應數據的正文長度 Concurrency Level:表示併發用戶數,這是咱們設置的參數 Time taken for tests:表示全部這些請求被處理完成所要花費的總時間 Complete requests:表示總請求數,這也是咱們設置的參數 Failed requests:表示失敗的總請求數,這裏的失敗指請求在鏈接服務器、發送數據、接收數據等環節發生異常,以及無響應超時等狀況 Total transferred:表示全部請求的響應數據長度總和,包括HTTP響應的頭信息和正文數據的長度 HTML transferred:表示全部請求的響應數據中正文數據的總和 Requests per second:表示服務器吞吐率(這是咱們應該重點關注的) Time per request:表示用戶平均請求的等待時間 Time per request (mean, across all concurrent requests):表示服務器平均請求處理時間 Transfer rate:表示這些請求在單位時間內從服務器獲取的數據長度 Percentage of the requests served within a certain time (ms):這部分數據描述每一個請求處理時間的分佈狀況