问题描述
我正在编写一个脚本来获取 URL 的 TTFB。这个 getResponseCode 方法为我提供了 URL 的状态代码。建立连接后,我可以使用什么方法或公式来获取TTFB?
HttpURLConnection 连接
try {
URL url = new URL('www.xyx.com')
connection = (HttpURLConnection)url.openConnection()
connection.setRequestMethod("GET")
connection.connect()
println(connection.getResponseCode())
}
catch(Exception ex) {
return 0
}
finally {
connection.disconnect()
}
解决方法
显而易见的方法
long t0 = System.getTimeMillis()
connection.getInputStream()?.read()
long t1 = System.getTimeMillis()
long ttfb = t1 - t0
但是我不确定准确性...