Edit: The following answer precedes Go 1.7. Go 1.7 added HTTP tracing, so be sure to check out the new answer: Getting the TTFB value (time to the first byte) in golang
The original answer follows.
You can measure it by opening a TCP connection and βspeakingβ raw HTTP ( Wikipedia , rfc7230 , rfc7231 , rfc7232 , rfc7233 , rfc7234 , rfc7235 ). You establish a connection, send a request and start the timer here. And wait for an answer. In doing so, you will exclude DNS resolution and the time required to establish a connection.
Since you do not know if the server will start sending data immediately or only when everything is ready, and you do not have information about network delays, this will not be an accurate estimate.
I would measure in 2 points: when can I read the first byte and when everything is read:
conn, err := net.Dial("tcp", "google.com:80") if err != nil { panic(err) } defer conn.Close() conn.Write([]byte("GET / HTTP/1.0\r\n\r\n")) start := time.Now() oneByte := make([]byte,1) _, err = conn.Read(oneByte) if err != nil { panic(err) } log.Println("First byte:", time.Since(start)) _, err = ioutil.ReadAll(conn) if err != nil { panic(err) } log.Println("Everything:", time.Since(start))
Remarks:
It would be wise to measure from the 3rd moment: when all the response headers are read. This is defined as reading complete lines from a response (connection) and detecting an empty line. This empty line separates the response headers from the response body.
icza
source share