为了账号安全,请及时绑定邮箱和手机立即绑定

使用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:\Tool\JDK1.8
找到CLASSPATH,最前方添加.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;

找到PATH,在后面追加;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

下载并配置tomcat

地址:tomcat8.5下载链接,下载后解压到:

D:\Tool\apache-tomcat-8.5.34

双击D:\Tool\apache-tomcat-8.5.34\bin\startup.bat运行tomcat服务,然后打开浏览输入下面的地址验证tomcat服务器运行成功。

http://localhost:8080/
编写测试网页

编写一个简单的测试网页hello.html,内容如下:

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

放在

D:\Tool\apache-tomcat-8.5.34\webapps\ROOT\hello.html

然后在浏览器输入地址http://localhost:8080/hello.html,该地址在网页上显示hello字符串,我们以此地址作为本机tomcat服务器负载能力测试地址。

下载ab

ab是apache附带的一个小工具,直接下载apache即可,地址:apache下载地址,注意64位系统选择x64下载下载,点击右侧的德国国旗图标即可开始下载。下载后解压至:

D:\Tool\Apache24

我们在bin目录下找到ab.exeD:\Tool\Apache24\bin\ab.exe,就是这个小家伙,作为今天的主角——性能测试工具。

执行一次简单的压力测试

打开命令行,使用cd命令切换到bin目录下,然后输入ab -V查看ab工具版本:

C:\Users\Administrator>D:

D:\>cd D:\Tool\Apache24\bin

D:\Tool\Apache24\bin>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:\Tool\Apache24\bin>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:\Tool\Apache24\bin>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:\Tool\Apache24\bin>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:\Tool\Apache24\bin>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)

点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消