1. 程式人生 > >測試工具之ab

測試工具之ab

  ab命令原理 
  Apache的ab命令模擬多執行緒併發請求,測試伺服器負載壓力,也可以測試nginx、lighthttp、IIS等其它Web伺服器的壓力。 
  ab命令對發出負載的計算機要求很低,既不會佔用很多CPU,也不會佔用太多的記憶體,但卻會給目標伺服器造成巨大的負載,因此是某些DDOS攻擊之必備良藥,老少皆宜。自己使用也須謹慎。否則一次上太多的負載,造成目標伺服器直接因記憶體耗光宕機,而不得不硬重啟,得不償失。

  在頻寬不足的情況下,最好是本機進行測試,建議使用內網的另一臺或者多臺伺服器通過內網進行測試,這樣得出的資料,準確度會高很多。遠端對web伺服器進行壓力測試,往往效果不理想(因為網路延時過大或頻寬不足)

 

  ab安裝於使用

    這部分較為簡單,從官網上進行下載即可,或者你如果在Windows下使用整合環境,在apache/bin/ab.exe 可直接使用  

 

  ab使用

    1) GET測試

    

ab -c 2 -n 10  http://domain/optimalServer.do?username=zhang&sex=nan

-n發出10個請求,-c模擬2併發,相當2人同時訪問,後面是測試url

 

     2) POST測試

.\abs.exe  -c 5 -n 5 -T "application/x-www-form-urlencoded
" -p ab.txt https://api.example.com/api/activity181212/assist?shopid=1

 post中提交的內容儲存在一個檔案中:ab.txt 

activityId=7&auth_access_token=8f94acd6428576be6296151091cc1c88Nh1ITRXIPL36A6Xejf15435618192&inviterUid=l2iYaJpo

  


 

This is ApacheBench, Version 2.3 <$Revision: 1638069 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking twapi.feekr.com (be patient).....done


Server Software: nginx/1.6.0
Server Hostname: twapi.feekr.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256

Document Path: /api/activity181212/assist?shopid=PK
Document Length: 63 bytes

Concurrency Level: 5
Time taken for tests: 0.211 seconds
Complete requests: 5
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Total transferred: 2857 bytes
Total body sent: 1505
HTML transferred: 372 bytes
Requests per second: 23.65 [#/sec] (mean)
Time per request: 211.434 [ms] (mean)
Time per request: 42.287 [ms] (mean, across all concurrent requests)
Transfer rate: 13.20 [Kbytes/sec] received
6.95 kb/s sent
20.15 kb/s total

Connection Times (ms)
min mean[+/-sd] median max
Connect: 17 23 6.4 23 33
Processing: 57 118 45.6 131 178
Waiting: 57 118 45.6 131 178
Total: 74 141 45.9 153 199

Percentage of the requests served within a certain time (ms)
50% 148
66% 158
75% 158
80% 199
90% 199
95% 199
98% 199
99% 199
100% 199 (longest request)

 


 

  引數說明:

    -p: 檔案完整路徑

    -n: 請求的個數

    -c: 併發的數量

    -t: 時間限制,預設沒有限制 

    -T: post資料所使用的Content-type頭資訊