go_dreamfactory/modules/battletest/worker.go
2024-02-29 17:52:44 +08:00

39 lines
724 B
Go

package battletest
import (
"context"
"go_dreamfactory/pb"
"math/rand"
"sync"
"time"
)
// 压测组件
type worker struct {
module *BattleTest
report *pb.BattleReport
statistics []int64
testnum int32
}
func (this *worker) Worker(wg sync.WaitGroup) {
var (
stime time.Time
duration int64
)
locp:
for {
stime = time.Now()
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
this.module.clientmgr.CheckBattle(ctx, this.report)
duration = time.Since(stime).Milliseconds()
this.statistics = append(this.statistics, duration)
this.testnum--
if this.testnum <= 0 {
break locp
}
time.Sleep(time.Millisecond * time.Duration(100+rand.Int31n(1000)))
}
wg.Done()
}