1. 程式人生 > >使用ab對Tomcat8.5進行壓力測試

使用ab對Tomcat8.5進行壓力測試

背景

一直不是很清楚tomcat能力大約是個什麼水平,到底算不算web伺服器中的高手,今天決定試試這傢伙的深淺。

恰好了解到apache自帶的ab(apache benchmark)工具可以進行簡單的壓力測試,所以就用它來試試。

流程

先配置tomcat8.5,然後隨便扔一個靜態網頁到tomcat上執行。

然後下載ab。

然後使用ab進行初步壓力測試。

最後使用ab對網易和本機tomcat對比測試。

下載並配置tomcat8.5

安裝並配置jdk

tomcat執行需要先安裝JDK,下載JDK1.8下載連結,然後配置環境變數:

新增JAVA_HOME,值為D:ToolJDK1.8 找到CLASSPATH

,最前方新增.;%JAVA_HOME%libdt.jar;%JAVA_HOME%lib ools.jar;

找到PATH,在後面追加;%JAVA_HOME%in;%JAVA_HOME%jrein;

下載並配置tomcat
D:Toolapache-tomcat-8.5.34

雙擊D:Toolapache-tomcat-8.5.34instartup.bat執行tomcat服務,然後開啟瀏覽輸入下面的地址驗證tomcat伺服器執行成功。

http://localhost:8080/
編寫測試網頁

編寫一個簡單的測試網頁hello.html,內容如下:

<html>
<head>
</head>
<body>
hello
</body>
</html>

放在

D:Toolapache-tomcat-8.5.34webappsROOThello.html

然後在瀏覽器輸入地址http://localhost:8080/hello.html,該地址在網頁上顯示hello字串,我們以此地址作為本機tomcat伺服器負載能力測試地址。

下載ab

ab是apache附帶的一個小工具,直接下載apache即可,地址:apache下載地址,注意64位系統選擇x64下載下載,點選右側的德國國旗圖示即可開始下載。下載後解壓至:

D:ToolApache24

我們在bin目錄下找到ab.exeD:ToolApache24inab.exe,就是這個小傢伙,作為今天的主角——效能測試工具。

執行一次簡單的壓力測試

開啟命令列,使用cd命令切換到bin目錄下,然後輸入ab -V檢視ab工具版本:

C:UsersAdministrator>D:

D:>cd D:ToolApache24in

D:ToolApache24in>ab -V
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/

輸入ab -n 10 -c 10 http://localhost:8080/hello.html進行測試,其中-n後面表示請求次數,-c後面表示併發數:(注意#符號後面內容是我加的註釋)

D:ToolApache24in>ab -n 10 -c 10 http://localhost:8080/hello.html
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 localhost (be patient).....done


