fix: race error

This commit is contained in:
debugtalk
2021-12-23 17:26:42 +08:00
parent 2ea2227b1a
commit e9513b508d
4 changed files with 6 additions and 9 deletions

View File

@@ -85,7 +85,7 @@ func TestStandaloneRun(t *testing.T) {
b.Quit() b.Quit()
if count != 10 { if atomic.LoadInt64(&count) != 10 {
t.Error("count is", count, "expected: 10") t.Error("count is", count, "expected: 10")
} }

View File

@@ -197,7 +197,7 @@ func (r *runner) startSpawning(spawnCount int, spawnRate float64, spawnCompleteF
r.stats.clearStatsChan <- true r.stats.clearStatsChan <- true
r.stopChan = make(chan bool) r.stopChan = make(chan bool)
r.numClients = 0 atomic.StoreInt32(&r.numClients, 0)
go r.spawnWorkers(spawnCount, r.stopChan, spawnCompleteFunc) go r.spawnWorkers(spawnCount, r.stopChan, spawnCompleteFunc)
} }
@@ -244,7 +244,7 @@ func (r *localRunner) run() {
for { for {
select { select {
case data := <-r.stats.messageToRunnerChan: case data := <-r.stats.messageToRunnerChan:
data["user_count"] = r.numClients data["user_count"] = atomic.LoadInt32(&r.numClients)
r.outputOnEevent(data) r.outputOnEevent(data)
case <-r.closeChan: case <-r.closeChan:
r.stop() r.stop()

View File

@@ -116,12 +116,7 @@ func (s *requestStats) get(name string, method string) (entry *statsEntry) {
} }
func (s *requestStats) clearAll() { func (s *requestStats) clearAll() {
s.total = &statsEntry{
Name: "Total",
Method: "",
}
s.total.reset() s.total.reset()
s.transactionPassed = 0 s.transactionPassed = 0
s.transactionFailed = 0 s.transactionFailed = 0
s.entries = make(map[string]*statsEntry) s.entries = make(map[string]*statsEntry)
@@ -227,6 +222,8 @@ type statsEntry struct {
} }
func (s *statsEntry) reset() { func (s *statsEntry) reset() {
s.Name = ""
s.Method = ""
s.StartTime = time.Now().Unix() s.StartTime = time.Now().Unix()
s.NumRequests = 0 s.NumRequests = 0
s.NumFailures = 0 s.NumFailures = 0

View File

@@ -44,7 +44,7 @@ func startMemoryProfile(file string, duration time.Duration) (err error) {
log.Info().Dur("duration", duration).Msg("Start memory profiling") log.Info().Dur("duration", duration).Msg("Start memory profiling")
time.AfterFunc(duration, func() { time.AfterFunc(duration, func() {
err = pprof.WriteHeapProfile(f) err := pprof.WriteHeapProfile(f)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to write memory profile") log.Error().Err(err).Msg("failed to write memory profile")
} }