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?
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 usinghttptrace.WithClientTrace()
.Here's an example function which gets a URL param, and prints the timing of getting it:
Example calling it:
Example output:
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.