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
+19.3k Golang : Delete item from slice based on index/key position
+6.3k Golang : Get missing location after unmarshal binary and gob decode time.
+51.4k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+7.3k Golang : Null and nil value
+13.1k Golang : Calculate elapsed years or months since a date
+5k Nginx and PageSpeed build from source CentOS example
+5.5k Golang : Get S3 or CloudFront object or file information
+19.4k Golang : Display list of time zones with GMT
+13.5k Golang : Get constant name from value
+22.6k Golang : How to read JPG(JPEG), GIF and PNG files ?
+10.8k Golang : Resolve domain name to IP4 and IP6 addresses.
+11.1k Golang : Replace a parameter's value inside a configuration file example