修复进度条
This commit is contained in:
parent
6976f85512
commit
702f09881b
15
lib/ai.go
15
lib/ai.go
@ -21,7 +21,7 @@ type myAI struct {
|
|||||||
iscenes []IScene
|
iscenes []IScene
|
||||||
tickets Tickets //票池
|
tickets Tickets //票池
|
||||||
Obs Observer
|
Obs Observer
|
||||||
useCount uint32 //计数(压入的用户数)
|
// useCount uint32 //计数(压入的用户数)
|
||||||
useCountTotal uint32 //总数
|
useCountTotal uint32 //总数
|
||||||
lock sync.Mutex //合并数据锁
|
lock sync.Mutex //合并数据锁
|
||||||
config *storage.Config //配置
|
config *storage.Config //配置
|
||||||
@ -79,20 +79,15 @@ func (m *myAI) Start() bool {
|
|||||||
logrus.Info("测试中...")
|
logrus.Info("测试中...")
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
m.tickets.Take()
|
m.tickets.Take(int32(m.config.Global.UserCount), m.config.Global.IntervalS)
|
||||||
uCount := atomic.LoadUint32(&m.useCount)
|
go func() {
|
||||||
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 := NewRobot(m.config)
|
||||||
robot.SetScenes(m.iscenes)
|
robot.SetScenes(m.iscenes)
|
||||||
m.AppendRobot(robot)
|
m.AppendRobot(robot)
|
||||||
robot.Start()
|
robot.Start()
|
||||||
atomic.AddUint32(&m.useCountTotal, 1)
|
atomic.AddUint32(&m.useCountTotal, 1)
|
||||||
m.Obs.Notify(EVENT_PROGRESS, int32(1))
|
m.Obs.Notify(EVENT_PROGRESS, int32(1))
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -102,7 +97,7 @@ func (m *myAI) Start() bool {
|
|||||||
total := atomic.LoadUint32(&m.useCountTotal)
|
total := atomic.LoadUint32(&m.useCountTotal)
|
||||||
if total == m.config.Global.UserCountTotal {
|
if total == m.config.Global.UserCountTotal {
|
||||||
elipse := time.Since(start)
|
elipse := time.Since(start)
|
||||||
logrus.Info("开始生成测试报告")
|
logrus.Debug("开始生成测试报告")
|
||||||
m.MergeResult()
|
m.MergeResult()
|
||||||
m.genReport(elipse)
|
m.genReport(elipse)
|
||||||
m.Obs.Notify(EVENT_PROGRESS, int32(0))
|
m.Obs.Notify(EVENT_PROGRESS, int32(0))
|
||||||
|
@ -79,7 +79,7 @@ func (r *Robot) SendMsg(mainType, subType string, req proto.Message, rsp proto.M
|
|||||||
start := time.Now()
|
start := time.Now()
|
||||||
defer func() {
|
defer func() {
|
||||||
t := time.Since(start)
|
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
|
var name string
|
||||||
if r.scene != nil {
|
if r.scene != nil {
|
||||||
name = r.scene.Info().Name
|
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
|
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 {
|
for {
|
||||||
_, data, err := r.conn.ReadMessage()
|
_, data, err := r.conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -133,7 +133,6 @@ func (r *Robot) SendMsg(mainType, subType string, req proto.Message, rsp proto.M
|
|||||||
if !ProtoUnmarshal(msg, rsp) {
|
if !ProtoUnmarshal(msg, rsp) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
logrus.Info(rsp.Code)
|
|
||||||
return rsp.Code
|
return rsp.Code
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -255,7 +254,7 @@ func (m *Robot) SendResult(result *CallResult) bool {
|
|||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case m.resultCh <- result:
|
case m.resultCh <- result:
|
||||||
logrus.WithField("res", result).Debug("发送结果")
|
// logrus.WithField("res", result).Debug("发送结果")
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
m.printIgnoredResult(result, "通道满了")
|
m.printIgnoredResult(result, "通道满了")
|
||||||
|
@ -3,12 +3,14 @@ package lib
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Tickets 表示Goroutine票池的接口。
|
// Tickets 表示Goroutine票池的接口。
|
||||||
type Tickets interface {
|
type Tickets interface {
|
||||||
// 拿走一张票。
|
// 拿走一张票。
|
||||||
Take()
|
Take(useCount, inteval int32)
|
||||||
// 归还一张票。
|
// 归还一张票。
|
||||||
Return()
|
Return()
|
||||||
// 票池是否已被激活。
|
// 票池是否已被激活。
|
||||||
@ -24,6 +26,7 @@ type myTickets struct {
|
|||||||
total uint32 // 票的总数。
|
total uint32 // 票的总数。
|
||||||
ticketCh chan struct{} // 票的容器。
|
ticketCh chan struct{} // 票的容器。
|
||||||
active bool // 票池是否已被激活。
|
active bool // 票池是否已被激活。
|
||||||
|
count int32
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTickets 会新建一个Goroutine票池。
|
// NewTickets 会新建一个Goroutine票池。
|
||||||
@ -55,7 +58,13 @@ func (gt *myTickets) init(total uint32) bool {
|
|||||||
return true
|
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
|
<-gt.ticketCh
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
main.go
2
main.go
@ -66,7 +66,7 @@ func setupLogger() (err error) {
|
|||||||
// FullTimestamp: true,
|
// FullTimestamp: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
logrus.SetLevel(logrus.InfoLevel)
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
logrus.SetOutput(os.Stdout)
|
logrus.SetOutput(os.Stdout)
|
||||||
|
|
||||||
file, err := os.OpenFile("robot.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
file, err := os.OpenFile("robot.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
||||||
|
Loading…
Reference in New Issue
Block a user