我正在创建一个小应用程序,用于衡量HTML文档加载所需的时间,每隔x秒检查一次.
我在循环中使用jsoup:
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); }
我遇到的问题是,无论什么网站,jsoup连接的第一次执行总是慢于后续的一次.
这是我的输出 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
这就是我的成就 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
为什么第一次连接后它总是更快?有没有更好的方法来确定文档的加载速度?