HTTP load generator, ApacheBench (ab) replacement
Go
Reorganize boomer.
Burcu Dogan4111a6d5a138
diff --git a/boomer/boomer.go b/boomer/boomer.goindex 20835f3..7e4e3c7 100644--- a/boomer/boomer.go+++ b/boomer/boomer.go@@ -16,9 +16,13 @@package boomerimport (+ "crypto/tls"+ "io"+ "io/ioutil""net/http""net/url""strings"+ "sync""time""github.com/rakyll/pb"@@ -95,3 +99,94 @@ func newPb(size int) (bar *pb.ProgressBar) {bar.Start()return}++// client is the http.Client that will be used to make all requests+// to the destination.+var client *http.Client++// Run makes all the requests, prints the summary. It blocks until+// all work is done.+func (b *Boomer) Run() {+ b.results = make(chan *result, b.N)+ if b.Output == "" {+ b.bar = newPb(b.N)+ }++ start := time.Now()+ b.run()+ if b.Output == "" {+ b.bar.Finish()+ }++ printReport(b.N, b.results, b.Output, time.Now().Sub(start))+ close(b.results)+}++func (b *Boomer) worker(wg *sync.WaitGroup, ch chan *http.Request, readAll bool) {+ for req := range ch {+ s := time.Now()++ var code int+ var size int64++ resp, err := client.Do(req)++ if err == nil {+ size = resp.ContentLength+ code = resp.StatusCode+ if readAll {+ _, err = io.Copy(ioutil.Discard, resp.Body)+ }+ resp.Body.Close()+ }++ if b.bar != nil {+ b.bar.Increment()+ }++ wg.Done()+ b.results <- &result{+ statusCode: code,+ duration: time.Now().Sub(s),+ err: err,+ contentLength: size,+ }+ }+}++func (b *Boomer) run() {+ tr := &http.Transport{+ TLSClientConfig: &tls.Config{+ InsecureSkipVerify: b.AllowInsecure,+ },+ DisableCompression: b.DisableCompression,+ DisableKeepAlives: b.DisableKeepAlives,+ // TODO(jbd): Add dial timeout.+ TLSHandshakeTimeout: time.Duration(b.Timeout) * time.Millisecond,+ Proxy: http.ProxyURL(b.ProxyAddr),+ }+ client = &http.Client{Transport: tr}++ var wg sync.WaitGroup+ wg.Add(b.N)++ var throttle <-chan time.Time+ if b.Qps > 0 {+ throttle = time.Tick(time.Duration(1e6/(b.Qps)) * time.Microsecond)+ }+ jobs := make(chan *http.Request, b.N)+ for i := 0; i < b.C; i++ {+ go func() {+ b.worker(&wg, jobs, b.Req.ReadAll)+ }()+ }+ for i := 0; i < b.N; i++ {+ if b.Qps > 0 {+ <-throttle+ }+ jobs <- b.Req.Request()+ }+ close(jobs)++ wg.Wait()+}diff --git a/boomer/run.go b/boomer/run.godeleted file mode 100644index c15d270..0000000--- a/boomer/run.go+++ /dev/null@@ -1,115 +0,0 @@-// Copyright 2014 Google Inc. All Rights Reserved.-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package boomer--import (- "crypto/tls"- "io"- "io/ioutil"- "net/http"- "sync"- "time"-)--// client is the http.Client that will be used to make all requests-// to the destination.-var client *http.Client--// Run makes all the requests, prints the summary. It blocks until-// all work is done.-func (b *Boomer) Run() {- b.results = make(chan *result, b.N)- if b.Output == "" {- b.bar = newPb(b.N)- }-- start := time.Now()- b.run()- if b.Output == "" {- b.bar.Finish()- }-- printReport(b.N, b.results, b.Output, time.Now().Sub(start))- close(b.results)-}--func (b *Boomer) worker(wg *sync.WaitGroup, ch chan *http.Request, readAll bool) {- for req := range ch {- s := time.Now()-- var code int- var size int64-- resp, err := client.Do(req)-- if err == nil {- size = resp.ContentLength- code = resp.StatusCode- if readAll {- _, err = io.Copy(ioutil.Discard, resp.Body)- }- resp.Body.Close()- }-- if b.bar != nil {- b.bar.Increment()- }-- wg.Done()- b.results <- &result{- statusCode: code,- duration: time.Now().Sub(s),- err: err,- contentLength: size,- }- }-}--func (b *Boomer) run() {- tr := &http.Transport{- TLSClientConfig: &tls.Config{- InsecureSkipVerify: b.AllowInsecure,- },- DisableCompression: b.DisableCompression,- DisableKeepAlives: b.DisableKeepAlives,- // TODO(jbd): Add dial timeout.- TLSHandshakeTimeout: time.Duration(b.Timeout) * time.Millisecond,- Proxy: http.ProxyURL(b.ProxyAddr),- }- client = &http.Client{Transport: tr}-- var wg sync.WaitGroup- wg.Add(b.N)-- var throttle <-chan time.Time- if b.Qps > 0 {- throttle = time.Tick(time.Duration(1e6/(b.Qps)) * time.Microsecond)- }- jobs := make(chan *http.Request, b.N)- for i := 0; i < b.C; i++ {- go func() {- b.worker(&wg, jobs, b.Req.ReadAll)- }()- }- for i := 0; i < b.N; i++ {- if b.Qps > 0 {- <-throttle- }- jobs <- b.Req.Request()- }- close(jobs)-- wg.Wait()-}