ab(ApacheBench)是一款小巧且使用簡單的壓測工具,可以提供站點基本的性能指標。ab 一般作為 Apache 服務器的子應用程序出現,而這里將介紹 Linux 下獨立安裝 ab 的方法以及它的簡單使用。
獨立安裝
CentOS 下獨立安裝 ab 的命令:
$ yum install httpd-tools
安裝后,查看 ab 版本:
$ ab -V
This is ApacheBench, Version 2.3
基本使用
基本命令
ab 使用命令格式為:
ab [options][http[s]://]hostname[:port]/path
通過ab -h命令查看 ab 命令參數,這里只列舉 常用參數。
-n # 在測試會話中所執(zhí)行的請求個數
-c # 一次產生的請求個數(并發(fā)數)
-p # 包含需要POST數據的文件,文件格式:“p1=1&p2=2”
-T # POST數據所使用的Content-type頭信息
-C # 對請求附加一個Cookie頭信息,格式為:name=value,多組值用 “,” 號分隔
-H # 對請求附加一個Header頭信息,格式例如:Accept-Encoding: gzip
對我博客主站點www.fanhaobai.com進行 100 并發(fā)壓測,命令如下:
$ ab -c 100 -n 100 https://www.fanhaobai.com/
測試結果
通過上述對www.fanhaobai.com的站點壓測,得到如下測試結果:
Benchmarking www.fanhaobai.com (be patient).....done
# web服務器名稱
Server Software: nginx
# host
Server Hostname: www.fanhaobai.com
# 監(jiān)聽端口,443(HTTPS)
Server Port: 443
SSL/TLS Protocol: TLSv1/SSLv3,ECDHE-RSA-AES256-GCM-SHA384,4096,256
# 測試的URI
Document Path: /
# 響應正文長度
Document Length: 37580 bytes
# 測試的并發(fā)數
Concurrency Level: 100
# 整個測試持續(xù)的時間
Time taken for tests: 41.491 seconds
# 完成的請求數量
Complete requests: 100
# 失敗的請求數量
Failed requests: 0
Write errors: 0
# 整個過程中的網絡傳輸量
Total transferred: 3792684 bytes
# 整個過程中的HTML內容傳輸量
HTML transferred: 3774201 bytes
# 吞吐率,最重要的指標之一
Requests per second: 2.41 [#/sec] (mean)
# 用戶平均請求等待時間,最重要的指標之二
Time per request: 41490.657 [ms] (mean)
# 服務器平均請求處理時間,最重要的指標之三
Time per request: 414.907 [ms] (mean, across all concurrent requests)
# 平均每秒網絡上的流量
Transfer rate: 89.27 [Kbytes/sec] received
# 網絡上消耗的時間的分解
Connection Times (ms)
min mean[+/-sd] median max
Connect: 60 1836 3096.3 1373 29355
Processing: 142 19725 7906.1 19955 38415
Waiting: 89 14829 8400.5 12547 38360
Total: 202 21561 7863.1 21334 41294
# 整個壓測中所有請求的響應情況。50% 用戶的響應時間小于 21334 毫秒,75% 用戶的響應時間小于26106 毫秒,最長響應時間小于 41294 毫秒。
Percentage of the requests served within a certain time (ms)
50% 21334
66% 25099
75% 26106
80% 26491
90% 30877
95% 36737
98% 41278
99% 41294
100% 41294 (longest request)
整理一下幾個 比較重要 的測試 指標:
- Requests per second —— 吞吐率
- Time per request —— 用戶平均請求等待時間
- Time per request —— 服務器平均請求處理時間
總結
ab 只是一款小巧使用簡單的壓測工具,沒有圖形化結果且不能監(jiān)控,只供臨時測試使用,商業(yè)化應用軟件必須使用專業(yè)壓測工具 LoadRunner。