最近总遇到使用tomcat还是使用nginx进行前端文件访问的争论。出差周末在酒店, 可以自己进行一下简单的测试,希望能够对未来的工作进行一些指导。
测试脚本
#使用ab命令进行测试,ab命令全称为 Apache bench,是Apache自带的压力测试工具 #30KB 文件测试 ab -c 1000 -n 50000 -k http://127.0.0.1:5201/index.html #7.5MB 文件测试 ab -c 1000 -n 50000 -k http://127.0.0.1:5201/zhaobsh.html
简要测试结果
TPS的比较, 值越大越好
文件大小 | tomcat-TPS | nginx-TPS |
---|---|---|
20KB | 44094 | 66418 |
7.5MB | 585 | 672 |
响应时间的比较,值越小越好
文件大小 | tomcat-RT | nginx-RT |
---|---|---|
20KB | 22.679 | 15.056 |
7.5MB | 1708.946 | 1488. |
测试结果简要分析
Nginx在处理静态文件 在比较零散时应该至少比tomcat要好 50%;但是如果有网络瓶颈时, 可能不会有这样的效果。
但是同时发现,java会开启大量的http线程进行响应,内存占用会比较高,一个线程至少1MB的栈区。
同时发现JVM几乎不会有堆区GC的现象。
另外nginx在进行压测时CPU的使用量是很低;压测java时CPU是能到 30%。
详细结果信息
配置相同相同情况下:
- tomcat:采用产品直接运行的界面进行测试。
- nginx:采用将文件放到 html目录下面进行测试。
下面用对比测试来说明差异:30KB小文件和 7.5MB的大文件,直接在物理机器上面进行测试。
1、使用tomcat直接进行测试
1000个连接 五万个请求时 30KB测试结果 ab -c 1000 -n 50000 -k http://127.0.0.1:5201/index.html Document Path: /index.html Document Length: 30877 bytes Concurrency Level: 1000 Time taken for tests: 1.134 seconds Complete requests: 50000 Failed requests: 0 Keep-Alive requests: 49999 Total transferred: 1569699971 bytes HTML transferred: 1543850000 bytes Requests per second: 44094.08 [#/sec] (mean) Time per request: 22.679 [ms] (mean) Time per request: 0.023 [ms] (mean, across all concurrent requests) Transfer rate: 1351845.32 [Kbytes/sec] received 7.5MB测试结果 ab -c 1000 -n 50000 -k http://127.0.0.1:5201/zhaobsh.html Document Path: /zhaobsh.html Document Length: 7693835 bytes Concurrency Level: 1000 Time taken for tests: 85.447 seconds Complete requests: 50000 Failed requests: 0 Keep-Alive requests: 50000 Total transferred: 384717700000 bytes HTML transferred: 384691750000 bytes Requests per second: 585.16 [#/sec] (mean) Time per request: 1708.946 [ms] (mean) Time per request: 1.709 [ms] (mean, across all concurrent requests) Transfer rate: 4396872.91 [Kbytes/sec] received
2、使用nginx进行测试
nginx 采用 auto 的woker线程数 ab -c 1000 -n 50000 -k http://127.0.0.1:5300/index.html 30KB测试结果 Document Path: /index.html Document Length: 30877 bytes Concurrency Level: 1000 Time taken for tests: 0.753 seconds Complete requests: 50000 Failed requests: 56 (Connect: 0, Receive: 0, Length: 28, Exceptions: 28) Keep-Alive requests: 49972 Total transferred: 1554678892 bytes HTML transferred: 1542985444 bytes Requests per second: 66418.88 [#/sec] (mean) Time per request: 15.056 [ms] (mean) Time per request: 0.015 [ms] (mean, across all concurrent requests) Transfer rate: 2016797.47 [Kbytes/sec] received 7.5MB测试结果 ab -c 1000 -n 50000 -k http://127.0.0.1:5300/zhaobsh.html Document Path: /zhaobsh.html Document Length: 7693835 bytes Concurrency Level: 1000 Time taken for tests: 74.401 seconds Complete requests: 50000 Failed requests: 64 (Connect: 0, Receive: 0, Length: 32, Exceptions: 32) Keep-Alive requests: 49968 Total transferred: 384457439664 bytes HTML transferred: 384445547280 bytes Requests per second: 672.04 [#/sec] (mean) Time per request: 1488.010 [ms] (mean) Time per request: 1.488 [ms] (mean, across all concurrent requests) Transfer rate: 5046292.27 [Kbytes/sec] received
3、非本机测试
tomcat 30KB的结果为: Document Path: /index.html Document Length: 30877 bytes Concurrency Level: 1000 Time taken for tests: 13.396 seconds Complete requests: 50000 Failed requests: 0 Write errors: 0 Keep-Alive requests: 50000 Total transferred: 1569700000 bytes HTML transferred: 1543850000 bytes Requests per second: 3732.34 [#/sec] (mean) Time per request: 267.928 [ms] (mean) Time per request: 0.268 [ms] (mean, across all concurrent requests) Transfer rate: 114426.95 [Kbytes/sec] received nginx 30KB的结果为: Document Path: /index.html Document Length: 30877 bytes Concurrency Level: 1000 Time taken for tests: 13.469 seconds Complete requests: 50000 Failed requests: 0 Write errors: 0 Keep-Alive requests: 50000 Total transferred: 1555550000 bytes HTML transferred: 1543850000 bytes Requests per second: 3712.25 [#/sec] (mean) Time per request: 269.379 [ms] (mean) Time per request: 0.269 [ms] (mean, across all concurrent requests) Transfer rate: 112784.93 [Kbytes/sec] received
tomcat的压测情况
压测之前:
- Thread (reserved=242MB, committed=242MB) (thread #241) (stack: reserved=241MB, committed=241MB) (malloc=1MB #1210)
压测时:
- Thread (reserved=390MB, committed=390MB) (thread #387) (stack: reserved=387MB, committed=387MB) (malloc=1MB #1940) (arena=1MB #769)
注意 这是 tomcat 的配置事项:
tomcat: threads: max: 500 accept-count: 1000 max-connections: 20000 connection-timeout: 60000
但是需要注意,仅使用前端,几乎不会带来GC的现象。线程会有所增加, 应该是提高了 http的工作线程。