Skip to content

Commit

Permalink
use ubers atomic pkg instead
Browse files Browse the repository at this point in the history
  • Loading branch information
brenol committed Oct 18, 2020
1 parent 55a02ea commit cc7c887
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1
github.com/tsenart/vegeta/v12 v12.8.3
go.uber.org/atomic v1.7.0
go.uber.org/goleak v1.1.10
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b/go.mod h1:ac9efd0D
github.com/c2h5oh/datasize v0.0.0-20171227191756-4eba002a5eae/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-gk v0.0.0-20140819190930-201884a44051 h1:ByJUvQYyTtNNCVfYNM48q6uYUT4fAlN0wNmd3th4BSo=
github.com/dgryski/go-gk v0.0.0-20140819190930-201884a44051/go.mod h1:qm+vckxRlDt0aOla0RYJJVeqHZlWfOm2UIxHaqPB46E=
github.com/dgryski/go-lttb v0.0.0-20180810165845-318fcdf10a77/go.mod h1:Va5MyIzkU0rAM92tn3hb3Anb7oz7KcnixF49+2wOMe4=
Expand Down Expand Up @@ -67,12 +69,15 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 h1:7z3LSn867ex6VSaahyKadf4WtSsJIgne6A1WLOAGM8A=
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tsenart/go-tsz v0.0.0-20180814232043-cdeb9e1e981e/go.mod h1:SWZznP1z5Ki7hDT2ioqiFKEse8K9tU2OUvaRI0NeGQo=
github.com/tsenart/vegeta/v12 v12.8.3 h1:UEsDkSrEJojMKW/xr7KUv4H/bYykX+V48KCsPZPqEfk=
github.com/tsenart/vegeta/v12 v12.8.3/go.mod h1:ZiJtwLn/9M4fTPdMY7bdbIeyNeFVE8/AHbWFqCsUuho=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
8 changes: 4 additions & 4 deletions gui/drawer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package gui
import (
"context"
"fmt"
"sync/atomic"
"time"

"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/widgets/linechart"
"github.com/mum4k/termdash/widgets/text"
"go.uber.org/atomic"

"github.com/nakabonne/ali/attacker"
)
Expand All @@ -21,7 +21,7 @@ type drawer struct {
metricsCh chan *attacker.Metrics

// aims to avoid to perform multiple `redrawChart`.
chartDrawing int32
chartDrawing *atomic.Bool
}

// redrawChart appends entities as soon as a result arrives.
Expand All @@ -39,7 +39,7 @@ func (d *drawer) redrawChart(ctx context.Context, maxSize int) {
return append(to, float64(val)/float64(time.Millisecond))
}

atomic.StoreInt32(&d.chartDrawing, 1)
d.chartDrawing.Store(true)
L:
for {
select {
Expand Down Expand Up @@ -84,7 +84,7 @@ L:
)
}
}
atomic.StoreInt32(&d.chartDrawing, 0)
d.chartDrawing.Store(false)
}

func (d *drawer) redrawGauge(ctx context.Context, maxSize int) {
Expand Down
13 changes: 8 additions & 5 deletions gui/drawer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/golang/mock/gomock"
"go.uber.org/atomic"

"github.com/nakabonne/ali/attacker"
)
Expand Down Expand Up @@ -94,9 +95,10 @@ func TestRedrawChart(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &drawer{
widgets: &widgets{latencyChart: tt.latencyChart, percentilesChart: tt.percentilesChart},
chartCh: make(chan *attacker.Result),
gaugeCh: make(chan bool, 100),
widgets: &widgets{latencyChart: tt.latencyChart, percentilesChart: tt.percentilesChart},
chartCh: make(chan *attacker.Result),
gaugeCh: make(chan bool, 100),
chartDrawing: atomic.NewBool(false),
}
go d.redrawChart(ctx, len(tt.results))
for _, res := range tt.results {
Expand Down Expand Up @@ -146,8 +148,9 @@ func TestRedrawGauge(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &drawer{
widgets: &widgets{progressGauge: tt.gauge},
gaugeCh: make(chan bool),
widgets: &widgets{progressGauge: tt.gauge},
gaugeCh: make(chan bool),
chartDrawing: atomic.NewBool(false),
}
go d.redrawGauge(ctx, tt.size)
for i := 0; i < tt.size; i++ {
Expand Down
12 changes: 7 additions & 5 deletions gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/mum4k/termdash/linestyle"
"github.com/mum4k/termdash/terminal/termbox"
"github.com/mum4k/termdash/terminal/terminalapi"
"go.uber.org/atomic"

"github.com/nakabonne/ali/attacker"
)
Expand Down Expand Up @@ -55,11 +56,12 @@ func run(t *termbox.Terminal, r runner, targetURL string, opts *attacker.Options
}

d := &drawer{
widgets: w,
gridOpts: gridOpts,
chartCh: make(chan *attacker.Result),
gaugeCh: make(chan bool),
metricsCh: make(chan *attacker.Metrics),
widgets: w,
gridOpts: gridOpts,
chartCh: make(chan *attacker.Result),
gaugeCh: make(chan bool),
metricsCh: make(chan *attacker.Metrics),
chartDrawing: atomic.NewBool(false),
}
go d.redrawMetrics(ctx)

Expand Down
3 changes: 1 addition & 2 deletions gui/keybinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gui

import (
"context"
"sync/atomic"
"time"

"github.com/mum4k/termdash/container"
Expand Down Expand Up @@ -48,7 +47,7 @@ func keybinds(ctx context.Context, cancel context.CancelFunc, c *container.Conta
}

func attack(ctx context.Context, d *drawer, target string, opts attacker.Options) {
if atomic.LoadInt32(&d.chartDrawing) == 1 {
if d.chartDrawing.Load() {
return
}
requestNum := opts.Rate * int(opts.Duration/time.Second)
Expand Down
5 changes: 3 additions & 2 deletions gui/keybinds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/mum4k/termdash/keyboard"
"github.com/mum4k/termdash/terminal/terminalapi"
"go.uber.org/atomic"

"github.com/nakabonne/ali/attacker"
)
Expand Down Expand Up @@ -187,11 +188,11 @@ func TestAttack(t *testing.T) {

tests := []struct {
name string
chartDrawing int32
chartDrawing *atomic.Bool
}{
{
name: "chart is drawing",
chartDrawing: 1,
chartDrawing: atomic.NewBool(true),
},
}
for _, tt := range tests {
Expand Down

0 comments on commit cc7c887

Please sign in to comment.