Skip to content

Instantly share code, notes, and snippets.

@ayufan
Last active April 25, 2017 09:33
Show Gist options
  • Save ayufan/6da46a9b4b540a25500d3e87ae7beaf0 to your computer and use it in GitHub Desktop.
Save ayufan/6da46a9b4b540a25500d3e87ae7beaf0 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
var (
FURL = flag.String(
"url",
"https://52.167.219.168",
"Destination URL, supported schemes: http, https. Example: https://gitlab.com",
)
FT = flag.Int(
"time",
300,
"Period of time, in seconds. Example: 300",
)
)
func testRequests(client *http.Client, requests int, discard bool) {
for requests > 0 {
requests--
started := time.Now()
resp, err := client.Get(*FURL)
if err != nil {
fmt.Printf("Could not get data from URL %s: %s\n", *FURL, err.Error())
continue
}
if discard {
io.Copy(ioutil.Discard, resp.Body)
}
resp.Body.Close()
fmt.Printf(
"Response time is %.3f seconds\n",
float32(time.Since(started)/time.Millisecond)/1000,
)
}
}
func main() {
flag.Parse()
if !strings.HasPrefix(*FURL, "https://") && !strings.HasPrefix(*FURL, "http://") {
fmt.Println("URL must have one of the prefixes 'https://' or 'http://'.")
os.Exit(-1)
}
if *FT <= 0 {
fmt.Println("Preiod of time must be grather than zero.")
os.Exit(-1)
}
fmt.Printf("Calculating response time from %s during %d seconds...\n", *FURL, *FT)
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{
Transport: tr,
}
fmt.Println()
fmt.Println("No resumption")
testRequests(client, 5, false)
tr = &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
ClientSessionCache: tls.NewLRUClientSessionCache(1000),
},
}
client = &http.Client{
Transport: tr,
}
fmt.Println()
fmt.Println("With resumption")
testRequests(client, 5, false)
tr = &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client = &http.Client{
Transport: tr,
}
fmt.Println()
fmt.Println("With keep-alive")
testRequests(client, 5, true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment