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
+5.4k Golang : Pad file extension automagically
+7.9k Javascript : How to check a browser's Do Not Track status?
+14.6k Golang : How to get URL port?
+11.5k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+5.9k Cash Flow : 50 days to pay your credit card debt
+24k Golang : Fix type interface{} has no field or no methods and type assertions example
+20.2k Golang : Count number of digits from given integer value
+55.4k Golang : Unmarshal JSON from http response
+16k Golang : Get file permission
+4.1k Detect if Google Analytics and Developer Media are loaded properly or not
+7.5k Golang : Shuffle strings array
+10.4k Golang : cannot assign type int to value (type uint8) in range error