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
+4.4k Javascript : Push notifications to browser with Push.js
+9.2k Golang : Reverse IP address for reverse DNS lookup example
+6.2k Golang : Print how to use flag for your application example
+4.2k Golang : Spell checking with ispell example
+3.9k Golang : Missing Subversion command
+17.5k Golang : How to run Golang application such as web server in the background or as daemon?
+4.5k Golang : Totalize or add-up an array or slice example
+5k Golang : Process json data with Jason package
+25.1k error: trying to remove "yum", which is protected
+24.2k Golang : Interpolating or substituting variables in string examples
+7.5k Golang : How to delete element(data) from map ?
+24.7k Golang : Get first few and last few characters from string