HTTP load generator, ApacheBench (ab) replacement
Go
Merge branch 'master' of github.com:rakyll/boom
Burcu Dogan6f0569e79ac7
diff --cc boom.goindex 8334093,1f86b0b..986d89a--- a/boom.go+++ b/boom.go@@@ -28,26 -28,26 +29,31 @@@ import"github.com/rakyll/boom/boomer")+ const (+ headerRegexp = "^([\\w-]+):\\s*(.+)"+ authRegexp = "^([\\w-\\.]+):(.+)"+ )+var (- method = flag.String("m", "GET", "")- flagMethod = flag.String("m", "GET", "")- flagHeaders = flag.String("h", "", "")- flagD = flag.String("d", "", "")- flagAccept = flag.String("A", "", "")- flagType = flag.String("T", "text/html", "")- flagAuth = flag.String("a", "", "")- flagInsecure = flag.Bool("allow-insecure", false, "")- flagOutput = flag.String("o", "", "")- flagProxyAddr = flag.String("x", "", "")-- flagC = flag.Int("c", 50, "")- flagN = flag.Int("n", 200, "")- flagQ = flag.Int("q", 0, "")- flagT = flag.Int("t", 0, "")++ m = flag.String("m", "GET", "")+ headers = flag.String("h", "", "")+ body = flag.String("d", "", "")+ accept = flag.String("A", "", "")+ contentType = flag.String("T", "text/html", "")+ authHeader = flag.String("a", "", "")++ output = flag.String("o", "", "")++ c = flag.Int("c", 50, "")+ n = flag.Int("n", 200, "")+ q = flag.Int("q", 0, "")+ t = flag.Int("t", 0, "")+ cpus = flag.Int("cpus", runtime.GOMAXPROCS(-1), "")++ insecure = flag.Bool("allow-insecure", false, "")+ disableCompression = flag.Bool("disable-compression", false, "")+ disableKeepAlives = flag.Bool("disable-keepalive", false, "")+ proxyAddr = flag.String("x", "", ""))var usage = `Usage: boom [options...] <url>@@@ -103,12 -99,12 +109,12 @@@ func main()usageAndExit("")}- n := *flagN- c := *flagC- q := *flagQ- t := *flagT++ runtime.GOMAXPROCS(*cpus)+ num := *n+ conc := *c- qps := *q- timout := *t++ q := *q- if n <= 0 || c <= 0 {+ if num <= 0 || conc <= 0 {usageAndExit("n and c cannot be smaller than 1.")}@@@ -124,21 -116,20 +126,20 @@@header http.Header = make(http.Header))- m = strings.ToUpper(*method)- method = strings.ToUpper(*flagMethod)++ method = strings.ToUpper(*m)url, originalHost = resolveUrl(flag.Args()[0])// set content-type- header.Set("Content-Type", *flagType)+ header.Set("Content-Type", *contentType)// set any other additional headers- if *flagHeaders != "" {- headers := strings.Split(*flagHeaders, ";")+ if *headers != "" {+ headers := strings.Split(*headers, ";")for _, h := range headers {- re := regexp.MustCompile("([\\w|-]+):(.+)")- matches := re.FindAllStringSubmatch(h, -1)- if len(matches) < 1 {- usageAndExit("")+ match, err := parseInputWithRegexp(h, headerRegexp)+ if err != nil {+ usageAndExit(err.Error())}- header.Set(matches[0][1], matches[0][2])+ header.Set(match[1], match[2])}}@@@ -147,39 -138,35 +148,38 @@@}// set basic auth if set- if *flagAuth != "" {- match, err := parseInputWithRegexp(*flagAuth, authRegexp)+ if *authHeader != "" {- re := regexp.MustCompile("([\\w|\\-|_|\\.]+):(\\w+)")- matches := re.FindAllStringSubmatch(*authHeader, -1)- if len(matches) < 1 {- usageAndExit("")++ match, err := parseInputWithRegexp(*authHeader, authRegexp)+ if err != nil {+ usageAndExit(err.Error())}- username = matches[0][1]- password = matches[0][2]+ username, password = match[1], match[2]}- if *flagOutput != "csv" && *flagOutput != "" {+ if *output != "csv" && *output != "" {usageAndExit("Invalid output type.")}(&boomer.Boomer{Req: &boomer.ReqOpts{- Method: m,+ Method: method,Url: url,- Body: *flagD,+ Body: *body,Header: header,Username: username,Password: password,OriginalHost: originalHost,},- N: n,- C: c,- Qps: q,- Timeout: t,- AllowInsecure: *flagInsecure,- Output: *flagOutput,- ProxyAddr: *flagProxyAddr}).Run()+ N: num,+ C: conc,- Qps: qps,- Timeout: timout,++ Qps: q,++ Timeout: *t,+ AllowInsecure: *insecure,+ DisableCompression: *disableCompression,+ DisableKeepAlives: *disableKeepAlives,++ ProxyAddr: *proxyAddr,+ Output: *output,- ProxyAddr: *proxyAddr}).Run()++ }).Run()}// Replaces host with an IP and returns the provided@@@ -235,3 -222,13 +235,12 @@@ func usageAndExit(message string)fmt.Fprintf(os.Stderr, "\n")os.Exit(1)}++ func parseInputWithRegexp(input, regx string) (matches []string, err error) {+ re := regexp.MustCompile(regx)+ matches = re.FindStringSubmatch(input)+ if len(matches) < 1 {+ err = errors.New("Could not parse provided input")+ }-+ return+ }diff --cc boom_test.goindex dbc24d1,3e7bac4..15fb560--- a/boom_test.go+++ b/boom_test.go@@@ -27,7 -27,7 +27,7 @@@ func (r *mockDnsResolver) Lookup(host s}func TestParseUrl(t *testing.T) {-- defaultDnsResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}++ defaultDNSResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}u, _ := resolveUrl("http://google.com:80/path/to/resource?q=rawquery")if u != "http://[2a00:1450:400a:806::1007]:80/path/to/resource?q=rawquery" {t.Errorf("Problem during url parsing, %v is found.", u)@@@ -35,7 -35,7 +35,7 @@@}func TestParseUrl_IPv4(t *testing.T) {-- defaultDnsResolver = &mockDnsResolver{Addr: "127.0.0.1"}++ defaultDNSResolver = &mockDnsResolver{Addr: "127.0.0.1"}u, s := resolveUrl("http://google.com")if s != "google.com" {t.Errorf("Original server name doesn't match with google.com, %v is found.", s)@@@ -46,7 -46,7 +46,7 @@@}func TestParseUrl_IPv4AndPort(t *testing.T) {-- defaultDnsResolver = &mockDnsResolver{Addr: "127.0.0.1"}++ defaultDNSResolver = &mockDnsResolver{Addr: "127.0.0.1"}u, s := resolveUrl("http://google.com:80")if s != "google.com:80" {t.Errorf("Original server name doesn't match with google.com, %v is found.", s)@@@ -57,7 -57,7 +57,7 @@@}func TestParseUrl_IPv6(t *testing.T) {-- defaultDnsResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}++ defaultDNSResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}u, s := resolveUrl("http://google.com")if s != "google.com" {t.Errorf("Original server name doesn't match with google.com, %v is found.", s)@@@ -68,7 -68,7 +68,7 @@@}func TestParseUrl_IPv6AndPort(t *testing.T) {-- defaultDnsResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}++ defaultDNSResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}u, s := resolveUrl("http://google.com:80")if s != "google.com:80" {t.Errorf("Original server name doesn't match with google.com, %v is found.", s)