Last active
December 3, 2015 22:53
-
-
Save jmervine/ec366538115d0d9e4a66 to your computer and use it in GitHub Desktop.
request something over and over
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"net/http" | |
"os" | |
"runtime" | |
"sync" | |
"time" | |
) | |
// limitations | |
// - only does get request | |
// - no custom headers | |
// - http only, no https | |
var ( | |
concurent int | |
conns int | |
path string | |
wg sync.WaitGroup | |
) | |
func main() { | |
flag.IntVar(&conns, "n", 1, "number of total connections") | |
flag.IntVar(&concurent, "c", 1, "number of concurrent connections") | |
flag.StringVar(&path, "p", "", "endpoint to hit") | |
flag.Parse() | |
if path == "" { | |
fmt.Println("'-p' option is required, see '-help' for options") | |
os.Exit(1) | |
} | |
runtime.GOMAXPROCS(concurent) | |
fmt.Printf("args: -n %d -c %d -p %q\n", conns, concurent, path) | |
for i := 0; i < conns; i++ { | |
wg.Add(1) | |
go func(i int, path string) { | |
defer func() { | |
wg.Done() | |
}() | |
begin := time.Now() | |
r, e := http.Get(path) | |
if e != nil { | |
panic(e) | |
} | |
fmt.Printf("%s [response %q] took %v\n", path, r.Status, time.Since(begin)) | |
}(i, path) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment