Skip to content

Instantly share code, notes, and snippets.

@haggen
Created May 27, 2019 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haggen/2325750beb87dc61a2182ef318db6f67 to your computer and use it in GitHub Desktop.
Save haggen/2325750beb87dc61a2182ef318db6f67 to your computer and use it in GitHub Desktop.
Time HTTP requests in Go.
package main
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httptrace"
"time"
)
func main() {
req, _ := http.NewRequest("GET", "https://focusfoto.com.br", nil)
trace := &httptrace.ClientTrace{
GetConn: func(_ string) {
fmt.Printf("%30s: %s\n", "GetConn", time.Now())
},
GotConn: func(_ httptrace.GotConnInfo) {
fmt.Printf("%30s: %s\n", "GotConn", time.Now())
},
DNSStart: func(_ httptrace.DNSStartInfo) {
fmt.Printf("%30s: %s\n", "DNSStart", time.Now())
},
DNSDone: func(_ httptrace.DNSDoneInfo) {
fmt.Printf("%30s: %s\n", "DNSDone", time.Now())
},
ConnectStart: func(_, _ string) {
fmt.Printf("%30s: %s\n", "ConnectStart", time.Now())
},
ConnectDone: func(_, _ string, _ error) {
fmt.Printf("%30s: %s\n", "ConnectDone", time.Now())
},
TLSHandshakeStart: func() {
fmt.Printf("%30s: %s\n", "TLSHandshakeStart", time.Now())
},
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) {
fmt.Printf("%30s: %s\n", "TLSHandshakeDone", time.Now())
},
WroteHeaders: func() {
fmt.Printf("%30s: %s\n", "WroteHeaders", time.Now())
},
WroteRequest: func(_ httptrace.WroteRequestInfo) {
fmt.Printf("%30s: %s\n", "WroteRequest", time.Now())
},
GotFirstResponseByte: func() {
fmt.Printf("%30s: %s\n", "GotFirstResponseByte", time.Now())
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
resp, _ := http.DefaultClient.Do(req)
fmt.Printf("%30s %d", "", resp.StatusCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment