HTTP load generator, ApacheBench (ab) replacement
Go
Request should not be reused.
Burcu Dogan82937a0d17b0
diff --git a/boom.go b/boom.goindex bbbeaa8..cd241cc 100644--- a/boom.go+++ b/boom.go@@ -104,15 +104,19 @@ func main() {n = c}- url := flag.Args()[0]- method := strings.ToUpper(*flagMethod)+ var (+ url, method, serverName string+ // Username and password for basic auth+ username, password string+ // request headers+ header http.Header = make(http.Header)+ )- ipResolvedUrl, serverName := newURL(url)- req, _ := http.NewRequest(- method, ipResolvedUrl.String(), strings.NewReader(*flagD))- // set content-type- req.Header.Set("Content-Type", *flagType)+ method = strings.ToUpper(*flagMethod)+ url, serverName = resolveUrl(flag.Args()[0])+ // set content-type+ header.Set("Content-Type", *flagType)// set any other additional headersif *flagHeaders != "" {headers := strings.Split(*flagHeaders, ";")@@ -122,7 +126,7 @@ func main() {if len(matches) < 1 {usageAndExit("")}- req.Header.Set(matches[0][1], matches[0][2])+ header.Set(matches[0][1], matches[0][2])}}@@ -133,7 +137,8 @@ func main() {if len(matches) < 1 {usageAndExit("")}- req.SetBasicAuth(matches[0][1], matches[0][2])+ username = matches[0][1]+ password = matches[0][2]}if *flagOutput != "csv" && *flagOutput != "" {@@ -141,14 +146,21 @@ func main() {}(&commands.Boom{- N: n,- C: c,- Qps: q,- Timeout: t,- Req: req,- OrigServerName: serverName,- AllowInsecure: *flagInsecure,- Output: *flagOutput}).Run()+ Req: &commands.ReqOpts{+ Method: method,+ Url: url,+ Body: *flagD,+ Header: header,+ Username: username,+ Password: password,+ ServerName: serverName,+ },+ N: n,+ C: c,+ Qps: q,+ Timeout: t,+ AllowInsecure: *flagInsecure,+ Output: *flagOutput}).Run()}// Replaces host with an IP and returns the provided@@ -163,7 +175,7 @@ func main() {// <schema>://google.com[:port]// <schema>://173.194.116.73[:port]// <schema>://\[2a00:1450:400a:806::1007\][:port]-func newURL(url string) (*gourl.URL, string) {+func resolveUrl(url string) (string, string) {uri, err := gourl.ParseRequestURI(url)if err != nil {usageAndExit(err.Error())@@ -191,7 +203,7 @@ func newURL(url string) (*gourl.URL, string) {uri.Host = fmt.Sprintf("[%s]", ip)}}- return uri, serverName+ return uri.String(), serverName}func usageAndExit(message string) {diff --git a/boom_test.go b/boom_test.goindex cc5b101..f7d792a 100644--- a/boom_test.go+++ b/boom_test.go@@ -28,52 +28,52 @@ func (r *mockDnsResolver) Lookup(host string) ([]string, error) {func TestParseUrl(t *testing.T) {defaultDnsResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}- u, _ := newURL("http://google.com:80/path/to/resource?q=rawquery")- if u.String() != "http://[2a00:1450:400a:806::1007]:80/path/to/resource?q=rawquery" {+ 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)}}func TestParseUrl_IPv4(t *testing.T) {defaultDnsResolver = &mockDnsResolver{Addr: "127.0.0.1"}- u, s := newURL("http://google.com")+ 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)}- if u.String() != "http://127.0.0.1" {+ if u != "http://127.0.0.1" {t.Errorf("URL is expected to be http://127.0.0.1, %v is found.", u)}}func TestParseUrl_IPv4AndPort(t *testing.T) {defaultDnsResolver = &mockDnsResolver{Addr: "127.0.0.1"}- u, s := newURL("http://google.com:80")+ u, s := resolveUrl("http://google.com:80")if s != "google.com" {t.Errorf("Original server name doesn't match with google.com, %v is found.", s)}- if u.String() != "http://127.0.0.1:80" {+ if u != "http://127.0.0.1:80" {t.Errorf("URL is expected to be http://127.0.0.1, %v is found.", u)}}func TestParseUrl_IPv6(t *testing.T) {defaultDnsResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}- u, s := newURL("http://google.com")+ 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)}- if u.String() != "http://[2a00:1450:400a:806::1007]" {+ if u != "http://[2a00:1450:400a:806::1007]" {t.Errorf("URL is expected to be http://[2a00:1450:400a:806::1007], %v is found.", u)}}func TestParseUrl_IPv6AndPort(t *testing.T) {defaultDnsResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}- u, s := newURL("http://google.com:80")+ u, s := resolveUrl("http://google.com:80")if s != "google.com" {t.Errorf("Original server name doesn't match with google.com, %v is found.", s)}- if u.String() != "http://[2a00:1450:400a:806::1007]:80" {+ if u != "http://[2a00:1450:400a:806::1007]:80" {t.Errorf("URL is expected to be http://[2a00:1450:400a:806::1007]:80, %v is found.", u)}}diff --git a/commands/boom.go b/commands/boom.goindex 41edee3..0c03c8e 100644--- a/commands/boom.go+++ b/commands/boom.go@@ -17,6 +17,7 @@ package commandsimport ("net/http"+ "strings""time""github.com/rakyll/pb"@@ -28,12 +29,31 @@ type result struct {duration time.Duration}+type ReqOpts struct {+ Method string+ Url string+ Header http.Header+ Body string+ Username string+ Password string+ // Request host is an resolved IP. TLS/SSL handshakes may require+ // the original server name, keep it to initate the TLS client.+ ServerName string+}++// Creates a req object from req options+func (r *ReqOpts) Request() *http.Request {+ req, _ := http.NewRequest(r.Method, r.Url, strings.NewReader(r.Body))+ req.Header = r.Header+ if r.Username != "" && r.Password != "" {+ req.SetBasicAuth(r.Username, r.Password)+ }+ return req+}+type Boom struct {// Request to make.- Req *http.Request- // Req.Host is an resolved IP. TLS/SSL handshakes may require- // the original server name, keep it to initate the TLS client.- OrigServerName string+ Req *ReqOpts// Total number of requests to make.N int// Concurrency level, the number of concurrent workers to run.diff --git a/commands/run.go b/commands/run.goindex c461343..c6f65c9 100644--- a/commands/run.go+++ b/commands/run.go@@ -30,14 +30,14 @@ func (b *Boom) Run() {b.run()}-func (b *Boom) worker(ch chan bool) {+func (b *Boom) worker(ch chan *http.Request) {tr := &http.Transport{- TLSClientConfig: &tls.Config{InsecureSkipVerify: b.AllowInsecure, ServerName: b.OrigServerName},+ TLSClientConfig: &tls.Config{InsecureSkipVerify: b.AllowInsecure, ServerName: b.Req.ServerName},}client := &http.Client{Transport: tr}- for _ = range ch {+ for req := range ch {s := time.Now()- resp, err := client.Do(b.Req)+ resp, err := client.Do(req)code := 0if resp != nil {code = resp.StatusCode@@ -65,7 +65,7 @@ func (b *Boom) run() {}start := time.Now()- jobs := make(chan bool, b.N)+ jobs := make(chan *http.Request, b.N)// Start workers.for i := 0; i < b.C; i++ {go func() {@@ -79,7 +79,7 @@ func (b *Boom) run() {if b.Qps > 0 {<-throttle}- jobs <- true+ jobs <- b.Req.Request()}close(jobs)diff --git a/commands/run_test.go b/commands/run_test.goindex cab1416..c08c192 100644--- a/commands/run_test.go+++ b/commands/run_test.go@@ -15,6 +15,7 @@package commandsimport (+ "io/ioutil""net/http""net/http/httptest""sync"@@ -31,11 +32,13 @@ func TestN(t *testing.T) {server := httptest.NewServer(http.HandlerFunc(handler))defer server.Close()- req, _ := http.NewRequest("GET", server.URL, nil)boom := &Boom{- Req: req,- N: 20,- C: 2,+ Req: &ReqOpts{+ Method: "GET",+ Url: server.URL,+ },+ N: 20,+ C: 2,}boom.Run()if count != 20 {@@ -52,9 +55,11 @@ func TestQps(t *testing.T) {server := httptest.NewServer(http.HandlerFunc(handler))defer server.Close()- req, _ := http.NewRequest("GET", server.URL, nil)boom := &Boom{- Req: req,+ Req: &ReqOpts{+ Method: "GET",+ Url: server.URL,+ },N: 20,C: 2,Qps: 1,@@ -69,3 +74,35 @@ func TestQps(t *testing.T) {go boom.Run()wg.Wait()}++func TestBody(t *testing.T) {+ var wg sync.WaitGroup+ var count int64+ handler := func(w http.ResponseWriter, r *http.Request) {+ body, _ := ioutil.ReadAll(r.Body)+ if string(body) == "Body" {+ atomic.AddInt64(&count, int64(1))+ }+ }+ server := httptest.NewServer(http.HandlerFunc(handler))+ defer server.Close()++ boom := &Boom{+ Req: &ReqOpts{+ Method: "POST",+ Url: server.URL,+ Body: "Body",+ },+ N: 10,+ C: 1,+ }+ wg.Add(1)+ time.AfterFunc(time.Second, func() {+ if count != 10 {+ t.Errorf("Expected to boom 10 times, found %v", count)+ }+ wg.Done()+ })+ go boom.Run()+ wg.Wait()+}