Skip to content

armgit / hey

Public Imported from GitHub
0

HTTP load generator, ApacheBench (ab) replacement

Go

Avoiding to copy the response body to /dev/null

Burcu Dogan4a56dff4d1f3
diff --git a/commands/boom.go b/commands/boom.go
index e408bd9..289b636 100644
--- a/commands/boom.go
+++ b/commands/boom.go
@@ -38,15 +38,12 @@ type Boom struct {
Timeout int
// Rate limit.
Qps int
- // HTTP client to make the requests.
- Client *http.Client
// Option to allow insecure TLS/SSL certificates.
AllowInsecure bool
- results chan *result
- jobs chan bool
bar *pb.ProgressBar
rpt *report
+ results chan *result
}
func newPb(size int) (bar *pb.ProgressBar) {
diff --git a/commands/print.go b/commands/print.go
index 2499f48..b191ec8 100644
--- a/commands/print.go
+++ b/commands/print.go
@@ -32,34 +32,31 @@ type report struct {
average float64
rps float64
- start time.Time
- end time.Time
- total time.Duration
+ results chan *result
+ total time.Duration
statusCodeDist map[int]int
lats []float64
}
-func newReport(size int) *report {
+func newReport(size int, results chan *result) *report {
return &report{
statusCodeDist: make(map[int]int),
- start: time.Now(),
+ results: results,
}
}
-func (r *report) finalize(results chan *result) {
+func (r *report) finalize(total time.Duration) {
for {
select {
- case res := <-results:
+ case res := <-r.results:
r.lats = append(r.lats, res.duration.Seconds())
r.avgTotal += res.duration.Seconds()
r.statusCodeDist[res.statusCode]++
default:
- r.end = time.Now()
- r.total = r.end.Sub(r.start)
+ r.total = total
r.rps = float64(len(r.lats)) / r.total.Seconds()
r.average = r.avgTotal / float64(len(r.lats))
-
r.print()
return
}
diff --git a/commands/run.go b/commands/run.go
index f32646a..0422d99 100644
--- a/commands/run.go
+++ b/commands/run.go
@@ -16,75 +16,70 @@ package commands
import (
"crypto/tls"
- "io"
- "io/ioutil"
"net/http"
"sync"
"time"
)
func (b *Boom) Run() {
- b.Req.Header.Add("cache-control", "no-cache")
- b.init()
- b.run()
- b.rpt.finalize(b.results)
-}
-
-func (b *Boom) init() {
- if b.Client == nil {
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: b.AllowInsecure},
- }
- b.Client = &http.Client{Transport: tr}
- }
b.results = make(chan *result, b.N)
b.bar = newPb(b.N)
- b.rpt = newReport(b.N)
+ b.rpt = newReport(b.N, b.results)
+ b.run()
}
-func (b *Boom) worker(wg *sync.WaitGroup) {
- defer wg.Done()
- for _ = range b.jobs {
+func (b *Boom) worker(ch chan bool) {
+ tr := &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: b.AllowInsecure},
+ }
+ client := &http.Client{Transport: tr}
+ for _ = range ch {
s := time.Now()
- resp, err := b.Client.Do(b.Req)
+ resp, err := client.Do(b.Req)
code := 0
if resp != nil {
code = resp.StatusCode
+ // cleanup body, so the socket can be reusable
+ resp.Body.Close()
}
+ b.bar.Increment()
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) run() {
var wg sync.WaitGroup
- // Start throttler if rate limit is specified.
+ wg.Add(b.C)
+
var throttle <-chan time.Time
if b.Qps > 0 {
- throttle = time.Tick(time.Duration(1e6/b.Qps) * time.Microsecond)
+ throttle = time.Tick(time.Duration(1e6/(b.Qps)) * time.Microsecond)
}
- // Start C workers to consume.
+
+ start := time.Now()
+ jobs := make(chan bool, b.N)
+ // Start workers.
for i := 0; i < b.C; i++ {
- wg.Add(1)
- go b.worker(&wg)
+ go func() {
+ b.worker(jobs)
+ wg.Done()
+ }()
}
- // Send N requests.
- b.jobs = make(chan bool, b.C)
+
+ // Start sending jobs to the workers.
for i := 0; i < b.N; i++ {
if b.Qps > 0 {
<-throttle
}
- b.jobs <- true
+ jobs <- true
}
- close(b.jobs)
+ close(jobs)
+
wg.Wait()
b.bar.Finish()
+ b.rpt.finalize(time.Now().Sub(start))
}