Skip to content

Commit

Permalink
fix found data races
Browse files Browse the repository at this point in the history
  • Loading branch information
brenol committed Oct 18, 2020
1 parent e7c22da commit 55a02ea
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
26 changes: 18 additions & 8 deletions attacker/attacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"net"
"net/http"
"sync"
"time"

vegeta "github.com/tsenart/vegeta/v12/lib"
Expand Down Expand Up @@ -110,29 +111,38 @@ func Attack(ctx context.Context, target string, resCh chan *Result, metricsCh ch

child, cancelChild := context.WithCancel(ctx)
defer cancelChild()
go sendMetrics(child, metrics, metricsCh)

// used to protect metrics
mu := &sync.Mutex{}
go sendMetrics(child, metrics, metricsCh, mu)

for res := range opts.Attacker.Attack(targeter, rate, opts.Duration, "main") {
select {
case <-ctx.Done():
opts.Attacker.Stop()
return
default:
mu.Lock()
metrics.Add(res)
p50 := metrics.Latencies.Quantile(0.50)
p90 := metrics.Latencies.Quantile(0.90)
p95 := metrics.Latencies.Quantile(0.95)
p99 := metrics.Latencies.Quantile(0.99)
mu.Unlock()
resCh <- &Result{
Latency: res.Latency,
P50: metrics.Latencies.Quantile(0.50),
P90: metrics.Latencies.Quantile(0.90),
P95: metrics.Latencies.Quantile(0.95),
P99: metrics.Latencies.Quantile(0.99),
P50: p50,
P90: p90,
P95: p95,
P99: p99,
}
}
}
metrics.Close()
metricsCh <- newMetrics(metrics)
metricsCh <- newMetrics(metrics, mu)
}

func sendMetrics(ctx context.Context, metrics *vegeta.Metrics, ch chan<- *Metrics) {
func sendMetrics(ctx context.Context, metrics *vegeta.Metrics, ch chan<- *Metrics, mu *sync.Mutex) {
// TODO: Make the interval changeable.
ticker := time.NewTicker(250 * time.Millisecond)
defer ticker.Stop()
Expand All @@ -142,7 +152,7 @@ func sendMetrics(ctx context.Context, metrics *vegeta.Metrics, ch chan<- *Metric
case <-ctx.Done():
return
case <-ticker.C:
ch <- newMetrics(metrics)
ch <- newMetrics(metrics, mu)
}
}
}
5 changes: 4 additions & 1 deletion attacker/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package attacker

import (
"sync"
"time"

vegeta "github.com/tsenart/vegeta/v12/lib"
Expand Down Expand Up @@ -68,7 +69,9 @@ type ByteMetrics struct {
Mean float64 `json:"mean"`
}

func newMetrics(m *vegeta.Metrics) *Metrics {
func newMetrics(m *vegeta.Metrics, mu *sync.Mutex) *Metrics {
mu.Lock()
defer mu.Unlock()
return &Metrics{
Latencies: LatencyMetrics{
Total: m.Latencies.Total,
Expand Down
7 changes: 4 additions & 3 deletions gui/drawer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gui
import (
"context"
"fmt"
"sync/atomic"
"time"

"github.com/mum4k/termdash/cell"
Expand All @@ -20,7 +21,7 @@ type drawer struct {
metricsCh chan *attacker.Metrics

// aims to avoid to perform multiple `redrawChart`.
chartDrawing bool
chartDrawing int32
}

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

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

func (d *drawer) redrawGauge(ctx context.Context, maxSize int) {
Expand Down
3 changes: 2 additions & 1 deletion gui/keybinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gui

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

"github.com/mum4k/termdash/container"
Expand Down Expand Up @@ -47,7 +48,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 d.chartDrawing {
if atomic.LoadInt32(&d.chartDrawing) == 1 {
return
}
requestNum := opts.Rate * int(opts.Duration/time.Second)
Expand Down
4 changes: 2 additions & 2 deletions gui/keybinds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ func TestAttack(t *testing.T) {

tests := []struct {
name string
chartDrawing bool
chartDrawing int32
}{
{
name: "chart is drawing",
chartDrawing: true,
chartDrawing: 1,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 55a02ea

Please sign in to comment.