Last active
May 6, 2016 12:36
-
-
Save luisbebop/5909994 to your computer and use it in GitHub Desktop.
http benchmark tool
This file contains 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
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