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
+7.4k Golang : Mapping Iban to Dunging alphabets
+8.6k Golang : Find network service name from given port and protocol
+10.2k Golang : Select region of interest with mouse click and crop from image
+12.4k Swift : Convert (cast) Int or int32 value to CGFloat
+5.9k Golang : Convert Chinese UTF8 characters to Pin Yin
+21.5k Golang : How to reverse slice or array elements order
+28.2k Get file path of temporary file in Go
+15.2k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+17k Golang : When to use init() function?
+27.8k Golang : Connect to database (MySQL/MariaDB) server
+11.4k SSL : The certificate is not trusted because no issuer chain was provided
+22.4k Golang : Round float to precision example