HTTP load generator, ApacheBench (ab) replacement
Go
Merge upstream.
mdakin051680549d06
diff --cc commands/boom.goindex 815cba9,f7bd2c3..9e852cf--- a/commands/boom.go+++ b/commands/boom.go@@@ -27,27 -27,15 +27,28 @@@ type result structduration time.Duration}+type report struct {+ total time.Duration+ lats []float64+ avgTotal float64+ statusCodeDist map[int]int+ fastest, slowest, average, rps float64+}+type Boom struct {- Req *http.Request- N int // Number of requests- C int // Number of Concurrent workers- S int // Timeout- Q int // Rate limit (QPS)- Client *http.Client+ Req *http.Request- N int- C int++ N int // Number of requests++ C int // Number of Concurrent workers++ S int // Timeout++ Q int // Rate limit (QPS)+ Client *http.Client+ AllowInsecure bool- start time.Time- end time.Time- results chan *result- bar *pb.ProgressBar+ start time.Time+ end time.Time+ results chan *result+ jobs chan bool+ bar *pb.ProgressBar+ timedOut bool+ rpt report}diff --cc commands/run.goindex 7089525,bd551fb..189b492--- a/commands/run.go+++ b/commands/run.go@@@ -32,14 -36,14 +35,17 @@@ func (b *Boom) Run()func (b *Boom) init() {if b.Client == nil {- b.Client = &http.Client{}+ tr := &http.Transport{+ TLSClientConfig: &tls.Config{InsecureSkipVerify: b.AllowInsecure},+ }+ b.Client = &http.Client{Transport: tr}}- b.results = make(chan *result, b.N)+ b.results = make(chan *result, b.C)+ b.jobs = make(chan bool, b.C)b.bar = pb.StartNew(b.N)+ b.rpt.statusCodeDist = make(map[int]int)b.start = time.Now()+ b.timedOut = false}func (b *Boom) teardown() {@@@ -50,75 -51,45 +56,81 @@@b.bar.Finish()}+func (b *Boom) worker(wg *sync.WaitGroup) {+ defer wg.Done()+workerLoop:+ for {+ select {+ case _, chOpen := <-b.jobs:+ if !chOpen {+ break workerLoop+ }+ s := time.Now()+ resp, err := b.Client.Do(b.Req)+ code := 0+ if resp != nil {+ code = resp.StatusCode+ }+ b.results <- &result{+ statusCode: code,+ duration: time.Now().Sub(s),+ err: err,+ }++++ if resp != nil {++ io.Copy(ioutil.Discard, resp.Body)++ resp.Body.Close()++ }+++ b.bar.Increment()+ }+ }+}++func (b *Boom) collector() {+ for {+ select {+ case r := <-b.results:+ b.rpt.lats = append(b.rpt.lats, r.duration.Seconds())+ b.rpt.statusCodeDist[r.statusCode]+++ b.rpt.avgTotal += r.duration.Seconds()+ }+ }+}+func (b *Boom) run() {var wg sync.WaitGroup- ch := make(chan bool, b.C)- // Create c amount worker goroutines.+ // Start collector.+ go b.collector()+ // Start throttler if rate limit is specified.+ var throttle <-chan time.Time+ if b.Q > 0 {+ throttle = time.Tick(time.Duration(1e6/b.Q) * time.Microsecond)+ }+ // Start timeout counter if time limit is specified.+ var timeout <-chan time.Time+ if b.S > 0 {+ timeout = time.After(time.Duration(b.S) * time.Second)+ }+ // Start workers.for i := 0; i < b.C; i++ {wg.Add(1)- go b.worker(ch, &wg)+ go b.worker(&wg)}- // Push requests to channel.+ // Start sending requests.+requestLoop:for i := 0; i < b.N; i++ {- ch <- true- }- close(ch)- // Wait all goroutines to finish.- wg.Wait()-}--func (b *Boom) worker(ch chan bool, wg *sync.WaitGroup) {- defer wg.Done()-- // Pick one request from channel and process.- for _ = range ch {- s := time.Now()- resp, err := b.Client.Do(b.Req)- code := 0- if resp != nil {- code = resp.StatusCode- }- b.results <- &result{- statusCode: code,- duration: time.Now().Sub(s),- err: err,- }-- if resp != nil {- io.Copy(ioutil.Discard, resp.Body)- resp.Body.Close()+ select {+ default:+ if b.Q > 0 {+ <-throttle+ }+ b.jobs <- true+ case <-timeout:+ break requestLoop}-- b.bar.Increment()}+ close(b.jobs)+ wg.Wait()+ close(b.results)}diff --cc main.goindex 123d245,8dfa0f3..6776cee--- a/main.go+++ b/main.go@@@ -109,7 -107,7 +113,7 @@@ func main()req.SetBasicAuth(matches[0][1], matches[0][2])}- (&commands.Boom{N: n, C: c, Q: q, S: s, Req: req}).Run()- (&commands.Boom{N: n, C: c, Req: req, AllowInsecure: *flagInsecure}).Run()++ (&commands.Boom{N: n, C: c, Q: q, S: s, Req: req, AllowInsecure: *flagInsecure}).Run()}func usageAndExit() {