Nginx与Tomcat作为前端服务器的性能比较

nginx,tomcat,作为,前端,服务器,性能,比较 · 浏览次数 : 342

小编点评

## Nginx vs Tomcat 30KB 文件测试结果摘要 **Nginx 测试结果:** * 30KB 文件测试结果显示 Transfer rate 接近 135K,远超过 Tomcat 30KB 测试的结果 (201K)。 * 30KB 文件测试结果显示 Time per request 更低,表明 Nginx 对请求处理的效率更高。 * 30KB 文件测试结果显示线程数略低于 Tomcat,但明显超过 Tomcat 的线程数 (500)。 **Tomcat 测试结果:** * 30KB 文件测试结果显示 Transfer rate 接近 112K,略低于 Nginx 30KB 测试的结果 (114K)。 * 30KB 文件测试结果显示 Time per request 更高,表明 Tomcat 对请求处理的效率更低。 * 30KB 文件测试结果显示线程数高于 Nginx,但依然低于 Nginx 的线程数 (500)。 **总结:** * 30KB 文件测试结果表明 Nginx 性能更优于 Tomcat。 * 30KB 文件测试结果也说明 Nginx 对请求处理的效率更高,线程数也更多。 * 这可以解释 Nginx 在生产环境中性能优越的原因。

正文

Nginx与Tomcat作为前端服务器的性能比较


摘要

最近总遇到使用tomcat还是使用nginx进行前端文件访问的争论
想着出差周末在酒店, 可以自己进行一下简单的测试. 
希望能够对未来的工作进行一些指导. 

测试脚本

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.010

测试结果简要分析

Nginx在处理静态文件 在比较零散时 应该至少比tomcat要好 50%
但是如果有网络瓶颈时, 可能不会有这样的效果
但是同时发现. java会开启大量的http线程进行相应
内存占用会比较高, 一个线程至少1MB的栈区.
同时发现JVM几乎不会有堆区GC的现象. 

另外nginx在进行压测时CPU的使用量是很低. 压测java时CPU是能到 30%
但是nginx的CPU使用量比较低. 

详细结果信息

说明: 配置相同
tomcat: 采用产品直接运行的界面进行测试
nginx:  采用将文件放到 html目录下面进行测试
测试说明 30KB小文件和 7.5MB的大文件. 
直接在物理机器上面进行测试

使用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


使用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

非本机测试

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的工作线程. 

与Nginx与Tomcat作为前端服务器的性能比较相似的内容:

Nginx与Tomcat作为前端服务器的性能比较

Nginx与Tomcat作为前端服务器的性能比较 摘要 最近总遇到使用tomcat还是使用nginx进行前端文件访问的争论 想着出差周末在酒店, 可以自己进行一下简单的测试. 希望能够对未来的工作进行一些指导. 测试脚本 30KB 文件测试 ab -c 1000 -n 50000 -k http:/

长连接与短连接以及线程数相关的比较

长连接与短连接以及线程数相关的比较 摘要 对比了nginx与tomcat作为静态业务服务的性能之后 突然又想比较一下 长连接与否的性能情况. 很多时候恰当的配置还是非常重要的. 测试方法 分别使用长连接和短连接进行测试工作. 主要命令为: 短连接 ab -c 1000 -n 50000 -r htt

HTTPS下tomcat与nginx的前端性能比较

HTTPS下tomcat与nginx的前端性能比较 摘要 之前比较http的web服务器的性能. 发现nginx 比 tomcat 要好 50% 然后想到, https的情况下不知道两者有什么区别 所以准备再尝试一下. 换用https进行检查. Springboot的设置 server: ssl:

ngix安装与使用

```textile 主要是nginx的安装使用 1. 安装nginx(以及两个tomcat) 2. 使用nginx(测试负载均衡) 想要搭建的测试环境, 1.两个tomcat, 端口分别是80和8090(因为之前安装过一次端口为80的tomcat) 2.nginx端口为8088 ``` ![ima

[转帖]OpenResty编译安装

https://www.cnblogs.com/lizexiong/p/15012869.html OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 We

每个后端都应该了解的OpenResty入门以及网关安全实战

简介 在官网上对 OpenResty 是这样介绍的(http://openresty.org): “OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 We

[转帖]nginx源码编译及优化

Apache与nginx的区别 apache: 进程,稳定模块超多,基本想到的都可以找到少bug ,nginx 的bug 相对较多 nginx: 线程,快,不稳定。多线程是共享的,一个线程出问题,其他的也会受牵连。7层调度,反向代理能力强。CDN这块nginx也用的多轻量级,同样起web 服务,比a

[转帖]Nginx 使用与异常处理

http://jartto.wang/2017/04/15/nginx-exception-handling/ 以前总是偷懒使用 Http-Server 来启动一个本地服务,后来花时间学习了一下 Nginx,感觉挺好用。总结整理一下,就当打点存档了。 一、简单介绍 Nginx — Ngine X,是

[转帖]Nginx优化与防盗链

目录 一、配置Nginx隐藏版本号1、第一种方法修改配置文件2、第二种方法修改源码文件,重新编译安装 二、修改Nginx用户与组三、配置Nginx网页缓存时间四、实现Nginx的日志分割五、配置Nginx实现连接超时六、更改Nginx运行进程数七、配置Nginx实现网页压缩功能八、配置Nginx防盗

TCP内核参数与Nginx配置的简单测试

背景 昨天晚上整理了下几个TCP内核的参数. 学习到了一点内核参数的影响. 但是因为时间比较晚了没有继续钻研与nginx的关系 今天想着继续研究一下TCP的部分参数与nginx的关系 每个系统都不一样. 结果可能跟内核版本和内核参数强相关. 我这里用的是基于ARM的银河麒麟 还有基于x86的Open