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
+14.5k Golang : Parsing or breaking down URL
+22k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+31.7k Golang : bufio.NewReader.ReadLine to read file line by line
+9.5k Golang : Web(Javascript) to server-side websocket example
+7.6k Android Studio : How to detect camera, activate and capture example
+20.3k Golang : Count number of digits from given integer value
+5.8k Golang : Detect words using using consecutive letters in a given string
+8.2k Golang : Tell color name with OpenCV example
+9.6k Golang : Changing a RGBA image number of channels with OpenCV
+9.1k Golang : Inject/embed Javascript before sending out to browser example
+7k Golang : Calculate BMI and risk category
+5.4k Golang : Pad file extension automagically