ab命令(ApacheBench)
ApacheBench(即ab)通常用來做網站性能壓力測試,是性能調優過程中必不可少的一環,ab命令會創建很多的並發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問。它的測試目標是基於URL的,因此,既可以用來測試Apache的負載壓力,也可以測試nginx、lighthttp、tomcat、IIS等其它Web服務器的壓力。
ab命令對發出負載的計算機要求很低,既不會占用很高CPU,也不會占用很多內存,但卻會給目標服務器造成巨大的負載,其原理類似CC攻擊。自己測試使用也須注意,否則一次上太多的負載,可能造成目標服務器因資源耗完,嚴重時甚至導致死機。
ApacheBench參數說明
格式:ab [options] [http://]hostname[:port]/path
參數說明:
-n requests Number of requests to perform
//在測試會話中所執行的請求個數(本次測試總共要訪問頁面的次數)。默認時,僅執行一個請求。-c concurrency Number of multiple requests to make
//一次產生的請求個數(並發數)。默認是一次一個。-t timelimit Seconds to max. wait for responses
//測試所進行的最大秒數。其內部隱含值是-n 50000。它可以使對服務器的測試限制在一個固定的總時間以內。默認時,沒有時間限制。-p postfile File containing data to POST
//包含了需要POST的數據的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt 。 (配合-T)-T content-type Content-type header for POSTing
//POST數據所使用的Content-type頭信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p)-v verbosity How much troubleshooting info to print
//設置顯示信息的詳細程度 – 4或更大值會顯示頭信息, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。 -V 顯示版本號並退出。-w Print out results in HTML tables
//以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。-i Use HEAD instead of GET
// 執行HEAD請求,而不是GET。-x attributes String to insert as table attributes //用來設置輸出結果的表格的table屬性
-y attributes String to insert as tr attributes //用來設置輸出結果的表格的tr屬性
-z attributes String to insert as td or th attributes //用來設置輸出結果的表格的th屬性-C attribute Add cookie, eg. -C “c1=1234,c2=2,c3=3″ (repeatable)
//-C cookie-name=value 對請求附加一個Cookie:行。 其典型形式是name=value的一個參數對。此參數可以重復,用逗號分割。
提示:可以借助session實現原理傳遞 JSESSIONID參數, 實現保持會話的功能,如
-C ” c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8″ 。-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.//-P proxy-auth-username:password 對一個中轉代理提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。無論服務器是否需要(即, 是否發送了401認證需求代碼),此字符串都會被發送。
-X proxy:port Proxyserver and port number to use
-V Print version number and exit //打印版本號,然后退出
-k Use HTTP KeepAlive feature //使用http keepAlive 特性
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-h Display usage information (this message)
//-attributes 設置屬性的字符串. 缺陷程序中有各種靜態聲明的固定長度的緩沖區。另外,對命令行參數、服務器的響應頭和其他外部輸入的解析也很簡單,這可能會有不良后果。它沒有完整地實現 HTTP/1.x; 僅接受某些’預想’的響應格式。 strstr(3)的頻繁使用可能會帶來性能問題,即你可能是在測試ab而不是服務器的性能。
參數很多,一般我們用 -c 和 -n 參數就可以了。例如:
# ab -c 5000 -n 10000 http://127.0.0.1/index.php
這里用-c指定每次請求並發數為5000,用-n設置請求次數為10000.
如果提示:ab: Cannot use concurrency level greater than total number of requests,說明-c的每次並發數設置太高,ab命令本身沒限制,是你的系統有限制。
測試報告
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 1997-2005 The Apache Software Foundation, http://www.apache.org/
Benchmarking www.google.com (be patient)…..done
//平台apache 版本2.0.54
Server Software: GWS/2.1//服務器主機名
Server Hostname: www.google.com//服務器端口
Server Port: 80//測試的頁面文檔
Document Path: ///文檔大小
Document Length: 230 bytes//並發數
Concurrency Level: 10/*整個測試持續的時間*/
Time taken for tests: 3.234651 seconds/*完成的請求數量*/
Complete requests: 10/*失敗的請求數量*/
Failed requests: 0/*請求寫入失敗的次數 */
Write errors: 0Non-2xx responses: 10
/*保持聯機連接的請求數量。只有在命令行中使用-k,才能看到該屬性值。*/
Keep-Alive requests: 10/*整個場景中的網絡傳輸量*/
Total transferred: 6020 bytes/*整個場景中的HTML內容傳輸量*/
HTML transferred: 2300 bytes/*大家最關心的指標之一,相當於 LR 中的 每秒事務數 ,有人稱作“吞吐率”,后面括號中的 mean 表示這是一個平均值*/
Requests per second: 3.09 [#/sec] (mean)/*大家最關心的指標之二,平均每個請求消耗的時間, 相當於 LR 中的 平均事務響應時間 ,后面括號中的 mean 表示這是一個平均值*/
Time per request: 3234.651 [ms] (mean)/*即上面的時間(Time per request)除以並發數 ,平均每個並發請求處理的時間 */
Time per request: 323.465 [ms] (mean, across all concurrent requests)/*平均每秒網絡上的流量,可以幫助排除是否存在網絡流量過大導致響應時間延長的問題,也就是這些請求在單位時間從服務器獲取的數據長度*/
Transfer rate: 1.55 [Kbytes/sec] received/*網絡上消耗的時間的分解,各項數據的具體算法還不是很清楚*/
Connection Times (ms)min mean[+/-sd] median max
Connect: 20 318 926.1 30 2954
Processing: 40 2160 1462.0 3034 3154
Waiting: 40 2160 1462.0 3034 3154
Total: 60 2479 1276.4 3064 3184
/*下面的內容為整個場景中所有請求的響應情況。在場景中每個請求都有一個響應時間,其中 50% 的用戶響應時間小於 3064 毫秒,60 % 的用戶響應時間小於 3094 毫秒,最大的響應時間小於 3184 毫秒*/
Percentage of the requests served within a certain time (ms)50% 3064
66% 3094
75% 3124
80% 3154
90% 3184
95% 3184
98% 3184
99% 3184
100% 3184 (longest request)
--------------------轉載
1、准備好ab小工具(我這個不用安裝)
2、編輯aaa.bat這個文件
輸入命令
ab -n 100 -c 10 http://test.com/
其中-n表示請求數,-c表示並發數
![]()
3、運行aaa.bat文
4、如果需要登陸后得頁面,舉例我遇到的,有token的
5、主要是要會分析結果
a、一般總次數是並發數的10倍(個人習慣),並發可以從1,10,50,100,200......這樣
b、通過chrome去點擊你的系統,看看那些請求比較耗時,超過500ms的都可以關注下,測測性能
c、一般說來,並發1測試出來的時間,100並發測試出來的時間相差到30倍以上,就說明程序有一定優化空間。