diff --git a/lib/ai.go b/lib/ai.go index 07db9e0..ba66bde 100644 --- a/lib/ai.go +++ b/lib/ai.go @@ -16,12 +16,12 @@ import ( ) type myAI struct { - robots []*Robot - scenes []*scene - iscenes []IScene - tickets Tickets //票池 - Obs Observer - useCount uint32 //计数(压入的用户数) + robots []*Robot + scenes []*scene + iscenes []IScene + tickets Tickets //票池 + Obs Observer + // useCount uint32 //计数(压入的用户数) useCountTotal uint32 //总数 lock sync.Mutex //合并数据锁 config *storage.Config //配置 @@ -79,20 +79,15 @@ func (m *myAI) Start() bool { logrus.Info("测试中...") go func() { for { - m.tickets.Take() - uCount := atomic.LoadUint32(&m.useCount) - if uCount >= uint32(m.config.Global.UserCount) { - atomic.StoreUint32(&m.useCount, 0) - time.Sleep(time.Duration(m.config.Global.IntervalS) * time.Second) - } - //加入机器人 - atomic.AddUint32(&m.useCount, 1) - robot := NewRobot(m.config) - robot.SetScenes(m.iscenes) - m.AppendRobot(robot) - robot.Start() - atomic.AddUint32(&m.useCountTotal, 1) - m.Obs.Notify(EVENT_PROGRESS, int32(1)) + m.tickets.Take(int32(m.config.Global.UserCount), m.config.Global.IntervalS) + go func() { + robot := NewRobot(m.config) + robot.SetScenes(m.iscenes) + m.AppendRobot(robot) + robot.Start() + atomic.AddUint32(&m.useCountTotal, 1) + m.Obs.Notify(EVENT_PROGRESS, int32(1)) + }() } }() @@ -102,7 +97,7 @@ func (m *myAI) Start() bool { total := atomic.LoadUint32(&m.useCountTotal) if total == m.config.Global.UserCountTotal { elipse := time.Since(start) - logrus.Info("开始生成测试报告") + logrus.Debug("开始生成测试报告") m.MergeResult() m.genReport(elipse) m.Obs.Notify(EVENT_PROGRESS, int32(0)) diff --git a/lib/robot.go b/lib/robot.go index 9d4047c..5fb1346 100644 --- a/lib/robot.go +++ b/lib/robot.go @@ -79,7 +79,7 @@ func (r *Robot) SendMsg(mainType, subType string, req proto.Message, rsp proto.M start := time.Now() defer func() { t := time.Since(start) - logrus.WithFields(logrus.Fields{"MainType": mainType, "SubType": subType, "rsp": rsp, "since": t.String()}).Debug("接收消息") + // logrus.WithFields(logrus.Fields{"MainType": mainType, "SubType": subType, "rsp": rsp, "since": t.String()}).Debug("接收消息") var name string if r.scene != nil { name = r.scene.Info().Name @@ -109,7 +109,7 @@ func (r *Robot) SendMsg(mainType, subType string, req proto.Message, rsp proto.M return pb.ErrorCode_SystemError } - logrus.WithFields(logrus.Fields{"MainType": mainType, "SubType": subType, "req": req}).Debug("发送消息") + // logrus.WithFields(logrus.Fields{"MainType": mainType, "SubType": subType, "req": req}).Debug("发送消息") for { _, data, err := r.conn.ReadMessage() if err != nil { @@ -133,7 +133,6 @@ func (r *Robot) SendMsg(mainType, subType string, req proto.Message, rsp proto.M if !ProtoUnmarshal(msg, rsp) { break } - logrus.Info(rsp.Code) return rsp.Code } } @@ -255,7 +254,7 @@ func (m *Robot) SendResult(result *CallResult) bool { } select { case m.resultCh <- result: - logrus.WithField("res", result).Debug("发送结果") + // logrus.WithField("res", result).Debug("发送结果") return true default: m.printIgnoredResult(result, "通道满了") diff --git a/lib/tickets.go b/lib/tickets.go index 1a7abba..aa2b007 100644 --- a/lib/tickets.go +++ b/lib/tickets.go @@ -3,12 +3,14 @@ package lib import ( "errors" "fmt" + "sync/atomic" + "time" ) // Tickets 表示Goroutine票池的接口。 type Tickets interface { // 拿走一张票。 - Take() + Take(useCount, inteval int32) // 归还一张票。 Return() // 票池是否已被激活。 @@ -24,6 +26,7 @@ type myTickets struct { total uint32 // 票的总数。 ticketCh chan struct{} // 票的容器。 active bool // 票池是否已被激活。 + count int32 } // NewTickets 会新建一个Goroutine票池。 @@ -55,7 +58,13 @@ func (gt *myTickets) init(total uint32) bool { return true } -func (gt *myTickets) Take() { +func (gt *myTickets) Take(useCount, inteval int32) { + atomic.AddInt32(>.count, 1) + count := atomic.LoadInt32(>.count) + if count >= int32(useCount) { + time.Sleep(time.Duration(inteval) * time.Second) + atomic.StoreInt32(>.count, 0) + } <-gt.ticketCh } diff --git a/main.go b/main.go index 6e93c80..6412ab3 100644 --- a/main.go +++ b/main.go @@ -66,7 +66,7 @@ func setupLogger() (err error) { // FullTimestamp: true, }) - logrus.SetLevel(logrus.InfoLevel) + logrus.SetLevel(logrus.DebugLevel) logrus.SetOutput(os.Stdout) file, err := os.OpenFile("robot.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)