简体   繁体   English

Tomcat 第一次响应慢

[英]First response is slow Tomcat

I made a Java Web Application hosted by Tomcat.我制作了一个由 Tomcat 托管的 Java Web 应用程序。 It can be found on this URL .它可以在这个URL上找到。

The problem that I am experiencing is that whenever I visit the page the first time it takes about 10s while every time after it it takes only 100-500ms.我遇到的问题是,每当我第一次访问该页面时,大约需要 10 秒,而每次访问后仅需要 100-500 毫秒。 I would say the speed improvement is due to browser caching, but not really, when I refresh cache too ( ctrl + shift + r ) I get the same quick response.我会说速度的提高是由于浏览器缓存,但实际上并非如此,当我也刷新缓存时( ctrl + shift + r )我得到了同样的快速响应。

Then after some time, about 5 minutes I visit the page again and it's slow again.然后过了一段时间,大约 5 分钟,我再次访问该页面,它又变慢了。

You may try some tests yourself on the URL provided by changing the search parameter value to eg: 1050 , 1051 , 1052 , 2670 , 4000 , 2300 , 2200 .您可以通过将搜索参数值更改为例如: 1050105110522670400023002200来尝试对提供的 URL 进行一些测试。

Also the interesting fact I have spotted is that no matter how big payload (compare 1050 with 2300) the time is almost always the same approx.我发现的另一个有趣的事实是,无论有效载荷有多大(将 1050 与 2300 进行比较),时间几乎总是相同的。 9-10s. 9-10 秒。 So what I assume is that something like Java Server has got to get ready and that is what is taking time.所以我假设像 Java Server 这样的东西必须准备好,这需要时间。

EDIT:编辑:

I was first thinking it could be related to Java/Tomcat having to load some resources and then after some time +-3-5mins again because for some reason it unloaded it.我首先认为它可能与 Java/Tomcat 必须加载一些资源有关,然后在一段时间后再次加载 +-3-5 分钟,因为出于某种原因它卸载了它。 But as I wrote above, even if I change the URL query string (that causes a different SQL query during execution), it again loads long.但正如我上面所写,即使我更改了 URL 查询字符串(这会在执行期间导致不同的 SQL 查询),它也会再次加载很长时间。 Can the issue be related to the DB?这个问题可以与数据库有关吗? I am using MySQL.我正在使用 MySQL。

EDIT2:编辑2:

The reason why it's faster is most likely the server caching.它更快的原因很可能是服务器缓存。 I am 95% sure and that is because I made couple of experiments such as trying it on 2 computers etc. (no it's not browser caching).我有 95% 的把握,那是因为我做了几个实验,例如在 2 台计算机上尝试它等(不,它不是浏览器缓存)。 Then I realized that if it's fast only when it's cached, what takes so long is the actual .executeQuery line of code.然后我意识到,如果它只有在缓存时才快,那么实际的.executeQuery代码行需要这么长时间。 It takes 10s even though the exact same request through a client such as WorkBench takes only 0.285s.即使通过WorkBench等客户端发出的完全相同的请求仅需 0.285 秒,也需要 10 秒。 Therefore I am going to try to use a PreparedStatement or experiment further.因此,我将尝试使用PreparedStatement或进一步实验。

The content is 200kB in size.内容大小为 200kB。 You need to optimize it in the front and backend.您需要在前端和后端对其进行优化。

The main problem is in your backend.主要问题出在您的后端。 Check why it takes so long.检查为什么需要这么长时间。

In the frontend you can enable gzip compression.在前端,您可以启用 gzip 压缩。 Check the tomcat documentation on how to do it.查看 tomcat 文档了解如何操作。 This will reduce the size for the download.这将减少下载的大小。

The second time is probably faster for you due to browser caching.由于浏览器缓存,第二次对您来说可能更快。 Check the response code in Firebug, if the response is 304 it is cached.检查 Firebug 中的响应代码,如果响应是 304,则它被缓存。 Response 200 means it has been loaded from the server.响应 200 表示它已从服务器加载。

ORM systems (like Hibernate ) can cause significantly slow startup if you forget to turn off the initial modell-schema sync option in production environment, for example in Spring -> JPA -> Hibernate -> application-[server].yml->如果您忘记在生产环境中关闭初始 modell-schema 同步选项,例如在Spring -> JPA -> Hibernate -> application-[server].yml-> 中,ORM 系统(如Hibernate )可能会导致启动速度非常慢

spring:
    jpa:
        hibernate:
            ddl-auto: update

update none 更新

If your model didn't change, switch "update" to "none" in the production environment for faster startup. 如果您的模型没有改变,请在生产环境中将“更新”切换为“无”以加快启动速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM