fix: resolve the problem of reporting last stats failure.

This commit is contained in:
徐聪
2022-01-13 10:54:18 +08:00
parent 66bc29ddf4
commit b70dc3a47b
2 changed files with 45 additions and 35 deletions
+1
View File
@@ -65,6 +65,7 @@ func init() {
boomCmd.Flags().StringVar(&requestIncreaseRate, "request-increase-rate", "-1", "Request increase rate, disabled by default.") boomCmd.Flags().StringVar(&requestIncreaseRate, "request-increase-rate", "-1", "Request increase rate, disabled by default.")
boomCmd.Flags().IntVar(&spawnCount, "spawn-count", 1, "The number of users to spawn for load testing") boomCmd.Flags().IntVar(&spawnCount, "spawn-count", 1, "The number of users to spawn for load testing")
boomCmd.Flags().Float64Var(&spawnRate, "spawn-rate", 1, "The rate for spawning users") boomCmd.Flags().Float64Var(&spawnRate, "spawn-rate", 1, "The rate for spawning users")
boomCmd.Flags().Int64Var(&loopCount, "loop-count", -1, "The specify running cycles for load testing")
boomCmd.Flags().StringVar(&memoryProfile, "mem-profile", "", "Enable memory profiling.") boomCmd.Flags().StringVar(&memoryProfile, "mem-profile", "", "Enable memory profiling.")
boomCmd.Flags().DurationVar(&memoryProfileDuration, "mem-profile-duration", 30*time.Second, "Memory profile duration.") boomCmd.Flags().DurationVar(&memoryProfileDuration, "mem-profile-duration", 30*time.Second, "Memory profile duration.")
boomCmd.Flags().StringVar(&cpuProfile, "cpu-profile", "", "Enable CPU profiling.") boomCmd.Flags().StringVar(&cpuProfile, "cpu-profile", "", "Enable CPU profiling.")
+16 -7
View File
@@ -273,12 +273,15 @@ func (r *localRunner) start() {
// all running workers(goroutines) will select on this channel. // all running workers(goroutines) will select on this channel.
// close this channel will stop all running workers. // close this channel will stop all running workers.
quitChan := make(chan bool) quitChan := make(chan bool)
// when this channel is closed, all statistics are reported successfully
reportedChan := make(chan bool)
go r.spawnWorkers(r.spawnCount, r.spawnRate, quitChan, nil) go r.spawnWorkers(r.spawnCount, r.spawnRate, quitChan, nil)
// output setup // output setup
r.outputOnStart() r.outputOnStart()
// start running // start running
go func() {
var ticker = time.NewTicker(reportStatsInterval) var ticker = time.NewTicker(reportStatsInterval)
for { for {
select { select {
@@ -293,30 +296,36 @@ func (r *localRunner) start() {
// report stats // report stats
case <-ticker.C: case <-ticker.C:
r.reportStats() r.reportStats()
// close reportedChan and return if the last stats is reported successfully
if atomic.LoadInt32(&r.state) == stateQuitting {
close(reportedChan)
return
}
}
}
}()
// stop // stop
case <-r.stopChan: <-r.stopChan
atomic.StoreInt32(&r.state, stateQuitting) atomic.StoreInt32(&r.state, stateQuitting)
// stop previous goroutines without blocking // stop previous goroutines without blocking
// those goroutines will exit when r.safeRun returns // those goroutines will exit when r.safeRun returns
close(quitChan) close(quitChan)
// wait until all stats are reported successfully
<-reportedChan
// stop rate limiter // stop rate limiter
if r.rateLimitEnabled { if r.rateLimitEnabled {
r.rateLimiter.Stop() r.rateLimiter.Stop()
} }
// report last stats
<-ticker.C
r.reportStats()
// output teardown // output teardown
r.outputOnStop() r.outputOnStop()
atomic.StoreInt32(&r.state, stateStopped) atomic.StoreInt32(&r.state, stateStopped)
return return
}
}
} }
func (r *localRunner) stop() { func (r *localRunner) stop() {