gpt4 book ai didi

java - 为什么tomcat比提供静态资源的nodejs快

转载 作者:行者123 更新时间:2023-11-28 22:00:56 24 4
gpt4 key购买 nike

有人告诉我,tomcat 在提供静态资源(例如 js/css/img)时速度很慢。 (我只熟悉nodejs)

我不明白为什么 tomcat 比 nodejs/nginx 慢。他们是否都应该使用内存缓存,或者至少在提供静态资源时使用非阻塞 io?


做了AB测试,结果出乎意料(tomcat比nodejs快)。我使用的是带有 Intel Core i5-5200 @ 2.20GHz 和 8GB RAM(戴尔笔记本)的 Windows 7 Pro。我的nodejs版本是v8.9.0,java版本是1.8.045,tomcat是5.5.17。

nodejs代码:

const path = require('path');
const Koa = require('koa');
const Router = require('koa-router');
const staticServer = require('koa-static');

const app = new Koa();
const router = new Router();

app.use(staticServer( path.join(__dirname, 'static')) );
app.listen(8001);

nodejs 结果是:

E:\soft-new\apache\Apache24\bin>ab -c 20 -n 1000 http://localhost:8001/benu_crm/a.html
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
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: 8001

Document Path: /benu_crm/a.html
Document Length: 3 bytes

Concurrency Level: 20
Time taken for tests: 0.679 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 209000 bytes
HTML transferred: 3000 bytes
Requests per second: 1472.67 [#/sec] (mean)
Time per request: 13.581 [ms] (mean)
Time per request: 0.679 [ms] (mean, across all concurrent requests)
Transfer rate: 300.57 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 1
Processing: 6 13 3.0 13 26
Waiting: 6 11 2.7 10 23
Total: 6 13 3.0 13 26

Percentage of the requests served within a certain time (ms)
50% 13
66% 14
75% 15
80% 15
90% 18
95% 20
98% 21
99% 23
100% 26 (longest request)

而 tomcat 结果是:(对于 tomcat,我只是制作了一个没有任何 java 代码的 a.html)

E:\soft-new\apache\Apache24\bin>ab -c 20 -n 1000 http://localhost:8050/benu_crm/a.html
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
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: Apache-Coyote/1.1
Server Hostname: localhost
Server Port: 8050

Document Path: /benu_crm/a.html
Document Length: 3 bytes

Concurrency Level: 20
Time taken for tests: 0.171 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 222000 bytes
HTML transferred: 3000 bytes
Requests per second: 5847.61 [#/sec] (mean)
Time per request: 3.420 [ms] (mean)
Time per request: 0.171 [ms] (mean, across all concurrent requests)
Transfer rate: 1267.74 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 2
Processing: 0 3 1.3 3 8
Waiting: 0 3 1.4 3 8
Total: 0 3 1.3 3 8

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

@EugèneAdell 谢谢。我将 a.html 的大小增加到 309,344 字节。然后tomcat 5.5.17和nodejs koa2差不多。稍后我会尝试NIO tomcat并把结果也放上去。但它仍然让我感到惊讶,因为 BIO tomcat 几乎与 koa2 相当。

nodejs 结果:

E:\soft-new\apache\Apache24\bin>ab -c 20 -n 1000 http://localhost:8001/benu_crm/a.html
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
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: 8001

Document Path: /benu_crm/a.html
Document Length: 309344 bytes

Concurrency Level: 20
Time taken for tests: 1.071 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 309555000 bytes
HTML transferred: 309344000 bytes
Requests per second: 933.65 [#/sec] (mean)
Time per request: 21.421 [ms] (mean)
Time per request: 1.071 [ms] (mean, across all concurrent requests)
Transfer rate: 282243.05 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 3
Processing: 8 21 12.0 20 386
Waiting: 2 10 2.2 9 23
Total: 8 21 12.0 20 386

Percentage of the requests served within a certain time (ms)
50% 20
66% 22
75% 23
80% 23
90% 25
95% 26
98% 29
99% 31
100% 386 (longest request)

tomcat 结果:

E:\soft-new\apache\Apache24\bin>ab -c 20 -n 1000 http://localhost:8050/benu_crm/a.html
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
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: Apache-Coyote/1.1
Server Hostname: localhost
Server Port: 8050

Document Path: /benu_crm/a.html
Document Length: 309344 bytes

Concurrency Level: 20
Time taken for tests: 0.916 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 309573000 bytes
HTML transferred: 309344000 bytes
Requests per second: 1091.64 [#/sec] (mean)
Time per request: 18.321 [ms] (mean)
Time per request: 0.916 [ms] (mean, across all concurrent requests)
Transfer rate: 330021.72 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.4 0 1
Processing: 4 18 3.6 17 45
Waiting: 0 2 2.8 1 22
Total: 4 18 3.6 18 45

Percentage of the requests served within a certain time (ms)
50% 18
66% 18
75% 19
80% 19
90% 21
95% 23
98% 27
99% 37
100% 45 (longest request)

最佳答案

感谢 tomcat-users邮件列表和 devshed 为此 benchmark ,良好的 Tomcat 性能更可能归功于 Java 运行时:

How is it possible for pure-Java Tomcat to serve static resource faster than Apache httpd ? The main reason we can think of: because Tomcat is written in Java and because Java bytecode can be natively compiled and highly optimized at runtime, well-written Java code can run very fast when it runs on a mature Java VM that implements many runtime optimizations, such as the Sun Hotspot JVM. After it runs and serves many requests, the JVM knows how to optimize it for that particular use on that particular hardware. On the other hand, Apache httpd is written in C, which is completely compiled ahead of runtime.

但是,由于您的测试是多次访问一个小文件,因此也可能是 Tomcat 的设计比 Node.js 的设计更高效,特别是对于“接受连接和开始服务”部分。网络捕获可能有助于了解 Node.js 中的这种延迟是从哪里来的

关于java - 为什么tomcat比提供静态资源的nodejs快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51062767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com