Skip to content

armgit / hey

Public Imported from GitHub
0

HTTP load generator, ApacheBench (ab) replacement

Go

Code reorg and major rollback to old concurrency model, not supporting timeouts temporarily.

Burcu Dogan1939f5edfb8b
diff --git a/commands/boom.go b/commands/boom.go
index fe0ea28..a6d5918 100644
--- a/commands/boom.go
+++ b/commands/boom.go
@@ -27,17 +27,6 @@ type result struct {
duration time.Duration
}
-type report struct {
- total time.Duration
- lats []float64
- avgTotal float64
- statusCodeDist map[int]int
- fastest, slowest, average float64
- rps float64
- start time.Time
- end time.Time
-}
-
type Boom struct {
Req *http.Request
N int // Number of requests
@@ -50,5 +39,12 @@ type Boom struct {
results chan *result
jobs chan bool
bar *pb.ProgressBar
- rpt report
+ rpt *report
+}
+
+func newPb(size int) *pb.ProgressBar {
+ pb.Current = barChar
+ pb.BarStart = ""
+ pb.BarEnd = ""
+ return pb.StartNew(size)
}
diff --git a/commands/print.go b/commands/print.go
index 0722c64..fc900dd 100644
--- a/commands/print.go
+++ b/commands/print.go
@@ -25,20 +25,48 @@ const (
barChar = "█"
)
-func (r *report) update(res *result) {
- r.lats = append(r.lats, res.duration.Seconds())
- r.statusCodeDist[res.statusCode]++
- r.avgTotal += res.duration.Seconds()
+type report struct {
+ avgTotal float64
+ fastest float64
+ slowest float64
+ average float64
+ rps float64
+
+ start time.Time
+ end time.Time
+ total time.Duration
+
+ statusCodeDist map[int]int
+ lats []float64
}
-func (r *report) finalize(totalReq int) {
- r.end = time.Now()
- r.total = r.end.Sub(r.start)
- r.rps = float64(totalReq) / r.total.Seconds()
- r.average = r.avgTotal / float64(totalReq)
+func newReport(size int) *report {
+ return &report{
+ statusCodeDist: make(map[int]int),
+ start: time.Now(),
+ }
}
-func (r *report) Print() {
+func (r *report) finalize(results chan *result) {
+ for {
+ select {
+ case res := <-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.rps = float64(len(r.lats)) / r.total.Seconds()
+ r.average = r.avgTotal / float64(len(r.lats))
+
+ r.print()
+ return
+ }
+ }
+}
+
+func (r *report) print() {
if len(r.lats) > 0 {
sort.Float64s(r.lats)
r.fastest = r.lats[0]
@@ -49,7 +77,6 @@ func (r *report) Print() {
fmt.Printf(" Fastest:\t%4.4f secs.\n", r.fastest)
fmt.Printf(" Average:\t%4.4f secs.\n", r.average)
fmt.Printf(" Requests/sec:\t%4.4f\n", r.rps)
- fmt.Printf(" Speed index:\t%v\n", speedIndex(r.rps))
r.printStatusCodes()
r.printHistogram()
r.printLatencies()
@@ -117,15 +144,3 @@ func (r *report) printStatusCodes() {
fmt.Printf(" [%d]\t%d responses\n", code, num)
}
}
-
-func speedIndex(rps float64) string {
- if rps > 500 {
- return "Whoa, pretty neat"
- } 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.go
index bd36f33..aa18385 100644
--- a/commands/run.go
+++ b/commands/run.go
@@ -21,16 +21,13 @@ import (
"net/http"
"sync"
"time"
-
- "github.com/cheggaaa/pb"
)
func (b *Boom) Run() {
b.Req.Header.Add("cache-control", "no-cache")
b.init()
b.run()
- b.teardown()
- b.rpt.Print()
+ b.rpt.finalize(b.results)
}
func (b *Boom) init() {
@@ -40,98 +37,54 @@ func (b *Boom) init() {
}
b.Client = &http.Client{Transport: tr}
}
- b.results = make(chan *result, b.C)
- b.jobs = make(chan bool, b.C)
+ b.results = make(chan *result, b.N)
b.bar = newPb(b.N)
- b.rpt.statusCodeDist = make(map[int]int)
- b.rpt.start = time.Now()
-}
-
-func (b *Boom) teardown() {
- b.bar.Finish()
- b.rpt.finalize(b.N)
+ b.rpt = newReport(b.N)
}
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()
+ for _ = range b.jobs {
+ s := time.Now()
+ resp, err := b.Client.Do(b.Req)
+ code := 0
+ if resp != nil {
+ code = resp.StatusCode
}
- }
-}
-
-func (b *Boom) collector() {
- for {
- select {
- case r := <-b.results:
- b.rpt.update(r)
+ 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 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.T > 0 {
- timeout = time.After(time.Duration(b.T) * time.Second)
- }
- // Start workers.
+ // Start C workers to consume.
for i := 0; i < b.C; i++ {
wg.Add(1)
go b.worker(&wg)
}
- // Start sending requests.
-requestLoop:
+ // Send N requests.
+ b.jobs = make(chan bool, b.C)
for i := 0; i < b.N; i++ {
- select {
- default:
- if b.Q > 0 {
- <-throttle
- }
- b.jobs <- true
- case <-timeout:
- break requestLoop
+ if b.Q > 0 {
+ <-throttle
}
+ b.jobs <- true
}
close(b.jobs)
wg.Wait()
- close(b.results)
-}
-
-func newPb(size int) *pb.ProgressBar {
- pb.Current = barChar
- pb.BarStart = ""
- pb.BarEnd = ""
- return pb.StartNew(size)
+ b.bar.Finish()
}
diff --git a/main.go b/main.go
index 99f543f..4e80df5 100644
--- a/main.go
+++ b/main.go
@@ -46,7 +46,6 @@ Options:
-c Number of requests to run concurrently. Total number of requests cannot
be smaller than the concurency level.
-q Rate limit, in seconds (QPS).
- -t Time limit in seconds. boom will stop after s seconds if specified.
-m HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS.
-h Custom HTTP headers, name1:value1;name2:value2.