Java - the first HTTP request to a specific host is much slower

I am writing a tool to compare with a web application. The problem that I encountered is that the first request to the server always takes significantly longer than subsequent requests. I ran into this problem with apache http client 3.x, 4.x and google http client . Apache http client 4.x shows the biggest difference (the first request takes about seven times longer than subsequent ones). For Google and 3.x it is about 3 times longer.

My tool should be able to compare concurrent requests with threads. I cannot use one instance, for example. HttpClient and call it from all threads, because it throws a Concurrent exception. Therefore, I have to use a separate instance in each thread, which will only perform one request. This dramatically changes the overall results.

I do not understand this behavior. I do not think that this is due to the caching mechanism on the server, because a) the web application in question does not use caching (as far as I know), and b) this effect also manifests itself upon the first request to www.hostxy.com and subsequently www. hostxy.com/my/webapp.

I use System.nanoTime () just before and after calling client.execute(get) or get.execute() respectively.

Does anyone have an idea where this behavior comes from? Do these httpclients perform caching? I would be very grateful for any hints.

+4
source share
4 answers

Read the following: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html for pooling.

Your first connection probably takes the longest time because its connection is Connect: keep-alive, so the following connections can reuse this connection as soon as it is established. This is a fair assumption.

0
source

Did you first get into JSP after starting the server? If the server starts the working directory at each start, then the first hit of JSPs is compiled, and this takes a lot of time.

The first transaction is also executed: if the transaction uses the CA trust store, it will be loaded and cached.

0
source

If your problem is that “the first HTTP request to a specific host is much slower,” the cause of this symptom may be on the server, while you are concerned about the client.

If the “specific host” you are calling is a Google App Engine application (or any other cloud-based Plattform), it’s normal that the first call to this application will make you wait a little longer. This is because Google was idle at rest.

After a recent call (which usually takes longer to answer), subsequent calls have faster answers because server instances are awakened.

Take a look at this: Google App Engine application is very slow

Hope this helps you, good luck!

0
source

All Articles