修复时间间隔

This commit is contained in:
wh_zcy 2022-12-14 11:03:43 +08:00
parent d72a3a4700
commit 8e8f145bbc
6 changed files with 94 additions and 13 deletions

View File

@ -29,7 +29,6 @@ func (f *PayScene) Run(ai lib.IRobot) (err error) {
if code = ai.SendMsg("pay", "dailybuy", &pb.PayDailyBuyReq{
Id: 1,
}, &pb.PayDailyBuyResp{}); code != pb.ErrorCode_Success {
ai.Stop()
return
}
time.Sleep(time.Second)

View File

@ -19,7 +19,8 @@ type myAI struct {
robots []*Robot
scenes []*scene
iscenes []IScene
tickets Tickets //票池
tickets Tickets //票池
Obs Observer
useCount uint32 //计数(压入的用户数)
useCountTotal uint32 //总数
lock sync.Mutex //合并数据锁
@ -33,6 +34,7 @@ func NewAI(aip AIParam) (*myAI, error) {
}
ai := &myAI{
Obs: NewObserver(),
scenes: make([]*scene, 0),
config: aip.Config,
iscenes: aip.Scenes,
@ -78,18 +80,19 @@ func (m *myAI) Start() bool {
go func() {
for {
m.tickets.Take()
if m.useCount >= uint32(m.config.Global.UserCount) {
if atomic.LoadUint32(&m.useCount) >= uint32(m.config.Global.UserCount) {
m.Obs.Notify(EVENT_PROGRESS, atomic.LoadUint32(&m.useCount))
atomic.StoreUint32(&m.useCount, 0)
time.Sleep(time.Duration(m.config.Global.IntervalS) * time.Second)
}
go func() {
atomic.AddUint32(&m.useCount, 1)
robot := NewRobot(m.config)
robot.SetScenes(m.iscenes)
m.AppendRobot(robot)
robot.Start()
atomic.AddUint32(&m.useCountTotal, 1)
}()
atomic.AddUint32(&m.useCount, 1)
//加入机器人
robot := NewRobot(m.config)
robot.SetScenes(m.iscenes)
m.AppendRobot(robot)
robot.Start()
atomic.AddUint32(&m.useCountTotal, 1)
}
}()
@ -98,6 +101,7 @@ func (m *myAI) Start() bool {
for {
total := atomic.LoadUint32(&m.useCountTotal)
if total == m.config.Global.UserCountTotal {
m.Obs.Notify(EVENT_PROGRESS, uint32(0))
elipse := time.Since(start)
logrus.Info("开始生成测试报告")
m.MergeResult()

6
lib/event.go Normal file
View File

@ -0,0 +1,6 @@
package lib
const (
EVENT_FLAG = "dispatch"
EVENT_PROGRESS = "progress"
)

56
lib/observer.go Normal file
View File

@ -0,0 +1,56 @@
package lib
import "sync"
type (
Event string
OnNotify func(data interface{}, args ...interface{})
Listener struct {
OnNotify OnNotify
}
Observer interface {
AddListener(event Event, listener Listener)
Remove(event Event)
Notify(event Event, data interface{}, args ...interface{})
}
ObserverImpl struct {
listeners map[Event][]Listener
}
)
var (
obs *ObserverImpl
singletonObserver sync.Once
)
func NewObserver() *ObserverImpl {
singletonObserver.Do(func() {
obs = &ObserverImpl{}
})
return obs
}
func (o *ObserverImpl) AddListener(event Event, listener Listener) {
if o.listeners == nil {
o.listeners = map[Event][]Listener{}
}
o.listeners[event] = append(o.listeners[event], listener)
}
func (o *ObserverImpl) Remove(event Event) {
delete(o.listeners, event)
}
func (o *ObserverImpl) Notify(event Event, data interface{}, args ...interface{}) {
if listeners, ok := o.listeners[event]; !ok {
return
} else {
for _, listener := range listeners {
go listener.OnNotify(data, args...)
}
}
}

View File

@ -66,7 +66,7 @@ func setupLogger() (err error) {
// FullTimestamp: true,
})
logrus.SetLevel(logrus.DebugLevel)
logrus.SetLevel(logrus.InfoLevel)
logrus.SetOutput(os.Stdout)
file, err := os.OpenFile("robot.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)

View File

@ -99,6 +99,11 @@ func (mw *MainWindow) startContainer() {
return
}
fprogress := widget.NewProgressBar()
fprogress.TextFormatter = func() string {
return fmt.Sprintf("%.2f / %.2f", fprogress.Value, fprogress.Max)
}
startBtn.Enable()
startBtn.OnTapped = func() {
param := lib.AIParam{
@ -110,9 +115,20 @@ func (mw *MainWindow) startContainer() {
dialog.ShowError(err, mw.w)
return
}
ai.Start()
startBtn.Disable()
ai.Obs.AddListener(lib.EVENT_PROGRESS, lib.Listener{
OnNotify: func(data interface{}, args ...interface{}) {
count := data.(uint32)
if count == 0 { //表示结束
startBtn.Enable()
}
//进度
},
})
ai.Start()
}
content := container.NewCenter(startBtn)
mw.changeContent(content)
}