Server Software:
#伺服器名稱
Server Hostname:        localhost
#伺服器埠
Server Port:            8080
#文件路徑
Document Path:          /hello.html
#文件大小
Document Length:        56 bytes
#併發數
Concurrency Level:      10
#測試消耗的時間
Time taken for tests:   0.007 seconds
#測試次數
Complete requests:      10
#失敗的請求數
Failed requests:        0
#傳輸的總資料量
Total transferred:      2700 bytes
#html文件的總資料量
HTML transferred:       560 bytes
#平均每秒的請求數
Requests per second:    1424.50 [#/sec] (mean)
#10個併發請求一次的平均時間
Time per request:       7.020 [ms] (mean)
#1個併發請求一次的平均時間
Time per request:       0.702 [ms] (mean, across all concurrent requests)
#傳輸速率
Transfer rate:          375.60 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       0
Processing:     2    3   0.6      4       4
Waiting:        2    3   0.6      3       4
Total:          2    3   0.6      4       4
WARNING: The median and mean for the processing time are not within a normal deviation
        These results are probably not that reliable.
WARNING: The median and mean for the total time are not within a normal deviation
        These results are probably not that reliable.
#各時間範圍完成請求數
Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      4
  80%      4
  90%      4
  95%      4
  98%      4
  99%      4
 100%      4 (longest request)

剛剛的請求10個請求10個併發對伺服器來說壓力太小了,我們來是個比較厲害的壓力:

D:ToolApache24in>ab -n 1000 -c 1000 http://localhost:8080/hello.html
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 localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:
Server Hostname:        localhost
Server Port:            8080

Document Path:          /hello.html
Document Length:        56 bytes

Concurrency Level:      1000
#花費的時間並不算多,說明tomcat蠻牛X的
Time taken for tests:   1.646 seconds
#全部請求都成功了,鮮花和掌聲!
Complete requests:      1000
Failed requests:        0
Total transferred:      270000 bytes
HTML transferred:       56000 bytes
#每秒600多個請求處理
Requests per second:    607.71 [#/sec] (mean)
Time per request:       1645.516 [ms] (mean)
Time per request:       1.646 [ms] (mean, across all concurrent requests)
Transfer rate:          160.24 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2  27.4      0     501
Processing:    56  672 550.2    586    1604
Waiting:        2  648 550.0    556    1593
Total:         56  673 550.6    586    1604

Percentage of the requests served within a certain time (ms)
  50%    586#0.586秒時處理了50%的請求
  66%   1096
  75%   1098
  80%   1100
  90%   1600
  95%   1601
  98%   1603
  99%   1603
 100%   1604 (longest request)#1.604秒時處理了50%的請求

與網易對比測試

分別對本地網頁http://localhost:8080/hello.html和網易的一個靜態網頁http://tech.163.com/18/0922/15/DSAOGIPR00097U7R.html10個請求併發10次訪問:

為了公平起見,我們將本地網頁的資料量弄得跟網易測試網頁差不多大:

本地網頁測試如下:

D:ToolApache24in>ab -n 10 -c 10 http://localhost:8080/hello.html
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 localhost (be patient).....done


Server Software:
Server Hostname:        localhost
Server Port:            8080

Document Path:          /hello.html
Document Length:        168908 bytes

Concurrency Level:      10
Time taken for tests:   0.006 seconds
Complete requests:      10
Failed requests:        0
Total transferred:      1691300 bytes
HTML transferred:       1689080 bytes
Requests per second:    1661.96 [#/sec] (mean)
Time per request:       6.017 [ms] (mean)
Time per request:       0.602 [ms] (mean, across all concurrent requests)
Transfer rate:          274498.95 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       1
Processing:     3    3   0.6      3       5
Waiting:        1    1   0.3      2       2
Total:          3    3   0.5      3       5
ERROR: The median and mean for the waiting time are more than twice the standard
       deviation apart. These results are NOT reliable.

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

網易網頁測試如下,在註釋中新增些資訊:

D:ToolApache24in>ab -n 10 -c 10 http://tech.163.com/18/0922/15/DSAOGIPR00097U7R.html
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 tech.163.com (be patient).....done

#伺服器nginx
Server Software:        nginx
Server Hostname:        tech.163.com
Server Port:            80

Document Path:          /18/0922/15/DSAOGIPR00097U7R.html
Document Length:        159350 bytes

Concurrency Level:      10
#耗時遠遠比本地網頁大,應該是有網路原因
Time taken for tests:   0.323 seconds
Complete requests:      10
Failed requests:        0
Total transferred:      1596970 bytes
HTML transferred:       1593500 bytes
Requests per second:    31.00 [#/sec] (mean)
Time per request:       322.622 [ms] (mean)
Time per request:       32.262 [ms] (mean, across all concurrent requests)
Transfer rate:          4833.96 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        7   19  12.2     12      39
Processing:   129  169  27.4    166     222
Waiting:        8  100  57.4    126     170
Total:        166  188  23.8    182     229

Percentage of the requests served within a certain time (ms)
#剛開始50%比較慢,後面50%速度變快,說明重複請求時快取起作用了
  50%    182
  66%    186
  75%    197
  80%    229
  90%    229
  95%    229
  98%    229
  99%    229
 100%    229 (longest request)