Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
http benchmark tool
import (
"flag"
"fmt"
"net/http"
)
var (
n = flag.Int("n", 100, "number of GETs to run")
s = flag.Bool("s", false, "whether to use HTTPS")
c = flag.Bool("c", false, "whether to run concurrently")
u = flag.String("u", "google.com", "URL to fetch, without scheme")
)
func main() {
flag.Parse()
out := make(chan error, *n)
var url string
if *s {
url = fmt.Sprintf("https://%v", *u)
} else {
url = fmt.Sprintf("http://%v", *u)
}
for i := 0; i < *n; i++ {
f := func() {
res, err := http.Get(url)
if res != nil {
res.Body.Close()
}
if err != nil {
out <- err
} else {
out <- nil
}
}
if *c {
go f()
} else {
f()
}
}
failed := 0
for i := 0; i < *n; i++ {
err := <-out
if err != nil {
fmt.Println(err)
failed++
}
}
fmt.Printf("%d/%d failed\n", failed, *n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment