From 970734607e8c06a8a33768a4905d9cc30dbfcf21 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 23 Dec 2021 17:26:42 +0800 Subject: [PATCH] fix: race error --- internal/boomer/boomer_test.go | 2 +- internal/boomer/runner.go | 4 ++-- internal/boomer/stats.go | 7 ++----- internal/boomer/utils.go | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/internal/boomer/boomer_test.go b/internal/boomer/boomer_test.go index 5670c763..384ecbd4 100644 --- a/internal/boomer/boomer_test.go +++ b/internal/boomer/boomer_test.go @@ -85,7 +85,7 @@ func TestStandaloneRun(t *testing.T) { b.Quit() - if count != 10 { + if atomic.LoadInt64(&count) != 10 { t.Error("count is", count, "expected: 10") } diff --git a/internal/boomer/runner.go b/internal/boomer/runner.go index d3f11801..c8c24fab 100644 --- a/internal/boomer/runner.go +++ b/internal/boomer/runner.go @@ -197,7 +197,7 @@ func (r *runner) startSpawning(spawnCount int, spawnRate float64, spawnCompleteF r.stats.clearStatsChan <- true r.stopChan = make(chan bool) - r.numClients = 0 + atomic.StoreInt32(&r.numClients, 0) go r.spawnWorkers(spawnCount, r.stopChan, spawnCompleteFunc) } @@ -244,7 +244,7 @@ func (r *localRunner) run() { for { select { case data := <-r.stats.messageToRunnerChan: - data["user_count"] = r.numClients + data["user_count"] = atomic.LoadInt32(&r.numClients) r.outputOnEevent(data) case <-r.closeChan: r.stop() diff --git a/internal/boomer/stats.go b/internal/boomer/stats.go index 9978a584..18d9a5c3 100644 --- a/internal/boomer/stats.go +++ b/internal/boomer/stats.go @@ -116,12 +116,7 @@ func (s *requestStats) get(name string, method string) (entry *statsEntry) { } func (s *requestStats) clearAll() { - s.total = &statsEntry{ - Name: "Total", - Method: "", - } s.total.reset() - s.transactionPassed = 0 s.transactionFailed = 0 s.entries = make(map[string]*statsEntry) @@ -227,6 +222,8 @@ type statsEntry struct { } func (s *statsEntry) reset() { + s.Name = "" + s.Method = "" s.StartTime = time.Now().Unix() s.NumRequests = 0 s.NumFailures = 0 diff --git a/internal/boomer/utils.go b/internal/boomer/utils.go index 7d7bfe6f..9a6f3fef 100644 --- a/internal/boomer/utils.go +++ b/internal/boomer/utils.go @@ -44,7 +44,7 @@ func startMemoryProfile(file string, duration time.Duration) (err error) { log.Info().Dur("duration", duration).Msg("Start memory profiling") time.AfterFunc(duration, func() { - err = pprof.WriteHeapProfile(f) + err := pprof.WriteHeapProfile(f) if err != nil { log.Error().Err(err).Msg("failed to write memory profile") }