Golang : Measure http.Get() execution time
There are times where you need to measure how long a http.Get() response time takes for debugging reason or network optimization purpose. It is kinda like ping command and in this tutorial, we will learn how to implement the code in Golang to measure the time taken for http.Get().
Here you go :
package main
import (
"fmt"
"os"
"time"
"net/http"
)
func main() {
start := time.Now()
url := "http://www.golang.org"
result, err := http.Get(url)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer result.Body.Close()
elapsed := time.Since(start).Seconds()
fmt.Printf("http.Get to %s took %v seconds \n", url, elapsed)
}
Sample output :
http.Get to http://www.golang.org took 1.227666872 seconds
Reference :
https://www.socketloop.com/tutorials/golang-calculate-elapsed-run-time
See also : Golang : calculate elapsed run time
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+12.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+16.9k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+14k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+12.8k Python : Convert IPv6 address to decimal and back to IPv6
+23.4k Golang : Check if element exist in map
+25.6k Golang : missing Mercurial command
+13.5k Golang : Query string with space symbol %20 in between
+4.9k Nginx and PageSpeed build from source CentOS example
+21.2k Golang : How to get time zone and load different time zone?
+20.6k Golang : Saving private and public key to files
+11.6k How to tell if a binary(executable) file or web application is built with Golang?