1. 壓力測試的概念\定義
2. AB的起源
AB 全稱【ApacheBench】是 Apache 自帶的一款功能強大的測試工具,可以快速測試基於 HTTP 協議所有 Web 頁面的最大負載壓力,擴展性非常強,
下載地址:https://www.apachelounge.com/download/
下載相應系統的zip包,這里我演示的是 https://www.apachelounge.com/download/VC15/binaries/httpd-2.4.33-win64-VC15.zip ApacheBench Ver 2.3
本章教程只授權本作者寫在:https://www.cnblogs.com/BenLam/p/9263927.html ,他人盜用必究
3. 運行平台區分(Windows\Linux|Mac)
3.1、Windows 7、8、10運行方式
使用 cmd/Windows PowerShell,進入 Apache 下的 bin 目錄,運行 ad 工具
cd ./Apache24/bin/
start ab
shell/Windows PowerShell會打印以下信息,即表明 ab 正常工作
Usage: ab [options] [http://]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. -m method Method name -h Display usage information (this message)
本章教程只授權本作者寫在:https://www.cnblogs.com/BenLam/p/9263927.html ,他人盜用必究
3.2、Linux|Mac 運行方式
通過 shell 輸入 cd /etc/httpd/bin 路徑,運行 ad 工具
cd /etc/httpd/bin start ab
4. 你的第一個AB GET請求腳本
在 cmd/shell 中輸入以下兩條命令:
ab -n 10 -c 10 http://127.0.0.1/ # -n 總共10次請求
# -c 是 concurrency ,模擬10個並發用戶,即發起10個並發請求 ab -t 10 -c 10 http://127.0.0.1/ # -t 是 timelimit 執行測試的時間,單位是秒
# -c 是 concurrency ,模擬10個並發用戶,即發起10個並發請求
#######
# 命令說明:
# 通過 ab 工具模擬用戶瀏覽器行為,Get請求cnblogs網站並返回參數告訴ab你有沒有訪問我
# ab 的刨坑: 千萬別再URL上缺少 http:// ,否者無法發起請求
結果分析:
root~/># ab -t 10 -c 10 http://127.0.0.1/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://127.0.0.1/ Licensed to The Apache Software Foundation, http://www.apache.org/
#
# ------ 上邊的不重要 ------
#
Benchmarking 127.0.0.1 (be patient) Finished 311 requests # 一共發了 311 個請求 Server Software: Server Hostname: 127.0.0.1 Server Port: 80 # 描述你測試 URL 和端口號通常是 80 端口
Document Path: / Document Length: 147 bytes
# 描述你請求的頁面的相關信息,頁面大小為 147 字節
#----------
# 從這開始是 ab 的重點
#Concurrency Level: 10
# 並發請求數量為:10
Time taken for tests: 10.179 seconds
# 整個測試耗費 10.179 秒
Complete requests: 311
# 完成請求數有311次
Failed requests: 0
# 失敗請求數量
Non-2xx responses: 311
# HTTP Code 不是2xx的
Total transferred: 118907 bytes
# 整個測試場景中產生的網絡傳輸總量
HTML transferred: 45717 bytes
# 整個測試場景中 HTML 內容傳輸量
Requests per second: 30.55 [#/sec] (mean)
# 每秒的請求平均數,這是重要指標之一
Time per request: 327.299 [ms] (mean)
# 每個請求的平均時間,這是重要指標之一
Time per request: 32.730 [ms] (mean, across all concurrent requests)
# 服務器處理請求的平均時間,這是重要指標之一
Transfer rate: 11.41 [Kbytes/sec] received
# 網絡平均轉移率
Connection Times (ms) min mean[+/-sd] median max Connect: 16 31 10.0 31 94 Processing: 235 284 55.1 265 655 Waiting: 31 189 82.3 203 655 Total: 266 315 58.7 296 686 Percentage of the requests served within a certain time (ms) 50% 296 66% 300 75% 312 80% 312 90% 344 95% 360 98% 577 99% 640 100% 686 (longest request)
本章教程只授權本作者寫在:https://www.cnblogs.com/BenLam/p/9263927.html ,他人盜用必究
5. AB發起POST、PUT請求
AB的POST請求需要在當前路徑下創建個空的notepad文檔,命名為【Postdata.txt】,這里演示模擬login操作
【Postdata.txt】內容如下填寫:
user_name=root&password=123
# 說明:
# user_name 是服務器約定好的字段,解釋:有個用戶叫:root 名
# password 是服務器約定好的字段,解釋:有個root的用戶密碼是:123
# 填寫完成后直接保存
ab用到的參數解釋(為什么要用這些參數才能完成POST請求)
-p 是參數文檔路徑<path> -T 是content-type,解釋:這個一般指定POST\PUT請求頭,例如:此次我們提交的是表單【application/x-www-form-urlencoded】 post 表單格式為:application/x-www-form-urlencoded
一般用 -p 參數都會帶上 -T 指定請求頭
組合成命令是:
ab -n 1 -c 1 -p /home/postdata.txt -T application/x-www-form-urlencoded "http://127.0.0.1/login"
參數細節:
-p 我放在文件路徑是/home/postdata.txt 中
-T 上邊我們講了這次我們做提交表單的操作所以必須是 application/x-www-form-urlencoded ,如果不帶這個參數,服務器有可能會拒絕我們的請求
后邊就是我們請求的URL了
當我們要修改的時候可以提出下邊的參數:
ab -n 1 -c 1 -u /home/postdata.txt -T application/x-www-form-urlencoded "http://127.0.0.1/login"
此時的 -p 就替換成 -u 就可以完成Put 請求了
6. AB附帶有趣的參數(AB的細節)
6.1、客戶要求,一個頁面在 1秒 內完成 10 個用戶的 10次 並發請求,也就是說我們的命令是:
ab -n 10 -c 10 <Test URL>
# 怎么才能更直觀的 1秒 內完成所有請求
# ↓ ↓ ↓ ↓ ↓
# 通過 -s 參數來執行
-s 參數解釋:我發起測試請求時計算計算,如果你不寫這個參數,請求超時時間是30秒
我們來完成客戶要求我們做的測試:
command:# ab -n 10 -c 10 -s 0.001 <Test URL>
# 如果在1秒內能通過測試,就返回正常的測試數據
# 如果不能通過測試,會返回下邊的提示:
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)...apr_pollset_poll: The timeout specified has expired (70007)
# 意思是: apr_pollset_poll(指定的超時已過期)
6.2、AB如果輸出詳細的測試信息:
# ab 輸出HTML測試報告
在 ab 中加上 -w 參數,即可打印出HTML測試報告
ab的測試報告截圖:
6.3、AB如果插入Cookies 和 Header:
通過 ab 參數:-C 和 -H 兩個參數實現
-C 即是 Cookies的第一個大寫字母,意思:就是添加cookies
-H 即是 Header的第一個大寫字母,意思:就是添加請求頭信息
用法:
# ab -n 1 -c 1 -H '_name=_value' -C 'cookie1_name=cookie1_value' <Test URL>
# 這樣就可以實現帶表頭、緩存的請求了
本章教程只授權本作者寫在:https://www.cnblogs.com/BenLam/p/9263927.html ,他人盜用必究
6.4、AB請求帶proxy:port
ab -n 1 -c 1 -X 8080 <Test URL>
本章教程只授權本作者寫在:https://www.cnblogs.com/BenLam/p/9263927.html ,他人盜用必究
7. AB參數中文翻譯:
ab參數翻譯如下:
-n 即requests,用於指定壓力測試/總請求數。 -c 即concurrency,用於指定壓力測試的並發數。 -t 即timelimit,測試執行最大秒數,它可以讓測試限制在一個固定的總時間以內,默認值為50000。 -s 即timeout,請求最大等待時長,默認30s -b 即windowsize,TCP發送/接收的緩沖大小(單位:字節)。 -p 即postfile,發送POST請求時需要上傳的文件,文件格式如"p1=1&p2=2"。使用方法是 -p 123.txt 。 (配合-T) -u 即putfile,發送PUT請求時需要上傳的文件。(配合-T) -T 即content-type,用於設置Content-Type請求頭信息,如 -T "application/x-www-form-urlencoded”,默認值為text/plain。(配合-p) -v 即verbosity,設置顯示信息的詳細程度 – 4或更大值會顯示頭信息, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。 -w 以HTML表格形式打印結果。 -i 使用HEAD請求代替GET請求。 -x 插入字符串作為table標簽的屬性。 -y 插入字符串作為tr標簽的屬性。 -z 插入字符串作為td標簽的屬性。 -C 添加cookie信息,例如:"Apache=1234"。此參數可以重復,用逗號分割。提示:可以借助session實現原理傳遞 JSESSIONID參數, 實現保持會話的功能,如-C "c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8"。 -H 添加任意的請求頭,例如:"Accept-Encoding: gzip",請求頭將會添加在現有的多個請求頭之后(可以重復該參數選項以添加多個)。 -A 添加一個基本的網絡認證信息,用戶名和密碼之間用英文冒號隔開。 -P 添加一個基本的代理認證信息,用戶名和密碼之間用英文冒號隔開。如-P proxy-auth-username:password -X 指定使用的代理服務器和端口號,例如:"126.10.10.3:88"。 -V 顯示版本號並退出。 -k 使用HTTP的KeepAlive特性。 -d 不顯示百分比。 -S 不顯示預估和警告信息。 -q 超過150個請求后不顯示進度 -l 接受可變文檔長度(用於動態頁面) -g filename 輸出結果信息到gnuplot格式的文件中。 -e filename 輸出結果信息到CSV格式的文件中。 -r 指定接收到錯誤信息時不退出程序。 -m method 方法名 -h 幫助