ab(Apache Bench)命令詳解以及壓力測試模擬

簡介html

ApacheBench 是 Apache服務器自帶的一個web壓力測試工具,簡稱ab。ab又是一個命令行工具,對發起負載的本機要求很低,根據ab命令能夠建立不少的併發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問,所以能夠用來測試目標服務器的負載壓力。總的來講ab工具小巧簡單,上手學習較快,能夠提供須要的基本性能指標,可是沒有圖形化結果,不能監控。nginx

語法及參數web

Usage: ab [options] [http[s]://]hostname[:port]/path
用法:ab [選項] 地址apache

選項:
Options are:
    -n requests    #執行的請求數,即一共發起多少請求。
    -c concurrency    #請求併發數。
    -t timelimit    #測試所進行的最大秒數。其內部隱含值是-n 50000,它可使對服務器的測試限制在一個固定的總時間之內。默認時,沒有時間限制。
    -s timeout    #指定每一個請求的超時時間,默認是30秒。
    -b windowsize    #指定tcp窗口的大小,單位是字節。
    -B address    #指定在發起鏈接時綁定的ip地址是什麼。
    -p postfile    #指定要POST的文件,同時要設置-T參數。
    -u putfile    #指定要PUT的文件,同時要設置-T參數。
    -T content-type    #指定使用POST或PUT上傳文本時的文本類型,默認是'text/plain'。
    -v verbosity    #設置詳細模式等級。
    -w    #將結果輸出到html的表中。
    -i    #使用HEAD方式代替GET發起請求。
    -y attributes    #以表格方式輸出時,設置html表格tr屬性。 
    -z attributes    #以表格方式輸出時,設置html表格th或td屬性。
    -C attribute    #添加cookie,好比'Apache=1234'。(可重複)
    -H attribute    #爲請求追加一個額外的頭部,好比'Accept-Encoding: gzip'。(可重複)
    -A attribute    #對服務器提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。不管服務器是否須要(即,是否發送了401認證需求代碼),此字符串都會被髮送。
    -P attribute    #對一箇中轉代理提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。不管服務器是否須要(即, 是否發送了401認證需求代碼),此字符串都會被髮送。
    -X proxy:port   #指定代理服務器的IP和端口。
    -V              #打印版本信息。
    -k              #啓用HTTP KeepAlive功能,即在一個HTTP會話中執行多個請求。默認時,不啓用KeepAlive功能。
    -d              #不顯示"percentage served within XX [ms] table"的消息(爲之前的版本提供支持)。
    -q              #若是處理的請求數大於150,ab每處理大約10%或者100個請求時,會在stderr輸出一個進度計數。此-q標記能夠抑制這些信息。
    -g filename     #把全部測試結果寫入一個'gnuplot'或者TSV(以Tab分隔的)文件。此文件能夠方便地導入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行爲標題。
    -e filename     #產生一個以逗號分隔的(CSV)文件,其中包含了處理每一個相應百分比的請求所須要(從1%到100%)的相應百分比的(以微妙爲單位)時間。因爲這種格式已經「二進制化」,因此比'gnuplot'格式更有用。
    -r              #當收到錯誤時不要退出。
    -h              #輸出幫助信息
    -Z ciphersuite  指定SSL/TLS密碼套件
    -f protocol     指定SSL/TLS協議(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
示例以及結果分析 
發起併發爲10,總量爲100的請求www.baidu.com,以下:windows

[root@jia1 ~]# ab -n 100 -c 10 http://www.baidu.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/服務器

Benchmarking www.baidu.com (be patient).....donecookie


Server Software:        BWS/1.1
Server Hostname:        www.baidu.com
Server Port:            80網絡


Document Path:          /      #測試頁面
Document Length:        112439 bytes     #測試頁面大小併發

Concurrency Level:      10     #併發數
Time taken for tests:   1.256 seconds    #整個測試話費的時間
Complete requests:      100    #完成請求的總量
Failed requests:        96     #失敗的請求次數
   (Connect: 0, Receive: 0, Length: 96, Exceptions: 0)
Write errors:           0
Total transferred:      11348660 bytes    #傳輸數據總大小
HTML transferred:       11253726 bytes    #傳輸頁面總大小
Requests per second:    79.62 [#/sec] (mean)    #平均每秒請求數
Time per request:       125.593 [ms] (mean)     #平均每次併發10個請求的處理時間
Time per request:       12.559 [ms] (mean, across all concurrent requests)   #平均每一個請求處理時間,全部併發的請求加一塊兒
Transfer rate:          8824.29 [Kbytes/sec] received  #平均每秒網絡流量tcp

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        4   20   7.7     18      38
Processing:    18   90  50.5     82     356
Waiting:        4   22   7.9     22      41
Total:         22  111  50.7    101     384
#花費在鏈接Connect,處理Processing,等待Waiting的時間的最小min,平均值mean,標準差[+/-sd],中值median,最大表max的一個表。

Percentage of the requests served within a certain time (ms)
  50%    101   #50%請求的響應時間在101ms內
  66%    103   #66%請求的響應時間在103ms內
  75%    104   #...以此類推
  80%    105
  90%    111
  95%    267
  98%    311
  99%    384
 100%    384 (longest request)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
能夠看到用www.baidu.com測試,錯誤率很高。這應該和www.baidu.com有關係。正常狀況下,不會錯誤率這麼高。 
不過了解了具體怎麼分析便可。 
補充個測試www.sina.com的數據,總數100併發爲20.失敗爲0.以下:

[root@jia1 ~]# ab -n 100 -c 20 http://www.sina.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.sina.com (be patient).....done


Server Software:        nginx
Server Hostname:        www.sina.com
Server Port:            80

Document Path:          /
Document Length:        178 bytes

Concurrency Level:      20
Time taken for tests:   0.081 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Non-2xx responses:      100
Total transferred:      71200 bytes
HTML transferred:       17800 bytes
Requests per second:    1231.15 [#/sec] (mean)
Time per request:       16.245 [ms] (mean)
Time per request:       0.812 [ms] (mean, across all concurrent requests)
Transfer rate:          856.03 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        2    6   2.5      6      14
Processing:     3    7   3.2      5      14
Waiting:        3    6   2.7      5      14
Total:          7   13   3.8     12      23

Percentage of the requests served within a certain time (ms)   50%     12   66%     14   75%     15   80%     16   90%     17   95%     21   98%     21   99%     23  100%     23 (longest request) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 還有就是ab屬於一個輕量級的壓測工具,結果不會特別準確,能夠用做參考。  追求準確的話,推薦使用jmeter或者LoadRunner。 ---------------------  做者:賈己人  來源:CSDN  原文:https://blog.csdn.net/jiajiren11/article/details/79486967  版權聲明:本文爲博主原創文章,轉載請附上博文連接!

相關文章
相關標籤/搜索