Skip to content

Instantly share code, notes, and snippets.

@monmohan
Created July 11, 2020 16:59
Show Gist options
  • Save monmohan/2a967a5541d4a9863b8502726d02bfcf to your computer and use it in GitHub Desktop.
Save monmohan/2a967a5541d4a9863b8502726d02bfcf to your computer and use it in GitHub Desktop.
Gist for Blog
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"time"
)
func main() {
//Start a server that sleeps for 10 second before returning a response
slowServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
incoming, _ := httputil.DumpRequest(r, false)
fmt.Printf("Server: Incoming Request %s", string(incoming))
time.Sleep(10 * time.Second) // Do difficult Job
w.Write([]byte("Hello There!"))
}))
request, _ := http.NewRequest("GET", slowServer.URL, nil)
var (
resp *http.Response
err error
)
st := time.Now()
if resp, err = http.DefaultClient.Do(request); err != nil {
log.Fatalf("Error in request %s\n", err.Error())
}
body, err := ioutil.ReadAll(resp.Body)
fmt.Printf("Request to %s, Response = %s, Time Elapsed= %s\n", slowServer.URL, body, time.Now().Sub(st))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment