Skip to content

armgit / hey

Public Imported from GitHub
0

HTTP load generator, ApacheBench (ab) replacement

Go

Initial commit.

Burcu Dogan91e534c84e50
diff --git a/commands/boom.go b/commands/boom.go
new file mode 100644
index 0000000..c7c4773
--- /dev/null
+++ b/commands/boom.go
@@ -0,0 +1,28 @@
+package commands
+
+import (
+ "net/http"
+ "time"
+
+ "github.com/cheggaaa/pb"
+)
+
+type result struct {
+ err error
+ is2xx bool
+ dur time.Duration
+}
+
+type Boom struct {
+ Req *http.Request
+ N int
+ C int
+ Client *http.Client
+
+ start time.Time
+ end time.Time
+
+ bar *pb.ProgressBar
+
+ results chan *result
+}
diff --git a/commands/run.go b/commands/run.go
new file mode 100644
index 0000000..ce1abca
--- /dev/null
+++ b/commands/run.go
@@ -0,0 +1,114 @@
+package commands
+
+import (
+ "fmt"
+ "net/http"
+ "sync"
+ "time"
+
+ "github.com/cheggaaa/pb"
+)
+
+func (b *Boom) Run() error {
+ b.Req.Header.Add("cache-control", "no-cache")
+ b.init()
+ b.run()
+ b.teardown()
+
+ b.summary()
+ return nil
+}
+
+func (b *Boom) init() {
+ if b.Client == nil {
+ b.Client = &http.Client{}
+ }
+ b.results = make(chan *result, b.N)
+ b.bar = pb.StartNew(b.N)
+ b.start = time.Now()
+}
+
+func (b *Boom) teardown() {
+ b.end = time.Now()
+ b.bar.Finish()
+}
+
+func (b *Boom) summary() {
+ total := b.end.Sub(b.start)
+ totalSuccessful := 0
+ var avgTotal int64
+ var fastest, slowest time.Duration
+
+ for {
+ select {
+ case r := <-b.results:
+ avgTotal += r.dur.Nanoseconds()
+ if fastest.Nanoseconds() == 0 || r.dur.Nanoseconds() < fastest.Nanoseconds() {
+ fastest = r.dur
+ }
+ if r.dur.Nanoseconds() > slowest.Nanoseconds() {
+ slowest = r.dur
+ }
+ if r.is2xx {
+ totalSuccessful++
+ }
+ default:
+ rps := float64(b.N) / total.Seconds()
+ fmt.Printf("\n")
+ fmt.Printf("Results:\n")
+ fmt.Printf(" total:\t%v requests\n", b.N)
+ fmt.Printf(" concurrency:\t%v concurrent requests\n", b.C)
+ fmt.Printf(" total 2xx:\t%v requests\n", totalSuccessful)
+ fmt.Printf(" total:\t%v\n", total.Nanoseconds())
+ fmt.Printf(" slowest:\t%v\n", slowest.Nanoseconds())
+ fmt.Printf(" fastest:\t%v\n", fastest.Nanoseconds())
+ fmt.Printf(" average:\t%v\n", avgTotal/int64(b.N))
+ fmt.Printf(" requests/sec:\t%v\n", rps)
+ fmt.Printf(" speed index:\t%v\n", speedIndex(rps))
+ return
+ }
+ }
+}
+
+func (b *Boom) run() {
+ var wg sync.WaitGroup
+ wg.Add(b.N)
+
+ for i := 0; i < b.N; i++ {
+ go func() {
+ b.runOneReq()
+ b.bar.Increment()
+ wg.Done()
+ }()
+ }
+
+ wg.Wait()
+}
+
+func (b *Boom) runOneReq() {
+ s := time.Now()
+ resp, err := b.Client.Do(b.Req)
+
+ is2xx := resp != nil && resp.StatusCode >= 200 && resp.StatusCode < 300
+ b.results <- &result{is2xx: is2xx, err: err, dur: time.Now().Sub(s)}
+}
+
+func (b *Boom) handleResp(resp *http.Response, err error, dur time.Duration) {
+ is2xx := false
+ if resp != nil {
+ is2xx = resp.StatusCode >= 200 && resp.StatusCode < 300
+ }
+ b.results <- &result{is2xx: is2xx, err: err, dur: dur}
+}
+
+func speedIndex(rps float64) string {
+ if rps > 500 {
+ return ""
+ } else if rps > 100 {
+ return "Pretty good"
+ } else if rps > 50 {
+ return "Meh"
+ } else {
+ return "Hahahaha"
+ }
+}
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..0420d27
--- /dev/null
+++ b/main.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "net/http"
+ "os"
+
+ "github.com/rakyll/boom/commands"
+)
+
+var (
+ flagMethod = flag.String("m", "GET", "http method: {GET,POST,DELETE,PUT,HEAD,OPTIONS}")
+ flagHeaders = flag.String("h", "", "custom headers name1:value1,name2:value2")
+ flagD = flag.String("d", "", "request body")
+ flagAuth = flag.String("auth", "", "basic authentication user:password")
+
+ flagC = flag.Int("c", 50, "concurrency")
+ flagN = flag.Int("n", 200, "number of requests to make")
+)
+
+func main() {
+ flag.Parse()
+ if flag.NArg() < 1 {
+ fmt.Fprintf(os.Stderr, "no url provided")
+ }
+
+ url := flag.Args()[0]
+ req, err := http.NewRequest(*flagMethod, url, nil)
+ if err != nil {
+ fmt.Errorf("error: %v", err)
+ os.Exit(1)
+ }
+
+ b := &commands.Boom{N: *flagN, C: *flagC, Req: req}
+ b.Run()
+}