1.安裝ab命令
sudo apt-get install apache2-utils
2.ab命令參數說明
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
//總的請求數 -n requests Number of requests to perform //一次同時并發的請求數 總的請求數(n)=次數*一次并發數(c) -c concurrency Number of multiple requests to make
-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
-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 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
-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而不是服務器的性能。
3.運行
ab -n 100 -c 10 http://www.companysz.com/
對 http://www.companysz.com/ 進行100次請求,10個并發請求壓力測試結果。
Server Software: lighttpd/1.4.20 Server Hostname: www.companysz.com Server Port: 80 Document Path: / Document Length: 2095 bytes Concurrency Level: 10 //整個測試持續的時間 Time taken for tests: 3.303 seconds //完成的請求數量 Complete requests: 100 Failed requests: 0 Write errors: 0 Total transferred: 235200 bytes HTML transferred: 209500 bytes //平均每秒處理30個請求 Requests per second: 30.27 [#/sec] (mean) //平均每個請求處理時間為330毫秒 注:這里將一次10個并發請求看成一個整體 Time per request: 330.335 [ms] (mean) //平均每個并發請求處理 時間 為33毫秒 Time per request: 33.034 [ms] (mean, across all concurrent requests) Transfer rate: 69.53 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 51 170 35.9 178 230 Processing: 60 153 64.5 121 263 Waiting: 55 148 64.4 115 258 Total: 235 322 59.9 299 437 Percentage of the requests served within a certain time (ms) //在這100個請求中有50%在299毫秒內完成 50% 299 //在這100個請求中有66%在312毫秒內完成 66% 312 75% 383 80% 412 90% 431 95% 432 98% 436 99% 437 100% 437 (longest request)
PS:如何查看當前Apache的連接數
查看了連接數和當前的連接數
netstat -ant | grep $ip:80 | wc -l netstat -ant | grep $ip:80 | grep EST | wc -l
查看IP訪問次數
netstat -nat|grep ":80"|awk '{print $5}' |awk -F: '{print $1}' | sort| uniq -c|sort -n
Linux命令:
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' watch "netstat -n | awk '/^tcp/ {++S[/$NF]} END {for(a in S) print a, S[a]}'"
返回結果示例:
LAST_ACK 5SYN_RECV 30ESTABLISHED 1597FIN_WAIT1 51FIN_WAIT2 504TIME_WAIT 1057
說明:
SYN_RECV 表示正在等待處理的請求數;
ESTABLISHED 表示正常數據傳輸狀態;
TIME_WAIT 表示處理完畢,等待超時結束的請求數;
FIN_WAIT1 表示server端主動要求關閉tcp連接;
FIN_WAIT2 表示客戶端中斷連接;
LAST_ACK 關閉一個TCP連接需要從兩個方向上分別進行關閉,雙方都是通過發送FIN來表示單方向數據的關閉,當通信雙方發送了最后一個FIN的時候,發送方此時處于LAST_ACK狀態,當發送方收到對方的確認(Fin的Ack確認)后才真正關閉整個TCP連接;