Skip to content

Commit

Permalink
Ensure to attack with pushing key
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Sep 12, 2020
1 parent 9f478d9 commit f774434
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
20 changes: 17 additions & 3 deletions gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package gui
import (
"context"
"fmt"
"time"

"github.com/k0kubun/pp"
"github.com/mum4k/termdash"
"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/container/grid"
"github.com/mum4k/termdash/keyboard"
"github.com/mum4k/termdash/linestyle"
"github.com/mum4k/termdash/terminal/termbox"
"github.com/mum4k/termdash/terminal/terminalapi"

"github.com/nakabonne/ali/attacker"
)

const rootID = "root"
Expand Down Expand Up @@ -42,12 +46,22 @@ func Run() error {
return fmt.Errorf("failed to update container: %w", err)
}

quitter := func(k *terminalapi.Keyboard) {
if k.Key == keyboard.KeyEsc || k.Key == keyboard.KeyCtrlC {
keybinds := func(k *terminalapi.Keyboard) {
switch k.Key {
case keyboard.KeyEsc, keyboard.KeyCtrlC:
cancel()
case keyboard.KeyEnter:
resultCh := make(chan *attacker.Result)
go func() {
// TODO: Enalble to poplulate from input
metrics := attacker.Attack(ctx, "http://34.84.111.163:9898", resultCh, attacker.Options{Rate: 50, Duration: 10 * time.Second})
pp.Println(metrics)
}()
go redrawChart(ctx, w.plotChart, resultCh)
}
}
return termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter), termdash.RedrawInterval(redrawInterval))

return termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(keybinds), termdash.RedrawInterval(redrawInterval))
}

func gridLayout(w *widgets) ([]container.Option, error) {
Expand Down
20 changes: 3 additions & 17 deletions gui/widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type widgets struct {
}

func newWidgets(ctx context.Context, c *container.Container) (*widgets, error) {
l, err := newLineChart(ctx)
l, err := newLineChart()
if err != nil {
return nil, err
}
Expand All @@ -38,26 +38,12 @@ func newWidgets(ctx context.Context, c *container.Container) (*widgets, error) {
}, nil
}

// newLineChart returns a line plotChart that displays a heartbeat-like progression.
func newLineChart(ctx context.Context) (*linechart.LineChart, error) {
lc, err := linechart.New(
func newLineChart() (*linechart.LineChart, error) {
return linechart.New(
linechart.AxesCellOpts(cell.FgColor(cell.ColorRed)),
linechart.YLabelCellOpts(cell.FgColor(cell.ColorGreen)),
linechart.XLabelCellOpts(cell.FgColor(cell.ColorGreen)),
)
if err != nil {
return nil, err
}

resultCh := make(chan *attacker.Result)
go func() {
// TODO: Enalble to poplulate from input
metrics := attacker.Attack(ctx, "http://34.84.111.163:9898", resultCh, attacker.Options{Rate: 50, Duration: 10 * time.Second})
pp.Println(metrics)
}()
go redrawChart(ctx, lc, resultCh)

return lc, nil
}

func redrawChart(ctx context.Context, lineChart *linechart.LineChart, resultCh chan *attacker.Result) {
Expand Down

0 comments on commit f774434

Please sign in to comment.