bcbishop
bcbishop

Reputation: 2313

Why it's taking longer for HttpHead to return than HttpGet

We have the following code, which later on replaced with HttpHead method as we only need to pull back the header info of our web pages. After the change, we noticed that, on average, it took longer time for the HttpHead to return than the HttpGet for same sets of webpages. Is it normal? What could be wrong here?

    HttpClient httpclient = new DefaultHttpClient();
    // the time it takes to open TCP connection.
    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, this.timeout);

    // timeout when server does not send data.
    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, this.timeout);

    // the get method
    HttpGet httpget = new HttpGet(url);

    HttpResponse response = httpclient.execute(httphead);

Upvotes: 1

Views: 147

Answers (1)

Stephen C
Stephen C

Reputation: 718906

Is it normal?

It certainly seems a bit peculiar.

What could be wrong here?

It is difficult to say. It would seem that the strange behavior is most likely on the server side. I would check the following:

  • Write a micro-benchmark that repeatedly GETs and HEADs the same page to make sure that the performance difference is real, and not an artifact of the way you measured it.
  • Use packet logger to look at what is actually being sent and received.
  • Check the server logs.
  • Profile the server code under load using your micro-benchmark.

One possible explanation is that the HEAD is loading data from a (slow) database or file system. The following GET could then be faster because the data has already been cached. (It could be explicit caching in the server code, the query caching in the back-end database, or file system caching.) You could test for this by seeing if a GET is slower if not preceded by a HEAD.

Upvotes: 2

Related Questions