Apache服務器自帶了ab壓力測試工具,能夠用來測試網站性能,使用簡單方便。php
Apachehtml
打開Apache服務器的安裝路徑,在bin目錄中有一個ab.exe的可執行程序,就是咱們要介紹的壓力測試工具。web
在Windows系統的命令行下,進入ab.exe程序所在目錄,執行ab.exe程序。注意直接雙擊沒法正確運行。算法
執行ab命令成功後,能夠看到如圖提示。該幫助很清楚詳細的介紹了ab的用法以及各個參數的含義。apache
ab 的用法是:ab [options] [http://]hostname[:port]/path服務器
例如:ab -n 5000 -c 200 http://localhost/index.phpcookie
上例表示總共訪問http://localhost/index.php這個腳本5000次,200併發同時執行。網絡
ab經常使用參數的介紹:併發
-n :總共的請求執行數,缺省是1;ide
-c: 併發數,缺省是1;
-t:測試所進行的總時間,秒爲單位,缺省50000s
-p:POST時的數據文件
-w: 以HTML表的格式輸出結果
執行測試用例:ab -n 1000 -c 100 -w http://localhost/index.php >>c:\1.html
上面的測試用例表示100併發的狀況下,共測試訪問index.php腳本1000次,並將測試結果保存到c:\1.html文件中。
測試報告如圖,可知在該100併發訪問的狀況下,共測試訪問1000次,失敗了852次。可知該腳本在此環境沒法知足100併發訪問的要求。
修改參數繼續測試。測試併發50和30兩種狀況,由測試報告得知,在併發訪問降到30時,錯誤的訪問數降爲39。
格式: ./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時間乘以併發請求數
測試結果也一目瞭然,測試出的吞吐率爲:Requests per second: 2015.93 [#/sec] (mean) 初次以外還有其餘一些信息。
Server Software 表示被測試的Web服務器軟件名稱
Server Hostname 表示請求的URL主機名
Server Port 表示被測試的Web服務器軟件的監聽端口
Document Path 表示請求的URL中的根絕對路徑,經過該文件的後綴名,咱們通常能夠了解該請求的類型
Document Length 表示HTTP響應數據的正文長度
Concurrency Level 表示併發用戶數,這是咱們設置的參數之一
Time taken for tests 表示全部這些請求被處理完成所花費的總時間
Complete requests 表示總請求數量,這是咱們設置的參數之一
Failed requests 表示失敗的請求數量,這裏的失敗是指請求在鏈接服務器、發送數據等環節發生異常,以及無響應後超時的狀況。若是接收到的HTTP響應數據的頭信息中含有2XX之外的狀態碼,則會在測試結果中顯示另外一個名爲 「Non-2xx responses」的統計項,用於統計這部分請求數,這些請求並不算在失敗的請求中。
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 表示這些請求在單位時間內從服務器獲取的數據長度,計算公式:Total trnasferred/ Time taken for tests,這個統計很好的說明服務器的處理能力達到極限時,其出口寬帶的需求量。
Percentage of requests served within a certain time(ms) 這部分數據用於描述每一個請求處理時間的分佈狀況,好比以上測試,80%的請求處理時間都不超過6ms,這個處理時間是指前面的Time per request,即對於單個用戶而言,平均每一個請求的處理時間。