mac環境使用Apache ab工具測試服務性能

基本概念

先引用一段官方介紹,html

ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed to give you an impression of how your current Apache installation performs. This especially shows you how many requests per second your Apache installation is capable of serving.

意思是ab是apache的http服務測試工具,它能夠測試你的http server的性能,尤爲是每秒處理請求數。ab不只能夠對apache服務器進行網站訪問壓力測試,也能夠對或其它類型的服務器進行壓力測試。好比nginx、tomcat、IIS等。nginx

要注意的是,ab的測試目標是基於URL的,因此基於TCP的服務接口時無法用ab測試的。apache

環境準備

個人運行環境是macOS 10.14.6。tomcat

其實mac自己是帶來了ab工具的,你能夠試試在終端窗口執行服務器

ab -v

能夠看到ab的版本。併發

可是這個自帶的版本會有些問題,好比我在實際測試的發現以下現象,app

$ ab -n 1000 -c 200 http://127.0.0.1:8080/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
apr_socket_recv: Connection reset by peer (54)
Total of 524 requests completed

上網查了下,瞭解到這個報錯是由於ab版本太低引發的。因此要安裝一個比較新的版本替換掉這個自帶的。socket

首先要下載ab工具,咱們能夠去下面這個地址找到最新的版本而後下載便可。工具

http://httpd.apache.org/download.cgipost

我這裏下載的是 httpd-2.4.41.tar.gz這個版本。你們只要是下載一個最新的文檔版本就好了。

還須要安裝幾個依賴,aprapr-uitl,還有pcre。這個能夠在下面這個地址找到最新的版本下載。

http://apr.apache.org/download.cgi

https://ftp.pcre.org/pub/pcre/pcre-8.00.tar.gz

安裝的過程以下。

安裝apr,進入源碼目錄

./configure --prefix=/usr/local/apr 
sudo make 
sudo make install

安裝apr-uitl,進入源碼目錄

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
sudo make 
sudo make install

安裝pcre,進入源碼目錄

./configure --prefix=/usr/local/pcre

安裝httpd,進入源碼目錄

./configure --prefix=/usr/local/httpd/ --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/

sudo make

sudo make install

而後拷貝/usr/local/httpd目錄下的ab執行文件替換/usr/sbin下的。

另外,根據本身的實際狀況,還能夠調整下ulimit -n參數,不然測試的時候可能會報too many open fiels

ponydeMacBook-Pro:httpd ponyma$ ulimit -n 5000
ponydeMacBook-Pro:httpd ponyma$ ulimit -n
5000

ab經常使用命令

ab的命令不少,這裏給出比較經常使用的幾個,等下的示例也會用到。

-n    請求的總數,默認是1次。

-c    併發數,同一時間請求數。

-p    若是後面接的是文件,表示POST請求,文件中包含請求數據,根據數據格式,設置-T參數。

-T    設置請求頭中的Content-type(針對POST/PUT),好比:application/x-www-form-urlencoded。默認是text/plain。

-w    將測試結果打印到HTML表格中。

更多的命令能夠經過 man ab查詢。

開始測試

咱們先不測試本身的服務,先用qq的網站看看測試結果,一會也好作對比。

$ ./bin/ab -n 200 -c 100 http://www.qq.com/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.qq.com (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


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

Document Path:          /
Document Length:        154 bytes

Concurrency Level:      100
Time taken for tests:   0.387 seconds
Complete requests:      200
Failed requests:        0
Non-2xx responses:      200
Total transferred:      67200 bytes
HTML transferred:       30800 bytes
Requests per second:    516.36 [#/sec] (mean)
Time per request:       193.661 [ms] (mean)
Time per request:       1.937 [ms] (mean, across all concurrent requests)
Transfer rate:          169.43 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       14   29   7.7     28      49
Processing:    16   66  72.6     34     315
Waiting:       16   50  45.8     34     315
Total:         39   95  73.6     62     350

Percentage of the requests served within a certain time (ms)
  50%     62
  66%     81
  75%     85
  80%    129
  90%    185
  95%    310
  98%    341
  99%    349
 100%    350 (longest request)

而後我再測試下我本身的博客站點,

個人博客

$ ab -n 200 -c 100 http://www.machengyu.net/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.machengyu.net (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software:        GitHub.com
Server Hostname:        www.machengyu.net
Server Port:            80

Document Path:          /
Document Length:        18750 bytes

Concurrency Level:      100
Time taken for tests:   1.863 seconds
Complete requests:      200
Failed requests:        0
Total transferred:      3877599 bytes
HTML transferred:       3750000 bytes
Requests per second:    107.33 [#/sec] (mean)
Time per request:       931.721 [ms] (mean)
Time per request:       9.317 [ms] (mean, across all concurrent requests)
Transfer rate:          2032.11 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       97  118  17.6    116     199
Processing:   205  434 259.1    282    1218
Waiting:      100  259 173.9    159     661
Total:        308  552 258.2    403    1328

Percentage of the requests served within a certain time (ms)
  50%    403
  66%    599
  75%    632
  80%    690
  90%    986
  95%   1183
  98%   1285
  99%   1310
 100%   1328 (longest request)

我先解釋下測試結果的各個指標都是啥意思。

  • Concurrency Level    併發數多少,等於-c後面的數值
  • Time taken for tests    測試總耗時
  • Complete requests    成功收到返回的數目
  • Failed requests    請求失敗數目
  • Non-2xx responses    表示返回的HTTP status code不是2xx的數目(好比404,401,500...),若是都是2xx,這個指標不顯示在結果裏面
  • Requests per second    每秒請求數
  • Time per request    每個請求平均花費時間。第一個Time per request等於concurrency timetaken 1000 / done,第二個Time per request(mean, across all concurrency requests)等於timetaken * 1000 / done,第一個能夠理解爲用戶平均請求等待時間,第二能夠理解爲服務器平均請求等待時間

基於這幾個指標,qq的網站和個人博客站點哪一個性能好,就不用多說了吧,哈哈!

上面兩個測試都是基於get請求的,基於post請求也很容易,這裏就很少說了。有興趣能夠本身測試下。


參考:

https://www.cnblogs.com/cjsbl...

相關文章
相關標籤/搜索