三月 17, 2016 | 伺服器和Devops
網頁伺服器監測站!!不能不知道的ApacheBench使用方法
看過壓力測試軟體介紹後,大家是否都想嘗試其中寫到的各種壓力測試軟體呢?是否都想了解自己的伺服器,符不符合自己的目前的流量呢?讓我們來聊聊,如何簡單的使用ApacheBench吧!
ApacheBench介紹
這邊我們簡單介紹一下 ,ApacheBench 是由 Apache 開發的測試工具,專門用來測試執行網站伺服器的運行效能,可以看出網站伺服器每秒送出多少網頁,處理多少請求。
ApacheBench使用說明
安裝方式
在Ubuntu作業系統下
直接透過 apt-get 安裝就可以了
$ sudo apt-get install apache2-utils
在Windows作業系統下
到 Apache HTTP Server 直接下載 Win32 Binary 的版本,由於線上所提供的版本是 MSI 的封裝檔(Windows Installer ),安裝好之後也等同於在你的電腦內安裝了一套 Apache HTTP Server。
如果不需要多執行一套 Apache HTTP Server 的話,可以在安裝好之後進入 C:\Program Files\Apache Group\Apache2\bin 目錄,找到 ab.exe 執行檔,複製出來後再移除 Apache 安裝即可,因為 ab.exe 是可以獨立執行的,不需要任何關連的 dll 檔。
使用參數
以下是 ApacheBench 的使用參數摘要說明,若要看詳細說明可以看這篇
# ab -h
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 at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-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 to use for POST/PUT data, 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.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-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
(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
指令很多,但是我們常用到的,一般都只有 -c (模擬連線使用者) 與 -n (總請求次數)
模擬測試
模擬同時 10 個連線使用者,連續請求 1000 次瀏覽( 每個 請求(Request) 執行完畢後都會自動斷線,然後再重新連線 ) 輸出範例:
# ab -c 10 -n 1000 http://test1.n.tw/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking test1.n.tw (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: nginx/1.4.6
Server Hostname: test1.n.tw
Server Port: 80
Document Path: /
Document Length: 9643 bytes
Concurrency Level: 10
Time taken for tests: 10.946 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 10037000 bytes
HTML transferred: 9643000 bytes
Requests per second: 91.36 [#/sec] (mean)
Time per request: 109.462 [ms] (mean)
Time per request: 10.946 [ms] (mean, across all concurrent requests)
Transfer rate: 895.45 [Kbytes/sec] received
Connection Times (ms) :
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 64 109 26.7 104 306
Waiting: 60 104 26.1 100 303
Total: 64 109 26.7 105 306
Percentage of the requests served within a certain time (ms)
50% 105
66% 116
75% 123
80% 127
90% 140
95% 157
98% 170
99% 190
100% 306 (longest request)
輸出結果介紹
這邊介紹下上面的輸出結果的參數介紹
Server Software (Web主機的作業系統與版本)
Server Hostname (Web主機的IP位址)
Server Port (Web主機的連接埠(Port))
Document Path (網址的路徑部分)
Document Length (網頁回應的網頁大小)
Concurrency Level (同時進行壓力測試的人數)
Time taken for tests (本次壓力測試所花費的總秒數)
Complete requests (完成的要求數(Requests))
Failed requests (失敗的要求數(Requests))
Total transferred (本次壓力測試的總數據傳輸量(包括 HTTP Header 的資料也計算在內))
HTML transferred (本次壓力測試的總數據傳輸量(僅計算回傳的 HTML 的資料))
Requests per second (平均每秒可回應多少要求)
Time per request (平均每個要求所花費的時間(單位: 豪秒))
Time per request (平均每個要求所花費的時間,所有同時連線數的平均值(單位: 豪秒))
Transfer rate (從 ab 到 Web Server 之間的網路傳輸速度)
Connection Times (ms) (壓力測試時的連線處理時間)
下面的表格
橫軸欄位的部分:
min: 最小值
mean: 平均值(正、負標準差)
median: 平均值(中間值)
max: 最大值
縱軸欄位的部分:
Connect: 從 ab 發出 TCP 要求到 Web 主機所花費的建立時間。
Processing: 從 TCP 連線建立後,直到 HTTP 回應(Response)的資料全部都收到所花的時間。
Waiting: 從發送 HTTP 要求完後,到 HTTP 回應(Response)第一個 Byte 所等待的時間。
Total: 等於 Connect + Processing 的時間(因為 Waiting 包含在 Processing 時間內了)
結論
在經過這篇介紹後,大家應該都了解ApacheBench的操作與查看輸出結果。
在最後,給上一些進行壓力測試的注意事項
- 排除頻寬的限制
壓力測試通常不會考量「頻寬的限制」,所以大都在自己的伺服器上直接執行- 壓力要循序漸進
一次直接模擬太多人數,會使自己無法掌握伺服器目前可以多少人可穩定執行,當然也可以一次給大量的人數,試試web server的極限。