HTTP load generator, ApacheBench (ab) replacement
Go
Add status code based response dist.
Burcu Dogan6546f6c63c52
diff --git a/commands/boom.go b/commands/boom.goindex c7c4773..33e340c 100644--- a/commands/boom.go+++ b/commands/boom.go@@ -8,9 +8,9 @@ import ()type result struct {- err error- is2xx bool- dur time.Duration+ err error+ statusCode int+ dur time.Duration}type Boom struct {diff --git a/commands/print.go b/commands/print.goindex e96b709..dce2d24 100644--- a/commands/print.go+++ b/commands/print.go@@ -6,6 +6,8 @@ import ("time")+var statusCodeDist map[int]int = make(map[int]int)+func (b *Boom) Print() {total := b.end.Sub(b.start)totalSuccessful := 0@@ -15,7 +17,8 @@ func (b *Boom) Print() {for {select {case r := <-b.results:- if !r.is2xx {+ statusCodeDist[r.statusCode]+++ if r.statusCode >= 200 && r.statusCode < 300 {continue}avgTotal += r.dur.Nanoseconds()@@ -28,10 +31,7 @@ func (b *Boom) Print() {totalSuccessful++default:rps := float64(b.N) / total.Seconds()- fmt.Printf("\n")- fmt.Printf("Summary:\n")- fmt.Printf(" total:\t%v requests\n", b.N)- fmt.Printf(" concurrency:\t%v concurrent requests\n", b.C)+ fmt.Printf("\nSummary:\n")fmt.Printf(" total 2xx:\t%v requests\n", totalSuccessful)fmt.Printf(" total:\t%v secs\n", total.Seconds())fmt.Printf(" slowest:\t%v secs\n", slowest.Seconds())@@ -39,11 +39,19 @@ func (b *Boom) Print() {fmt.Printf(" average:\t%v secs\n", float64(avgTotal)/float64(b.N)*math.Pow(10, 9)) // TODO: in secondsfmt.Printf(" requests/sec:\t%v\n", rps)fmt.Printf(" speed index:\t%v\n", speedIndex(rps))+ b.printStatusCodes()return}}}+func (b *Boom) printStatusCodes() {+ fmt.Printf("\nStatus code distrubution:\n")+ for code, num := range statusCodeDist {+ fmt.Printf(" [%d]\t%d responses\n", code, num)+ }+}+func speedIndex(rps float64) string {if rps > 500 {return "Whoa, pretty neat"diff --git a/commands/run.go b/commands/run.goindex 7f37fc4..dc1b2ca 100644--- a/commands/run.go+++ b/commands/run.go@@ -32,15 +32,15 @@ func (b *Boom) teardown() {}func (b *Boom) run() {- remaining := b.N+ rem := b.Nfor {- if remaining < 1 {+ if rem == 0 {break}c := b.C- if remaining < b.C {- c = remaining+ if rem < b.C {+ c = rem}var wg sync.WaitGroup@@ -53,7 +53,7 @@ func (b *Boom) run() {}()}wg.Wait()- remaining = remaining - c+ rem -= c}}@@ -61,6 +61,9 @@ func (b *Boom) runOneReq() {s := time.Now()resp, err := b.Client.Do(b.Req)- is2xx := resp != nil && resp.StatusCode >= 200 && resp.StatusCode < 300- b.results <- &result{is2xx: is2xx, err: err, dur: time.Now().Sub(s)}+ code := 0+ if resp != nil {+ code = resp.StatusCode+ }+ b.results <- &result{statusCode: code, err: err, dur: time.Now().Sub(s)}}