什么是Apache ab
ab是用於對Apache超文本傳輸協議(HTTP)服務器進行基准測試的工具。這么解釋有點官方,下面會用幾個實例進行解釋。
環境搭建
APache ab 可以安裝在Linux下面,也可以安裝在Windows下面。但是安裝在Windows下面有點麻煩,而且自己比較喜歡Linux,所以下面只介紹在Linux環境下面搭建Apache ab
Linux環境搭建
sudo apt-get install apache2-utils //Ubuntu舉例
驗證是否安裝成功
參數介紹
root@ubuntu:~# 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 //超時,默認每個response 30s Default is 30 seconds -b windowsize Size of TCP send/receive buffer, in bytes //TCP發送/接收緩沖區的大小,以字節為單位 -B address Address to bind to when making outgoing connections //Address to bind to when making outgoing connections -p postfile File containing data to POST. Remember also to set -T //包含POST數據的文件。 -u putfile File containing data to PUT. Remember also to set -T //包含PUT的文件 -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 //在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 //作為tr屬性插入的字符串 -z attributes String to insert as td or th attributes //作為td或th屬性插入的字符串 -C attribute Add cookie, eg. 'Apache=1234'. (repeatable) //添加cookie -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip' //添加header Inserted after all normal header lines. (repeatable) -A attribute Add Basic WWW Authentication, the attributes //添加基本WWW身份驗證 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 // 要使用的Proxyserver和端口號 -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 //執行超過150個請求時不要顯示進度 -l Accept variable document length (use this for dynamic pages) //接受可變文檔長度(用於動態頁面) -g filename Output collected data to gnuplot format file. //將收集的數據輸出到gnuplot格式文件 -e filename Output CSV file with percentages served //輸出已提供百分比的CSV文件 -r Don't exit on socket receive errors. -m method Method name // 方法名稱 -h Display usage information (this message) //顯示使用信息 -Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers) -f protocol Specify SSL/TLS protocol (TLS1, TLS1.1, TLS1.2 or ALL)
實例
上面有部分參數我沒有進行翻譯~因為我暫時想不到使用場景。下面我會介紹Apache ab常用的幾種使用技巧
設置並發數,設置總請求數,請求方式為get請求
ab -c10 -n100 http://www.baidu.com/ // 10個用戶 一共發送100次請求,訪問百度首頁
設置設置並發數,設置總請求數,請求方式為post請求
post請求呢,我是用本地django項目調試的,試了一小時,發現真的不好用。網上找的資料也都模棱兩可,技術所限,后面再找找看。不過真心覺得不要用ab做post請求~jmeter特別好用
在HTML表格中打印結果
ab -c10 -n100 http://www.baidu.com/>report.html
測試結果分析
主要看以下數據:
Requests per second:吞吐率
Time taken for tests 測試總耗時
Failed requests 請求失敗數目
Time per request:上面的是用戶平均請求等待時間
Time per request:上面的是用戶平均請求等待時間
總結
雖然找了很多資料,去寫這個博客,但是自己慢慢用ab的時候,發現真的不好用,locust、siege、jmeter等都比這玩意好用,反正,仁者見仁,智者見智吧。想寫這個也只是興趣