Skip to content

Instantly share code, notes, and snippets.

@gerep
Forked from luisbebop/httpbenchmark.go
Last active May 6, 2016 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerep/45874431d1edd8b290e7b853399cef67 to your computer and use it in GitHub Desktop.
Save gerep/45874431d1edd8b290e7b853399cef67 to your computer and use it in GitHub Desktop.
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