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
+6.6k Mac OSX : Find large files by size
+9.2k Golang : Qt Yes No and Quit message box example
+24.2k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+13.6k Golang : Gin framework accept query string by post request example
+21.8k Golang : Match strings by wildcard patterns with filepath.Match() function
+9.7k Golang : Sort and reverse sort a slice of integers
+20.4k Golang : Saving private and public key to files
+16.5k Golang : Set up source IP address before making HTTP request
+16.8k Golang : Get number of CPU cores
+10.2k Golang : Generate random integer or float number
+6.6k Get Facebook friends working in same company