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.3k Swift : Convert (cast) Float to Int or Int32 value
+7.6k Golang : Accessing dataframe-go element by row, column and name example
+38.5k Golang : Converting a negative number to positive number
+28.1k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+8.4k Golang : Qt splash screen with delay example
+17.8k Golang : Parse date string and convert to dd-mm-yyyy format
+13.6k Golang : Get constant name from value
+8.3k Golang : Check from web if Go application is running or not
+10.6k Golang : cannot assign type int to value (type uint8) in range error
+19.5k Golang : Get RGBA values of each image pixel
+9k Golang : Sort lines of text example
+13.7k Golang : Count number of runes in string