標籤:style blog http color 使用 os io 檔案
ab是apache 內建的一個壓力測試的小工具,可用於介面簡單的壓力測試。
以下是AB的簡要介紹
格式: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的資料的檔案,文字檔裡邊放個json個是的資料,post.txt-T content-type Content-type header for POSTing//POST資料所使用的Content-type頭資訊,如 -T “application/x-www-form-urlencoded” ,如果是json的話, -T “application/json” (配合-p)-v verbosity How much troubleshooting info to print//設定顯示資訊的詳細程度 – 4或更大值會顯示頭資訊, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他資訊。 -V 顯示版本號碼並退出。//如果為4,可以輸出響應資訊和回應標頭-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-z attributes String to insert as td or th attributes-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 attributesare a colon separated username and password.-P attribute Add Basic Proxy Authentication, the attributesare 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-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而不是伺服器的效能。
舉一個工作中的例子:
ab.exe -c 10 -n 100 -t 60 -C "cookies" -p post.txt -T ‘application/json‘ -v 4 http://xxxx.xxxx.com/lmauto/api/xxxx/order/batchSave.htm
如上所述:
-c是有10個並發使用者,
-n 每個發送100個請求,
-t 總共處理60s,
-C 是有些介面需要驗證使用者的登入身份,所以帶上cookie,雙引號裡邊的cookies,就是你需要的cookies,
-p 是post請求需要的參數檔案,請保證參數檔案中資料的 正確性,
-T 是和-p一起使用,本文post的參數是json格式,所有採用了 ‘application/json‘,
-v 是列印伺服器的響應和回應標頭,有不同的層級,這次壓力測試剛開始沒能看見請求響應的結果,伺服器日誌也沒報異常,以為測試結果是OK的;第二天用了這個參數才知道,原來的測試結果誤差很大,根本就沒有能請求成功。(註:只要伺服器有響應(狀態代碼200),ab就認為是本次請求成功;不會在意返回的是成功和失敗,所以一定要確認,請求的返回是不是你所期望的)
後邊就是介面的url。
以下是1個並發,執行60s的測試結果:
註:還有一個弊端就是如果設計資料庫的insert操作,且是唯一的,ab的並發用的是hi同一份資料檔案,是無法進行資料的分配的,所以會有報重複,繼續關注,看看有沒有解決方案。