Skip to content

armgit / hey

Public Imported from GitHub
0

HTTP load generator, ApacheBench (ab) replacement

Go

Move more stuff to report, minor cleanup

mdakina93e0e5fe8b9
diff --git a/commands/boom.go b/commands/boom.go
index 9e852cf..fe0ea28 100644
--- a/commands/boom.go
+++ b/commands/boom.go
@@ -28,27 +28,27 @@ type result struct {
}
type report struct {
- total time.Duration
- lats []float64
- avgTotal float64
- statusCodeDist map[int]int
- fastest, slowest, average, rps float64
+ 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
C int // Number of Concurrent workers
- S int // Timeout
+ T int // Timeout
Q int // Rate limit (QPS)
Client *http.Client
AllowInsecure bool
- start time.Time
- end time.Time
- results chan *result
- jobs chan bool
- bar *pb.ProgressBar
- timedOut bool
- rpt report
+ results chan *result
+ jobs chan bool
+ bar *pb.ProgressBar
+ rpt report
}
diff --git a/commands/print.go b/commands/print.go
index 7f3d11f..c278d40 100644
--- a/commands/print.go
+++ b/commands/print.go
@@ -18,8 +18,22 @@ import (
"fmt"
"sort"
"strings"
+ "time"
)
+func (r *report) update(res *result) {
+ r.lats = append(r.lats, res.duration.Seconds())
+ r.statusCodeDist[res.statusCode]++
+ r.avgTotal += res.duration.Seconds()
+}
+
+func (r *report) finalize(b *Boom) {
+ r.end = time.Now()
+ r.total = r.end.Sub(r.start)
+ r.rps = float64(b.N) / r.total.Seconds()
+ r.average = r.avgTotal / float64(b.N)
+}
+
func (r *report) Print() {
if len(r.lats) > 0 {
sort.Float64s(r.lats)
diff --git a/commands/run.go b/commands/run.go
index 189b492..c0c2314 100644
--- a/commands/run.go
+++ b/commands/run.go
@@ -44,16 +44,12 @@ func (b *Boom) init() {
b.jobs = make(chan bool, b.C)
b.bar = pb.StartNew(b.N)
b.rpt.statusCodeDist = make(map[int]int)
- b.start = time.Now()
- b.timedOut = false
+ b.rpt.start = time.Now()
}
func (b *Boom) teardown() {
- b.end = time.Now()
- b.rpt.total = b.end.Sub(b.start)
- b.rpt.rps = float64(b.N) / b.rpt.total.Seconds()
- b.rpt.average = b.rpt.avgTotal / float64(b.N)
b.bar.Finish()
+ b.rpt.finalize(b)
}
func (b *Boom) worker(wg *sync.WaitGroup) {
@@ -91,9 +87,7 @@ func (b *Boom) collector() {
for {
select {
case r := <-b.results:
- b.rpt.lats = append(b.rpt.lats, r.duration.Seconds())
- b.rpt.statusCodeDist[r.statusCode]++
- b.rpt.avgTotal += r.duration.Seconds()
+ b.rpt.update(r)
}
}
}
@@ -109,8 +103,8 @@ func (b *Boom) run() {
}
// 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)
+ if b.T > 0 {
+ timeout = time.After(time.Duration(b.T) * time.Second)
}
// Start workers.
for i := 0; i < b.C; i++ {
diff --git a/main.go b/main.go
index 6776cee..99f543f 100644
--- a/main.go
+++ b/main.go
@@ -29,14 +29,14 @@ var (
flagMethod = flag.String("m", "GET", "")
flagHeaders = flag.String("h", "", "")
flagD = flag.String("d", "", "")
- flagType = flag.String("t", "text/html", "")
+ flagType = flag.String("T", "text/html", "")
flagAuth = flag.String("a", "", "")
flagInsecure = flag.Bool("allow-insecure", false, "")
flagC = flag.Int("c", 50, "")
flagN = flag.Int("n", 200, "")
flagQ = flag.Int("q", 0, "")
- flagS = flag.Int("s", 0, "")
+ flagT = flag.Int("t", 0, "")
)
var usage = `Usage: boom [options...] <url>
@@ -46,12 +46,12 @@ 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).
- -s Time limit in seconds. boom will stop after s seconds if specified.
+ -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.
-d HTTP request body.
- -t Content-type, defaults to "text/html".
+ -T Content-type, defaults to "text/html".
-a Basic authentication, username:password.
-allow-insecure Allow bad/expired TLS/SSL certificates.
@@ -71,7 +71,7 @@ func main() {
n := *flagN
c := *flagC
q := *flagQ
- s := *flagS
+ t := *flagT
if n <= 0 || c <= 0 {
usageAndExit()
@@ -113,7 +113,7 @@ func main() {
req.SetBasicAuth(matches[0][1], matches[0][2])
}
- (&commands.Boom{N: n, C: c, Q: q, S: s, Req: req, AllowInsecure: *flagInsecure}).Run()
+ (&commands.Boom{N: n, C: c, Q: q, T: t, Req: req, AllowInsecure: *flagInsecure}).Run()
}
func usageAndExit() {