简体   繁体   中英

First response is slow Tomcat

I made a Java Web Application hosted by Tomcat. It can be found on this 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. 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.

Then after some time, about 5 minutes I visit the page again and it's slow again.

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 .

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. 9-10s. So what I assume is that something like Java Server has got to get ready and that is what is taking time.

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. 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. Can the issue be related to the DB? I am using MySQL.

EDIT2:

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). Then I realized that if it's fast only when it's cached, what takes so long is the actual .executeQuery line of code. It takes 10s even though the exact same request through a client such as WorkBench takes only 0.285s. Therefore I am going to try to use a PreparedStatement or experiment further.

The content is 200kB in size. 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. Check the tomcat documentation on how to do it. 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. Response 200 means it has been loaded from the server.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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