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
+8k Golang : Sort and reverse sort a slice of floats
+13.5k Golang : How to add color to string?
+12.6k Golang : Force download file example
+6.8k Golang : Combine slices but preserve order example
+7.1k Golang : Accept any number of function arguments with three dots(...)
+15.2k Golang : Upload/Receive file progress indicator
+25.9k Golang : Smarter Error Handling with strings.Contains()
+14.8k Golang : How to log each HTTP request to your web server?
+15.6k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+22.2k Golang : Change file read or write permission example
+4.3k PHP : Proper way to get UTF-8 character or string length
+7.8k PHP : Get coordinates latitude/longitude from string