从 API Groovy 获取第一个字节的时间

问题描述

我正在编写一个脚本来获取 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

但是我不确定准确性...

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...