只要是在架站之後,想要了解一下你的 http service (Apache) 的效能,這時候可以使用 Apache 內建的效能測試程式 – ab (Apache HTTP Server Benchmarking Tool) 來得知.
Ubuntu
直接透過 apt-get 安裝就可以了.
root@benjr:~# apt-get install apache2-utils
RedHat
比較複雜一點.要安裝幾個套件後才能使用.
[root@benjr Packages]# rpm -ivh httpd-tools-2.4.6-31.el7.x86_64.rpm warning: httpd-tools-2.4.6-31.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY error: Failed dependencies: libapr-1.so.0()(64bit) is needed by httpd-tools-2.4.6-31.el7.x86_64 libaprutil-1.so.0()(64bit) is needed by httpd-tools-2.4.6-31.el7.x86_64 [root@benjr Packages]# rpm -ivh apr-1.4.8-3.el7.x86_64.rpm warning: apr-1.4.8-3.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ################################# [100%] Updating / installing... 1:apr-1.4.8-3.el7 ################################# [100%] [root@benjr Packages]# rpm -ivh apr-util-1.5.2-6.el7.x86_64.rpm warning: apr-util-1.5.2-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ################################# [100%] Updating / installing... 1:apr-util-1.5.2-6.el7 ################################# [100%] [root@benjr Packages]# rpm -ivh httpd-tools-2.4.6-31.el7.x86_64.rpm warning: httpd-tools-2.4.6-31.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ################################# [100%] Updating / installing... 1:httpd-tools-2.4.6-31.el7 ################################# [100%]
ab (ApacheBench) 工具存放在 httpd-tools 套件裡面,但安裝的時候會說少 libapr libaprutil 這兩個套件,但其實是要安裝 apr 與 apr-util 套件(在 RedHat Package 光碟內都有)
使用也相當簡單,就一行指令.
[root@benjr ]# ab -n 100 -c 10 http://www.google.com/index This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.google.com (be patient).....done Server Software: sffe Server Hostname: www.google.com Server Port: 80 Document Path: /index Document Length: 1566 bytes Concurrency Level: 10 Time taken for tests: 0.908 seconds Complete requests: 100 Failed requests: 0 Write errors: 0 Non-2xx responses: 100 Total transferred: 177100 bytes HTML transferred: 156600 bytes Requests per second: 110.17 [#/sec] (mean) Time per request: 90.769 [ms] (mean) Time per request: 9.077 [ms] (mean, across all concurrent requests) Transfer rate: 190.54 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 22 39 6.3 39 61 Processing: 31 47 8.4 47 69 Waiting: 30 47 8.1 46 69 Total: 61 87 11.6 86 116 Percentage of the requests served within a certain time (ms) 50% 86 66% 91 75% 92 80% 96 90% 101 95% 110 98% 114 99% 116 100% 116 (longest request)
指令 ab -n 100 -c 10 http://www.google.com/index
裡面用到兩個常用的參數
- -n requests
Number of requests to perform (代表連續測試 x 次) - -c concurrency
Number of multiple requests to make at a time (代表同時有 x 個連線)
最後就是要測試的網址.
其他參數請參考官方文件 http://httpd.apache.org/docs/2.2/programs/ab.html
我們來看一下執行的結果
Concurrency Level: 10
代表同時有 10 個連線
Time taken for tests: 0.908 seconds
Complete requests: 100
完成的請求數.
Failed requests: 0
失敗的請求數.
Write errors: 0
Non-2xx responses: 100
Total transferred: 177100 bytes
總資料傳送 Bytes
HTML transferred: 156600 bytes
HTML 資料傳送 Bytes
Requests per second: 110.17 [#/sec] (mean)
平均每秒可以回應多少個請求.
Time per request: 90.769 [ms] (mean)
平均每次請求所花費的時間,
Time per request: 9.077 [ms] (mean, across all concurrent requests)
平均每個請求所花費的時間 (同時有 10 個連線)
Transfer rate: 190.54 [Kbytes/sec] received
首先要來了解什麼是 TCP 三方交握 (TCP 3-way handshake) ,所謂的 TCP 三方交握(TCP 3-way handshake) 就是 Server 與 Client 間透過 3 次的溝通,才會進行資料的傳輸
這步驟如下:
- Client 用戶端向 Server 伺服器發送一個 "SYN" 訊息,跟 Server 伺服器請求連線.
- 如果 Server 伺服器準備好與 Client 用戶端連線,就會回傳一個 "SYN-ACK" 的訊息.
- 如果 Client 用戶端接受到剛剛的 "SYN-ACL" 而且也準備好,就會向 Server 伺服器發送一個 "ACK"訊號,讓 Server 伺服器知道現在要開始傳送資料了.
Client 用戶端每送一筆資料 Server 伺服器收到資料都會回傳給 Client 用戶端一個 ACK 訊號.但這樣未免太沒效率,所以通常會一次傳送多筆資料過去,這就是 TCP Sliding Window
Connection Times (ms)
min mean[+/-sd] median max
Connect: 22 39 6.3 39 61
Processing: 31 47 8.4 47 69
Waiting: 30 47 8.1 46 69
Total: 61 87 11.6 86 116
Connect:
這個就是剛剛說明 TCP 三方交握 (TCP 3-way handshake) 所需的時間.
Processing:
TCP 連線建立後,HTTP server 的資料全部都收到所花費的時間.
Waiting:
Client 發送到 HTTP 第一個 Byte 到全部完成所等待的時間.
Total:
Connect + Processing(包含 Waiting)
One thought on “ab – ApacheBench”