Skip to content

Commit

Permalink
Fix Progress gauge to reach 100%
Browse files Browse the repository at this point in the history
Signed-off-by: shubham <shubham.bajpai@mayadata.io>
  • Loading branch information
shubham14bajpai committed Nov 16, 2020
1 parent 6101998 commit fcc5a64
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
22 changes: 14 additions & 8 deletions gui/drawer.go
Expand Up @@ -45,13 +45,14 @@ type values struct {

// appendChartValues appends entities as soon as a result arrives.
// Given maxSize, then it can be pre-allocated.
func (d *drawer) appendChartValues(ctx context.Context, maxSize int) {
func (d *drawer) appendChartValues(ctx context.Context, rate int, duration time.Duration) {
// TODO: Change how to stop `redrawGauge`.
// We currently use this way to ensure to stop `redrawGauge` after the increase process is complete.
// But, it's preferable to stop goroutine where it's generated.
maxSize := rate * int(duration/time.Second)
child, cancel := context.WithCancel(ctx)
defer cancel()
go d.redrawGauge(child, maxSize)
go d.redrawGauge(child, duration)

d.chartValues.latencies = make([]float64, 0, maxSize)
d.chartValues.p50 = make([]float64, 0, maxSize)
Expand Down Expand Up @@ -125,17 +126,22 @@ L:
d.chartDrawing.Store(false)
}

func (d *drawer) redrawGauge(ctx context.Context, maxSize int) {
var count float64
size := float64(maxSize)
func (d *drawer) redrawGauge(ctx context.Context, duration time.Duration) {
totalTime := float64(duration)
d.widgets.progressGauge.Percent(0)
for {
for start := time.Now(); ; {
select {
case <-ctx.Done():
return
case <-d.gaugeCh:
count++
d.widgets.progressGauge.Percent(int(count / size * 100))
passed := float64(time.Since(start))
percent := int(passed / totalTime * 100)
// as time.Duration is the unit of nanoseconds
// small duration can exceed 100 on slow machines
if percent > 100 {
percent = 100
}
d.widgets.progressGauge.Percent(percent)
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions gui/drawer_test.go
Expand Up @@ -21,6 +21,7 @@ func TestAppendChartValues(t *testing.T) {
tests := []struct {
name string
results []*attacker.Result
duration time.Duration
progressGauge Gauge
}{
{
Expand All @@ -34,6 +35,7 @@ func TestAppendChartValues(t *testing.T) {
P99: 990000,
},
},
duration: 1,
progressGauge: func() Gauge {
g := NewMockGauge(ctrl)
g.EXPECT().Percent(gomock.Any()).AnyTimes()
Expand All @@ -58,6 +60,7 @@ func TestAppendChartValues(t *testing.T) {
P99: 1980000,
},
},
duration: 2,
progressGauge: func() Gauge {
g := NewMockGauge(ctrl)
g.EXPECT().Percent(gomock.Any()).AnyTimes()
Expand All @@ -75,7 +78,7 @@ func TestAppendChartValues(t *testing.T) {
doneCh: make(chan struct{}),
chartDrawing: atomic.NewBool(false),
}
go d.appendChartValues(ctx, len(tt.results))
go d.appendChartValues(ctx, len(tt.results), tt.duration)
for _, res := range tt.results {
d.chartCh <- res
}
Expand Down Expand Up @@ -155,7 +158,7 @@ func TestRedrawGauge(t *testing.T) {

tests := []struct {
name string
size int
size time.Duration
count int
gauge Gauge
}{
Expand All @@ -178,7 +181,7 @@ func TestRedrawGauge(t *testing.T) {
gaugeCh: make(chan struct{}),
}
go d.redrawGauge(ctx, tt.size)
for i := 0; i < tt.size; i++ {
for i := 0; i < int(tt.size); i++ {
d.gaugeCh <- struct{}{}
}
})
Expand Down
4 changes: 1 addition & 3 deletions gui/keybinds.go
Expand Up @@ -2,7 +2,6 @@ package gui

import (
"context"
"time"

"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/keyboard"
Expand Down Expand Up @@ -50,11 +49,10 @@ func attack(ctx context.Context, d *drawer, target string, opts attacker.Options
if d.chartDrawing.Load() {
return
}
requestNum := opts.Rate * int(opts.Duration/time.Second)
d.doneCh = make(chan struct{})

// To initialize, run redrawChart on a per-attack basis.
go d.appendChartValues(ctx, requestNum)
go d.appendChartValues(ctx, opts.Rate, opts.Duration)
go d.redrawCharts(ctx)
go func(ctx context.Context, d *drawer, t string, o attacker.Options) {
attacker.Attack(ctx, t, d.chartCh, d.metricsCh, o) // this blocks until attack finishes
Expand Down

0 comments on commit fcc5a64

Please sign in to comment.