douwayuan3063 2018-01-03 12:08
浏览 118
已采纳

在golang中获取TTFB(到第一个字节的时间)值

I am trying to get value TTFB and Connect value

    c := exec.Command(
        "curl", "-w",
        "Connect: %{time_connect} TTFB: %{time_starttransfer} Total time: %{time_total}",
        "-o",
        "/dev/null",
        "http://my.domain/favicon.ico")

    cID, err := c.Run()
    fmt.Printf("%s", cID)

It prints a sting like

Connect: 0.205 TTFB: 0.353 Total time: 0.354

However, I need only the value of TTFB , Connect, Total time value in golang variable.

Also, Is there any way I can get the values without specifically using curl?

  • 写回答

1条回答 默认 最新

  • dongshen6060 2018-01-03 12:49
    关注

    There's builtin support for this since Go 1.7. Go 1.7 added HTTP Tracing, read blog post: Introducing HTTP Tracing

    You can specify callback functions which get called at the appropriate phases / points when making an HTTP(S) request. You can specify your callback functions by creating a value of httptrace.ClientTrace, and "arm" it using httptrace.WithClientTrace().

    Here's an example function which gets a URL param, and prints the timing of getting it:

    func timeGet(url string) {
        req, _ := http.NewRequest("GET", url, nil)
    
        var start, connect, dns, tlsHandshake time.Time
    
        trace := &httptrace.ClientTrace{
            DNSStart: func(dsi httptrace.DNSStartInfo) { dns = time.Now() },
            DNSDone: func(ddi httptrace.DNSDoneInfo) {
                fmt.Printf("DNS Done: %v
    ", time.Since(dns))
            },
    
            TLSHandshakeStart: func() { tlsHandshake = time.Now() },
            TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
                fmt.Printf("TLS Handshake: %v
    ", time.Since(tlsHandshake))
            },
    
            ConnectStart: func(network, addr string) { connect = time.Now() },
            ConnectDone: func(network, addr string, err error) {
                fmt.Printf("Connect time: %v
    ", time.Since(connect))
            },
    
            GotFirstResponseByte: func() {
                fmt.Printf("Time from start to first byte: %v
    ", time.Since(start))
            },
        }
    
        req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
        start = time.Now()
        if _, err := http.DefaultTransport.RoundTrip(req); err != nil {
            log.Fatal(err)
        }
        fmt.Printf("Total time: %v
    ", time.Since(start))
    }
    

    Example calling it:

    timeGet("https://google.com")
    

    Example output:

    DNS Done: 7.998465ms
    Connect time: 12.677085ms
    TLS Handshake: 128.65394ms
    Time from start to first byte: 176.461087ms
    Total time: 176.723402ms
    

    Also be sure to check out github.com/davecheney/httpstat which gives you a CLI utility that also uses the httptracing package under the hood.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算