HTTP load generator, ApacheBench (ab) replacement
Go
Make result channel bounded by a reasonable value, not N. Incremental… (#74)
mdakin59fcb3294af7
diff --git a/hey.go b/hey.goindex 120e9f0..eaf0cdc 100644--- a/hey.go+++ b/hey.go@@ -212,8 +212,8 @@ func main() {signal.Notify(c, os.Interrupt)go func() {<-c- w.Finish()- os.Exit(1)+ fmt.Fprintf(os.Stderr, "\nInterrupted, stopping workers.")+ w.Stop()}()w.Run()}diff --git a/requester/print.go b/requester/print.goindex dfce022..c89a360 100644--- a/requester/print.go+++ b/requester/print.go@@ -26,6 +26,9 @@ const (barChar = "∎")+// We report for max 1M results.+const maxRes = 1000000+type report struct {avgTotal float64fastest float64@@ -45,53 +48,71 @@ type report struct {delayLats []float64results chan *result+ done chan booltotal time.DurationerrorDist map[string]intstatusCodeDist map[int]intlats []float64sizeTotal int64-- output string+ numRes int64+ output stringw io.Writer}-func newReport(w io.Writer, size int, results chan *result, output string, total time.Duration) *report {+func newReport(w io.Writer, results chan *result, output string, n int) *report {+ cap := min(n, maxRes)return &report{output: output,results: results,- total: total,+ done: make(chan bool, 1),statusCodeDist: make(map[int]int),errorDist: make(map[string]int),w: w,+ connLats: make([]float64, 0, cap),+ dnsLats: make([]float64, 0, cap),+ reqLats: make([]float64, 0, cap),+ resLats: make([]float64, 0, cap),+ delayLats: make([]float64, 0, cap),+ lats: make([]float64, 0, cap),}}-func (r *report) finalize() {+func runReporter(r *report) {+ // Loop will continue until channel is closedfor res := range r.results {+ r.numRes++if res.err != nil {r.errorDist[res.err.Error()]++} else {- r.lats = append(r.lats, res.duration.Seconds())r.avgTotal += res.duration.Seconds()r.avgConn += res.connDuration.Seconds()r.avgDelay += res.delayDuration.Seconds()r.avgDNS += res.dnsDuration.Seconds()r.avgReq += res.reqDuration.Seconds()r.avgRes += res.resDuration.Seconds()- r.connLats = append(r.connLats, res.connDuration.Seconds())- r.dnsLats = append(r.dnsLats, res.dnsDuration.Seconds())- r.reqLats = append(r.reqLats, res.reqDuration.Seconds())- r.delayLats = append(r.delayLats, res.delayDuration.Seconds())- r.resLats = append(r.resLats, res.resDuration.Seconds())+ if len(r.resLats) < maxRes {+ r.lats = append(r.lats, res.duration.Seconds())+ r.connLats = append(r.connLats, res.connDuration.Seconds())+ r.dnsLats = append(r.dnsLats, res.dnsDuration.Seconds())+ r.reqLats = append(r.reqLats, res.reqDuration.Seconds())+ r.delayLats = append(r.delayLats, res.delayDuration.Seconds())+ r.resLats = append(r.resLats, res.resDuration.Seconds())+ }r.statusCodeDist[res.statusCode]++if res.contentLength > 0 {r.sizeTotal += res.contentLength}}}- r.rps = float64(len(r.lats)) / r.total.Seconds()+ // Signal reporter is done.+ r.done <- true+}++func (r *report) finalize(total time.Duration) {+ r.total = total+ r.rps = float64(r.numRes) / r.total.Seconds()r.average = r.avgTotal / float64(len(r.lats))r.avgConn = r.avgConn / float64(len(r.lats))r.avgDelay = r.avgDelay / float64(len(r.lats))@@ -129,6 +150,9 @@ func (r *report) print() {r.printf(" Total data:\t%d bytes\n", r.sizeTotal)r.printf(" Size/request:\t%d bytes\n", r.sizeTotal/int64(len(r.lats)))}+ if r.numRes > maxRes {+ r.printf("\nNote: Distributions are for first %d results.", len(r.lats))+ }r.printHistogram()r.printLatencies()r.printf("\nDetails (average, fastest, slowest):")diff --git a/requester/requester.go b/requester/requester.goindex 87820df..838f4d5 100644--- a/requester/requester.go+++ b/requester/requester.go@@ -32,6 +32,10 @@ import (const heyUA = "hey/0.0.1"+// Max size of the buffer of result channel.+const maxResult = 1000000+const maxIdleConn = 500+type result struct {err errorstatusCode int@@ -88,6 +92,8 @@ type Work struct {results chan *resultstopCh chan struct{}start time.Time++ report *report}func (b *Work) writer() io.Writer {@@ -108,19 +114,31 @@ func (b *Work) Run() {ua += " " + heyUA}- b.results = make(chan *result, b.N)- b.stopCh = make(chan struct{}, 1000)+ b.results = make(chan *result, min(b.C*1000, maxResult))+ b.stopCh = make(chan struct{}, b.C)b.start = time.Now()-+ b.report = newReport(b.writer(), b.results, b.Output, b.N)+ // Run the reporter first, it polls the result channel until it is closed.+ go func() {+ runReporter(b.report)+ }()b.runWorkers()b.Finish()}+func (b *Work) Stop() {+ // Send stop signal so that workers can stop gracefully.+ for i := 0; i < b.C; i++ {+ b.stopCh <- struct{}{}+ }+}+func (b *Work) Finish() {- b.stopCh <- struct{}{}close(b.results)total := time.Now().Sub(b.start)- newReport(b.writer(), b.N, b.results, b.Output, total).finalize()+ // Wait until the reporter is done.+ <-b.report.done+ b.report.finalize(total)}func (b *Work) makeRequest(c *http.Client) {@@ -177,27 +195,12 @@ func (b *Work) makeRequest(c *http.Client) {}}-func (b *Work) runWorker(n int) {+func (b *Work) runWorker(client *http.Client, n int) {var throttle <-chan time.Timeif b.QPS > 0 {throttle = time.Tick(time.Duration(1e6/(b.QPS)) * time.Microsecond)}- tr := &http.Transport{- TLSClientConfig: &tls.Config{- InsecureSkipVerify: true,- },- DisableCompression: b.DisableCompression,- DisableKeepAlives: b.DisableKeepAlives,- Proxy: http.ProxyURL(b.ProxyAddr),- }- if b.H2 {- http2.ConfigureTransport(tr)- } else {- tr.TLSNextProto = make(map[string]func(string, *tls.Conn) http.RoundTripper)- }-- client := &http.Client{Transport: tr, Timeout: time.Duration(b.Timeout) * time.Second}if b.DisableRedirects {client.CheckRedirect = func(req *http.Request, via []*http.Request) error {return http.ErrUseLastResponse@@ -207,7 +210,13 @@ func (b *Work) runWorker(n int) {if b.QPS > 0 {<-throttle}- b.makeRequest(client)+ // Check if application is stopped. Do not send into a closed channel.+ select {+ case <-b.stopCh:+ return+ default:+ b.makeRequest(client)+ }}}@@ -215,10 +224,26 @@ func (b *Work) runWorkers() {var wg sync.WaitGroupwg.Add(b.C)+ tr := &http.Transport{+ TLSClientConfig: &tls.Config{+ InsecureSkipVerify: true,+ },+ MaxIdleConnsPerHost: min(b.C, maxIdleConn),+ DisableCompression: b.DisableCompression,+ DisableKeepAlives: b.DisableKeepAlives,+ Proxy: http.ProxyURL(b.ProxyAddr),+ }+ if b.H2 {+ http2.ConfigureTransport(tr)+ } else {+ tr.TLSNextProto = make(map[string]func(string, *tls.Conn) http.RoundTripper)+ }+ client := &http.Client{Transport: tr, Timeout: time.Duration(b.Timeout) * time.Second}+// Ignore the case where b.N % b.C != 0.for i := 0; i < b.C; i++ {go func() {- b.runWorker(b.N / b.C)+ b.runWorker(client, b.N/b.C)wg.Done()}()}@@ -241,3 +266,10 @@ func cloneRequest(r *http.Request, body []byte) *http.Request {}return r2}++func min(a, b int) int {+ if a < b {+ return a+ }+ return b+}