-
-
Save Jeongseup/7076c9794f62c1a93f2c3684ed71eb91 to your computer and use it in GitHub Desktop.
Send a bunch of HTTP requests via threads and goroutines
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" | |
"http" | |
"flag" | |
"runtime" | |
"bytes" | |
"log" | |
) | |
var ( | |
threads = flag.Int("threads", 1, "number of threads") | |
routines = flag.Int("goroutines", 1000, "number of goroutines") | |
count = flag.Int("count", 10, "number of HTTP goroutines per goroutine") | |
host = flag.String("host", "localhost:8080", "Hostname:port to connect to") | |
method = flag.String("method", "POST", "HTTP method to use") | |
path = flag.String("path", "/", "Path to request") | |
payload = flag.String("payload", "", "Payload of the request") | |
ctype = flag.String("content-type", "application/json", "Content-Type of payload") | |
) | |
func fetch(done chan<- bool) (okay bool) { | |
defer func() { | |
done <- okay | |
}() | |
url := &http.URL{ | |
Scheme: "http", | |
Host: *host, | |
Path: *path, | |
} | |
if len(url.Path) == 0 || url.Path[0] != '/' { | |
url.Path = "/" + url.Path | |
} | |
body := bytes.NewBufferString(*payload) | |
req, err := http.NewRequest(*method, url.String(), body) | |
if err != nil { | |
log.Printf("newrequest: %s", err) | |
return false | |
} | |
if len(*payload) > 0 { | |
req.Header.Set("Content-Type", *ctype) | |
} | |
resp, err := http.DefaultTransport.RoundTrip(req) | |
if err != nil { | |
log.Printf("roundtrip: %s", err) | |
return false | |
} | |
if resp.StatusCode != 200 { | |
log.Printf("response: %s", resp.Status) | |
return false | |
} | |
return true | |
} | |
func main() { | |
flag.Parse() | |
runtime.GOMAXPROCS(*threads) | |
done := make(chan bool) | |
for i := 0; i < *routines; i++ { | |
go func() { | |
for j := 0; j < *count; j++ { | |
fetch(done) | |
} | |
}() | |
} | |
errors := 0 | |
for i := 0; i < *routines * *count; i++ { | |
if !<-done { | |
errors++ | |
} | |
} | |
fmt.Printf("%d requests transmitted, %d errors; %.2f%% error rate\n", | |
*routines * *count, errors, 100*float64(errors)/float64(*routines * *count)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment