HTTP load generator, ApacheBench (ab) replacement
Go
Reorg commands.
Burcu Doganbd32018c1171
diff --git a/commands/print.go b/commands/print.gonew file mode 100644index 0000000..1ce267b--- /dev/null+++ b/commands/print.go@@ -0,0 +1,56 @@+package commands++import (+ "fmt"+ "time"+)++func (b *Boom) Print() {+ total := b.end.Sub(b.start)+ totalSuccessful := 0+ var avgTotal int64+ var fastest, slowest time.Duration++ for {+ select {+ case r := <-b.results:+ if !r.is2xx {+ continue+ }+ avgTotal += r.dur.Nanoseconds()+ if fastest.Nanoseconds() == 0 || r.dur.Nanoseconds() < fastest.Nanoseconds() {+ fastest = r.dur+ }+ if r.dur.Nanoseconds() > slowest.Nanoseconds() {+ slowest = r.dur+ }+ 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(" total 2xx:\t%v requests\n", totalSuccessful)+ fmt.Printf(" total:\t%v\n", total.Seconds())+ fmt.Printf(" slowest:\t%v\n", slowest.Seconds())+ fmt.Printf(" fastest:\t%v\n", fastest.Seconds())+ fmt.Printf(" average:\t%v nanoseconds\n", avgTotal/int64(b.N)) // TODO: in seconds+ fmt.Printf(" requests/sec:\t%v\n", rps)+ fmt.Printf(" speed index:\t%v\n", speedIndex(rps))+ return+ }+ }+}++func speedIndex(rps float64) string {+ if rps > 500 {+ return ""+ } else if rps > 100 {+ return "Pretty good"+ } else if rps > 50 {+ return "Meh"+ } else {+ return "Hahahaha"+ }+}diff --git a/commands/run.go b/commands/run.goindex d110a40..97bcbfd 100644--- a/commands/run.go+++ b/commands/run.go@@ -1,7 +1,6 @@package commandsimport (- "fmt""net/http""sync""time"@@ -9,14 +8,13 @@ import ("github.com/cheggaaa/pb")-func (b *Boom) Run() error {+func (b *Boom) Run() {b.Req.Header.Add("cache-control", "no-cache")b.init()b.run()b.teardown()- b.summary()- return nil+ b.Print()}func (b *Boom) init() {@@ -33,44 +31,6 @@ func (b *Boom) teardown() {b.bar.Finish()}-func (b *Boom) summary() {- total := b.end.Sub(b.start)- totalSuccessful := 0- var avgTotal int64- var fastest, slowest time.Duration-- for {- select {- case r := <-b.results:- if !r.is2xx {- continue- }- avgTotal += r.dur.Nanoseconds()- if fastest.Nanoseconds() == 0 || r.dur.Nanoseconds() < fastest.Nanoseconds() {- fastest = r.dur- }- if r.dur.Nanoseconds() > slowest.Nanoseconds() {- slowest = r.dur- }- 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(" total 2xx:\t%v requests\n", totalSuccessful)- fmt.Printf(" total:\t%v\n", total.Seconds())- fmt.Printf(" slowest:\t%v\n", slowest.Seconds())- fmt.Printf(" fastest:\t%v\n", fastest.Seconds())- fmt.Printf(" average:\t%v\n", avgTotal/int64(b.N))- fmt.Printf(" requests/sec:\t%v\n", rps)- fmt.Printf(" speed index:\t%v\n", speedIndex(rps))- return- }- }-}-func (b *Boom) run() {var wg sync.WaitGroupwg.Add(b.N)@@ -93,23 +53,3 @@ func (b *Boom) runOneReq() {is2xx := resp != nil && resp.StatusCode >= 200 && resp.StatusCode < 300b.results <- &result{is2xx: is2xx, err: err, dur: time.Now().Sub(s)}}--func (b *Boom) handleResp(resp *http.Response, err error, dur time.Duration) {- is2xx := false- if resp != nil {- is2xx = resp.StatusCode >= 200 && resp.StatusCode < 300- }- b.results <- &result{is2xx: is2xx, err: err, dur: dur}-}--func speedIndex(rps float64) string {- if rps > 500 {- return ""- } else if rps > 100 {- return "Pretty good"- } else if rps > 50 {- return "Meh"- } else {- return "Hahahaha"- }-}