This commit is contained in:
wh_zcy 2023-01-04 10:37:21 +08:00
parent 340494ec96
commit 9fc9751ab7
3 changed files with 21 additions and 23 deletions

View File

@ -16,17 +16,16 @@ import (
) )
type MyAI struct { type MyAI struct {
robots []*Robot robots []*Robot
scenes []*scene scenes []*scene
iscenes []IScene iscenes []IScene
tickets Tickets //票池 tickets Tickets //票池
Obs Observer Obs Observer
// useCount uint32 //计数(压入的用户数) userCountTotal uint32 //总数
useCountTotal uint32 //总数 cache sync.Mutex //数据缓存锁
cache sync.Mutex //数据缓存锁 lock sync.Mutex //合并数据锁
lock sync.Mutex //合并数据锁 config *storage.Config //配置
config *storage.Config //配置 ReportMap map[int]map[string]*Statistics //测试报告 key1:场景 key2:协议
ReportMap map[int]map[string]*Statistics //测试报告 key1:场景 key2:协议
} }
func NewAI(aip AIParam) (*MyAI, error) { func NewAI(aip AIParam) (*MyAI, error) {
@ -76,7 +75,6 @@ func (m *MyAI) Start() bool {
logrus.Warn("还未设置场景") logrus.Warn("还未设置场景")
return false return false
} }
logrus.Info("测试中...")
go func() { go func() {
for { for {
m.tickets.Take(int32(m.config.Global.UserCount), m.config.Global.IntervalS) m.tickets.Take(int32(m.config.Global.UserCount), m.config.Global.IntervalS)
@ -84,7 +82,7 @@ func (m *MyAI) Start() bool {
robot := NewRobot(m) robot := NewRobot(m)
robot.SetScenes() robot.SetScenes()
m.AppendRobot(robot) m.AppendRobot(robot)
atomic.AddUint32(&m.useCountTotal, 1) atomic.AddUint32(&m.userCountTotal, 1)
m.Obs.Notify(EVENT_ROBOT, int32(1)) m.Obs.Notify(EVENT_ROBOT, int32(1))
robot.Start() robot.Start()
}() }()

View File

@ -54,15 +54,15 @@ func NewRobot(ai *MyAI) *Robot {
} }
robot.Store("sid", ai.config.Global.SId) robot.Store("sid", ai.config.Global.SId)
ai.Obs.AddListener(EVENT_CLOSECON, Listener{ // ai.Obs.AddListener(EVENT_CLOSECON, Listener{
OnNotify: func(data interface{}, args ...interface{}) { // OnNotify: func(data interface{}, args ...interface{}) {
d := data.(bool) // d := data.(bool)
if d { // if d {
robot.prepareToStop() // robot.prepareToStop()
robot.conn.Close() // robot.conn.Close()
} // }
}, // },
}) // })
return robot return robot
} }

View File

@ -59,13 +59,13 @@ func (gt *myTickets) init(total uint32) bool {
} }
func (gt *myTickets) Take(useCount, inteval int32) { func (gt *myTickets) Take(useCount, inteval int32) {
atomic.AddInt32(&gt.count, 1)
count := atomic.LoadInt32(&gt.count) count := atomic.LoadInt32(&gt.count)
if count >= int32(useCount) { if count >= int32(useCount) {
time.Sleep(time.Duration(inteval) * time.Second) time.Sleep(time.Duration(inteval) * time.Second)
atomic.StoreInt32(&gt.count, 0) atomic.StoreInt32(&gt.count, 0)
} }
<-gt.ticketCh <-gt.ticketCh
atomic.AddInt32(&gt.count, 1)
} }
func (gt *myTickets) Return() { func (gt *myTickets) Return() {