refactor: simplify boomer runner

This commit is contained in:
debugtalk
2021-12-24 10:05:27 +08:00
parent dc2933550c
commit ac1fd28d96
8 changed files with 40 additions and 57 deletions
+19 -15
View File
@@ -201,20 +201,17 @@ type localRunner struct {
stopChan chan bool
}
func newLocalRunner(tasks []*Task, rateLimiter RateLimiter, spawnCount int, spawnRate float64) (r *localRunner) {
r = &localRunner{}
r.setTasks(tasks)
r.spawnRate = spawnRate
r.spawnCount = spawnCount
r.stopChan = make(chan bool)
if rateLimiter != nil {
r.rateLimitEnabled = true
r.rateLimiter = rateLimiter
func newLocalRunner(spawnCount int, spawnRate float64) *localRunner {
return &localRunner{
runner: runner{
state: stateInit,
spawnRate: spawnRate,
spawnCount: spawnCount,
stats: newRequestStats(),
outputs: make([]Output, 0),
},
stopChan: make(chan bool),
}
r.stats = newRequestStats()
return r
}
func (r *localRunner) start() {
@@ -240,6 +237,7 @@ func (r *localRunner) start() {
var ticker = time.NewTicker(reportStatsInterval)
for {
select {
// record stats
case t := <-r.stats.transactionChan:
r.stats.logTransaction(t.name, t.success, t.elapsedTime, t.contentSize)
case m := <-r.stats.requestSuccessChan:
@@ -247,28 +245,34 @@ func (r *localRunner) start() {
case n := <-r.stats.requestFailureChan:
r.stats.logRequest(n.requestType, n.name, n.responseTime, 0)
r.stats.logError(n.requestType, n.name, n.errMsg)
// report stats
case <-ticker.C:
data := r.stats.collectReportData()
data["user_count"] = atomic.LoadInt32(&r.currentClientsNum)
data["state"] = atomic.LoadInt32(&r.state)
r.outputOnEevent(data)
// stop
case <-r.stopChan:
atomic.StoreInt32(&r.state, stateQuitting)
// stop previous goroutines without blocking
// those goroutines will exit when r.safeRun returns
close(quitChan)
// stop rate limiter
if r.rateLimitEnabled {
r.rateLimiter.Stop()
}
// output teardown
r.outputOnStop()
atomic.StoreInt32(&r.state, stateStopped)
return
}
}
}
func (r *localRunner) stop() {
atomic.StoreInt32(&r.state, stateQuitting)
close(r.stopChan)
atomic.StoreInt32(&r.state, stateStopped)
}