Skip to content

Commit

Permalink
Stop redrawing charts gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Sep 14, 2020
1 parent 629ebed commit ce62399
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
ali

# Test binary, built with `go test -c`
*.test
Expand Down
5 changes: 2 additions & 3 deletions attacker/attacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"net/http"
"time"

"github.com/k0kubun/pp"

vegeta "github.com/tsenart/vegeta/v12/lib"
)

Expand Down Expand Up @@ -34,6 +32,8 @@ type Options struct {
// Result contains the results of a single HTTP request.
type Result struct {
Latency time.Duration
// Indicates if the last result in the entire attack.
End bool
}

// Attack keeps the request running for the specified period of time.
Expand Down Expand Up @@ -76,7 +76,6 @@ func Attack(ctx context.Context, target string, resCh chan *Result, opts Options
}
}
metrics.Close()
pp.Println("vegeta metrics", metrics)

return newMetrics(&metrics)
}
16 changes: 13 additions & 3 deletions gui/drawer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"
"time"

"github.com/mum4k/termdash/widgets/text"

"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/widgets/linechart"
"github.com/mum4k/termdash/widgets/text"

"github.com/nakabonne/ali/attacker"
)
Expand All @@ -16,16 +15,26 @@ type drawer struct {
widgets *widgets
chartsCh chan *attacker.Result
reportCh chan string

// Indicates if charts are drawing.
chartDrawing bool
}

// redrawChart appends entities as soon as a result arrives.
// Given maxSize, then it can be pre-allocated.
// TODO: In the future, multiple charts including bytes-in/out etc will be re-drawn.
func (d *drawer) redrawChart(ctx context.Context, maxSize int) {
values := make([]float64, 0, maxSize)
d.chartDrawing = true
L:
for {
select {
case <-ctx.Done():
return
break L
case res := <-d.chartsCh:
if res.End {
break L
}
values = append(values, float64(res.Latency/time.Millisecond))
d.widgets.latencyChart.Series("latency", values,
linechart.SeriesCellOpts(cell.FgColor(cell.ColorNumber(87))),
Expand All @@ -35,6 +44,7 @@ func (d *drawer) redrawChart(ctx context.Context, maxSize int) {
)
}
}
d.chartDrawing = false
}

func (d *drawer) redrawReport(ctx context.Context) {
Expand Down
8 changes: 6 additions & 2 deletions gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ func Run() error {
case keyboard.KeyEsc, keyboard.KeyCtrlC:
cancel()
case keyboard.KeySpace:
if d.chartDrawing {
return
}
// TODO: Enalble to poplulate from input
rate := 50
rate := 10
duration, _ := time.ParseDuration("2s")
requestNum := rate * int(duration/time.Second)
// To pre-allocate, run redrawChart on a per-attack basis.
go d.redrawChart(ctx, requestNum)
go func(ctx context.Context, d *drawer, r int, du time.Duration) {
metrics := attacker.Attack(ctx, "http://34.84.111.163:9898", d.chartsCh, attacker.Options{Rate: r, Duration: du})
//close(ch) TODO: Gracefully stop redrawChart goroutine.
d.reportCh <- metrics.String()
d.chartsCh <- &attacker.Result{End: true}
}(ctx, d, rate, duration)
}
}
Expand Down

0 comments on commit ce62399

Please sign in to comment.