Created
July 11, 2020 16:59
-
-
Save monmohan/2a967a5541d4a9863b8502726d02bfcf to your computer and use it in GitHub Desktop.
Gist for Blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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