Skip to content

Commit

Permalink
feat: add bucket histogram option
Browse files Browse the repository at this point in the history
  • Loading branch information
rzkmak committed Oct 14, 2020
1 parent f4ad79d commit fd2f4eb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion attacker/attacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Options struct {
Connections int
HTTP2 bool
LocalAddr net.IPAddr
Buckets []time.Duration

Attacker Attacker
}
Expand Down Expand Up @@ -100,7 +101,13 @@ func Attack(ctx context.Context, target string, resCh chan *Result, metricsCh ch
Header: opts.Header,
})

metrics := &vegeta.Metrics{}
var metrics *vegeta.Metrics
if len(opts.Buckets) > 0 {
histogram := &vegeta.Histogram{Buckets: opts.Buckets}
metrics = &vegeta.Metrics{Histogram: histogram}
} else {
metrics = &vegeta.Metrics{}
}

child, cancelChild := context.WithCancel(ctx)
defer cancelChild()
Expand Down
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -45,6 +46,7 @@ type cli struct {
noHTTP2 bool
localAddress string
noKeepAlive bool
buckets string

debug bool
version bool
Expand Down Expand Up @@ -81,6 +83,7 @@ func parseFlags(stdout, stderr io.Writer) (*cli, error) {
flagSet.IntVarP(&c.connections, "connections", "c", attacker.DefaultConnections, "Amount of maximum open idle connections per target host")
flagSet.BoolVar(&c.noHTTP2, "no-http2", false, "Don't issue HTTP/2 requests to servers which support it.")
flagSet.StringVar(&c.localAddress, "local-addr", "0.0.0.0", "Local IP address.")
flagSet.StringVar(&c.buckets, "buckets", "", "Histogram buckets.")
flagSet.Usage = c.usage
if err := flagSet.Parse(os.Args[1:]); err != nil {
if !errors.Is(err, flag.ErrHelp) {
Expand Down Expand Up @@ -191,6 +194,7 @@ func (c *cli) makeOptions() (*attacker.Options, error) {
Connections: c.connections,
HTTP2: !c.noHTTP2,
LocalAddr: localAddr,
Buckets: parseBucketOptions(c.buckets),
}, nil
}

Expand All @@ -202,6 +206,27 @@ func validateMethod(method string) bool {
return false
}

func parseBucketOptions(rawBuckets string) []time.Duration {
if rawBuckets == "" {
return []time.Duration{}
}

var result []time.Duration

stringBuckets := strings.Split(rawBuckets, ",")

for _, bucket := range stringBuckets {
trimmedBucket := strings.TrimSpace(bucket)
d, err := time.ParseDuration(trimmedBucket)
if err != nil {
log.Fatalf("Invalid bucket time format")
}
result = append(result, d)
}

return result
}

// Makes a new file under the working directory only when debug use.
func setDebug(w io.Writer, debug bool) {
if !debug {
Expand Down

0 comments on commit fd2f4eb

Please sign in to comment.