I am creating a small application to determine how long it takes to load an HTML document, checking every x seconds.
I use jsoup in a loop:
Connection.Response response = null; for (int i = 0; i < totalGets; i++) { long startTime = System.currentTimeMillis(); try { response = Jsoup.connect(url) .userAgent(USER_AGENT) //just using a Firefox user-agent .timeout(30_000) .execute(); } catch (IOException e) { if (e.getMessage().contains("connect timed out")) { System.out.println("Request timed out after 30 seconds!"); } } long currentTime = System.currentTimeMillis(); System.out.println("Response time: " + (currentTime - startTime) + "ms" + "\tResponse code: " + response.statusCode()); sleep(2000); }
The problem I am facing is that the very first jsoup connection execution is always slower than all subsequent times, no matter which website.
Here is my conclusion at https://www.google.com
Response time: 934ms Response code: 200 Response time: 149ms Response code: 200 Response time: 122ms Response code: 200 Response time: 136ms Response code: 200 Response time: 128ms Response code: 200
Here is what I get http://stackoverflow.com
Response time: 440ms Response code: 200 Response time: 182ms Response code: 200 Response time: 187ms Response code: 200 Response time: 193ms Response code: 200 Response time: 185ms Response code: 200
Why is it always faster after the first connection? Is there a better way to determine the loading speed of a document?
source share