Skip to content

armgit / hey

Public Imported from GitHub
0

HTTP load generator, ApacheBench (ab) replacement

Go

Add simple timeout and throttling support, start collecting results immediately, refactor.

Mehmet Akin779cb1bae54c
diff --git a/commands/boom.go b/commands/boom.go
index f9ea4bf..014ad0c 100644
--- a/commands/boom.go
+++ b/commands/boom.go
@@ -27,14 +27,25 @@ type result struct {
duration time.Duration
}
+type Report struct {
+ latencies []float64
+ avgTotal float64
+ statusCodeDist map[int]int
+}
+
type Boom struct {
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
- start time.Time
- end time.Time
- results chan *result
- bar *pb.ProgressBar
+ start time.Time
+ end time.Time
+ results chan *result
+ bar *pb.ProgressBar
+ timeout <-chan time.Time
+ timedOut bool
+ report Report
}
diff --git a/commands/print.go b/commands/print.go
index ba5610f..505e69c 100644
--- a/commands/print.go
+++ b/commands/print.go
@@ -18,59 +18,41 @@ import (
"fmt"
"sort"
"strings"
- "time"
)
-var statusCodeDist map[int]int = make(map[int]int)
-
-var latencies []float64
-
func (b *Boom) Print() {
total := b.end.Sub(b.start)
- var avgTotal float64
- var fastest, slowest time.Duration
-
- for {
- select {
- case r := <-b.results:
- latencies = append(latencies, r.duration.Seconds())
- statusCodeDist[r.statusCode]++
-
- avgTotal += r.duration.Seconds()
- if fastest.Nanoseconds() == 0 || r.duration.Nanoseconds() < fastest.Nanoseconds() {
- fastest = r.duration
- }
- if r.duration.Nanoseconds() > slowest.Nanoseconds() {
- slowest = r.duration
- }
- default:
- rps := float64(b.N) / total.Seconds()
- fmt.Printf("\nSummary:\n")
- fmt.Printf(" Total:\t%4.4f secs.\n", total.Seconds())
- fmt.Printf(" Slowest:\t%4.4f secs.\n", slowest.Seconds())
- fmt.Printf(" Fastest:\t%4.4f secs.\n", fastest.Seconds())
- fmt.Printf(" Average:\t%4.4f secs.\n", avgTotal/float64(b.N))
- fmt.Printf(" Requests/sec:\t%4.4f\n", rps)
- fmt.Printf(" Speed index:\t%v\n", speedIndex(rps))
- sort.Float64s(latencies)
- b.printHistogram()
- b.printLatencies()
- b.printStatusCodes()
- return
- }
+ var fastest, slowest float64
+ rps := float64(b.N) / total.Seconds()
+ lat := b.report.latencies
+ if len(lat) > 0 {
+ fastest = lat[0]
+ slowest = lat[len(lat)-1]
+ sort.Float64s(b.report.latencies)
+ fmt.Printf("\nSummary:\n")
+ fmt.Printf(" Total:\t%4.4f secs.\n", total.Seconds())
+ fmt.Printf(" Slowest:\t%4.4f secs.\n", slowest)
+ fmt.Printf(" Fastest:\t%4.4f secs.\n", fastest)
+ fmt.Printf(" Average:\t%4.4f secs.\n", b.report.avgTotal/float64(b.N))
+ fmt.Printf(" Requests/sec:\t%4.4f\n", rps)
+ fmt.Printf(" Speed index:\t%v\n", speedIndex(rps))
+ b.printStatusCodes()
+ b.printHistogram(&lat)
+ b.printLatencies(&lat)
}
}
// Prints percentile latencies.
-func (b *Boom) printLatencies() {
+func (b *Boom) printLatencies(latPtr *[]float64) {
+ lat := *latPtr
pctls := []int{10, 25, 50, 75, 90, 95, 99}
// Sort the array
data := make([]float64, len(pctls))
j := 0
- for i := 0; i < len(latencies) && j < len(pctls); i++ {
- current := (i + 1) * 100 / len(latencies)
+ for i := 0; i < len(lat) && j < len(pctls); i++ {
+ current := (i + 1) * 100 / len(lat)
if current >= pctls[j] {
- data[j] = latencies[i]
+ data[j] = lat[i]
j++
}
}
@@ -82,12 +64,13 @@ func (b *Boom) printLatencies() {
}
}
-func (b *Boom) printHistogram() {
+func (b *Boom) printHistogram(latPtr *[]float64) {
+ lat := *latPtr
bc := 10
buckets := make([]float64, bc+1)
counts := make([]int, bc+1)
- fastest := latencies[0]
- slowest := latencies[len(latencies)-1]
+ fastest := lat[0]
+ slowest := lat[len(lat)-1]
bs := (slowest - fastest) / float64(bc)
for i := 0; i < bc; i++ {
buckets[i] = fastest + bs*float64(i)
@@ -95,8 +78,8 @@ func (b *Boom) printHistogram() {
buckets[bc] = slowest
var bi int
var max int
- for i := 0; i < len(latencies); {
- if latencies[i] <= buckets[bi] {
+ for i := 0; i < len(lat); {
+ if lat[i] <= buckets[bi] {
i++
counts[bi]++
if max < counts[bi] {
@@ -120,7 +103,7 @@ func (b *Boom) printHistogram() {
// Prints status code distribution.
func (b *Boom) printStatusCodes() {
fmt.Printf("\nStatus code distribution:\n")
- for code, num := range statusCodeDist {
+ for code, num := range b.report.statusCodeDist {
fmt.Printf(" [%d]\t%d responses\n", code, num)
}
}
diff --git a/commands/run.go b/commands/run.go
index fb733c3..0a9898b 100644
--- a/commands/run.go
+++ b/commands/run.go
@@ -27,7 +27,6 @@ func (b *Boom) Run() {
b.init()
b.run()
b.teardown()
-
b.Print()
}
@@ -35,9 +34,12 @@ func (b *Boom) init() {
if b.Client == nil {
b.Client = &http.Client{}
}
- b.results = make(chan *result, b.N)
+ b.results = make(chan *result, b.C)
+ b.timeout = make(chan time.Time)
b.bar = pb.StartNew(b.N)
+ b.report.statusCodeDist = make(map[int]int)
b.start = time.Now()
+ b.timedOut = false
}
func (b *Boom) teardown() {
@@ -45,38 +47,69 @@ func (b *Boom) teardown() {
b.bar.Finish()
}
+func (b *Boom) worker(jobs chan bool, wg *sync.WaitGroup, timeout <-chan time.Time) {
+ defer wg.Done()
+WORKER_LOOP:
+ for !b.timedOut {
+ select {
+ case _, chOpen := <-jobs:
+ if !chOpen {
+ break WORKER_LOOP
+ }
+ 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,
+ }
+ b.bar.Increment()
+ case <-b.timeout:
+ b.timedOut = true
+ }
+ }
+}
+
+func (b *Boom) collector() {
+ for {
+ select {
+ case r := <-b.results:
+ b.report.latencies = append(b.report.latencies, r.duration.Seconds())
+ b.report.statusCodeDist[r.statusCode]++
+ b.report.avgTotal += r.duration.Seconds()
+ }
+ }
+}
+
func (b *Boom) run() {
+ jobs := make(chan bool, b.C)
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)
+ }
for i := 0; i < b.C; i++ {
wg.Add(1)
- go b.worker(ch, &wg)
+ go b.worker(jobs, &wg, timeout)
}
- // Push requests to channel.
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
+ if b.Q > 0 {
+ <-throttle
}
- b.results <- &result{
- statusCode: code,
- duration: time.Now().Sub(s),
- err: err,
- }
- b.bar.Increment()
+ jobs <- true
}
+ close(jobs)
+ wg.Wait()
}
diff --git a/main.go b/main.go
index d6548bb..f941ede 100644
--- a/main.go
+++ b/main.go
@@ -34,6 +34,8 @@ var (
flagC = flag.Int("c", 50, "")
flagN = flag.Int("n", 200, "")
+ flagQ = flag.Int("q", 0, "")
+ flagS = flag.Int("s", 0, "")
)
var usage = `Usage: boom [options...] <url>
@@ -42,6 +44,8 @@ Options:
-n Number of requests to run.
-c Number of requests to run concurrently. Total number of requests cannot
be smaller than the concurency level.
+ -q Rate limit, in seconds (QPS).
+ -s Time limit in seconds. boom will stop after b seconds if specified.
-m HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS.
-h Custom HTTP headers, name1:value1;name2:value2.
@@ -62,8 +66,10 @@ func main() {
n := *flagN
c := *flagC
+ q := *flagQ
+ s := *flagS
- if n <= 0 || c <= 0 {
+ if n <= 0 || c <= 0 {
usageAndExit()
}
@@ -103,7 +109,7 @@ func main() {
req.SetBasicAuth(matches[0][1], matches[0][2])
}
- (&commands.Boom{N: n, C: c, Req: req}).Run()
+ (&commands.Boom{N: n, C: c, Q: q, S: s, Req: req}).Run()
}
func usageAndExit() {