Skip to content

Commit

Permalink
Enable to specify timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Sep 15, 2020
1 parent 8965080 commit fe260b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
7 changes: 6 additions & 1 deletion attacker/attacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
const (
DefaultRate = 50
DefaultDuration = 10 * time.Second
DefaultTimeout = 30 * time.Second
DefaultMethod = http.MethodGet
)

Expand All @@ -23,6 +24,7 @@ type Attacker interface {
type Options struct {
Rate int
Duration time.Duration
Timeout time.Duration
Method string
Body []byte
Header http.Header
Expand Down Expand Up @@ -50,11 +52,14 @@ func Attack(ctx context.Context, target string, resCh chan *Result, opts Options
if opts.Duration == 0 {
opts.Duration = DefaultDuration
}
if opts.Timeout == 0 {
opts.Timeout = DefaultTimeout
}
if opts.Method == "" {
opts.Method = DefaultMethod
}
if opts.Attacker == nil {
opts.Attacker = vegeta.NewAttacker()
opts.Attacker = vegeta.NewAttacker(vegeta.Timeout(opts.Timeout))
}

rate := vegeta.Rate{Freq: opts.Rate, Per: time.Second}
Expand Down
5 changes: 3 additions & 2 deletions gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func gridLayout(w *widgets) ([]container.Option, error) {
grid.ColWidthPerc(64,
grid.RowHeightPerc(29, grid.Widget(w.urlInput, container.Border(linestyle.None))),
grid.RowHeightPerc(29,
grid.ColWidthPerc(50, grid.Widget(w.rateLimitInput, container.Border(linestyle.None))),
grid.ColWidthPerc(49, grid.Widget(w.durationInput, container.Border(linestyle.None))),
grid.ColWidthPerc(33, grid.Widget(w.rateLimitInput, container.Border(linestyle.None))),
grid.ColWidthPerc(33, grid.Widget(w.durationInput, container.Border(linestyle.None))),
grid.ColWidthPerc(33, grid.Widget(w.timeoutInput, container.Border(linestyle.None))),
),
grid.RowHeightPerc(29,
grid.ColWidthPerc(33, grid.Widget(w.methodInput, container.Border(linestyle.None))),
Expand Down
11 changes: 11 additions & 0 deletions gui/input_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func makeOptions(w *widgets) (attacker.Options, error) {
var (
rate int
duration time.Duration
timeout time.Duration
method string
body string
header = make(http.Header)
Expand All @@ -39,6 +40,15 @@ func makeOptions(w *widgets) (attacker.Options, error) {
}
}

if s := w.timeoutInput.Read(); s == "" {
timeout = attacker.DefaultTimeout
} else {
timeout, err = time.ParseDuration(s)
if err != nil {
return attacker.Options{}, fmt.Errorf("Unparseable timeout %q: %w", s, err)
}
}

if method = w.methodInput.Read(); method != "" {
if !validateMethod(method) {
return attacker.Options{}, fmt.Errorf("Given method %q isn't an HTTP request method", method)
Expand All @@ -65,6 +75,7 @@ func makeOptions(w *widgets) (attacker.Options, error) {
return attacker.Options{
Rate: rate,
Duration: duration,
Timeout: timeout,
Method: method,
Body: []byte(body),
Header: header,
Expand Down

0 comments on commit fe260b7

Please sign in to comment.