修复时间间隔
This commit is contained in:
parent
d72a3a4700
commit
8e8f145bbc
@ -29,7 +29,6 @@ func (f *PayScene) Run(ai lib.IRobot) (err error) {
|
|||||||
if code = ai.SendMsg("pay", "dailybuy", &pb.PayDailyBuyReq{
|
if code = ai.SendMsg("pay", "dailybuy", &pb.PayDailyBuyReq{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
}, &pb.PayDailyBuyResp{}); code != pb.ErrorCode_Success {
|
}, &pb.PayDailyBuyResp{}); code != pb.ErrorCode_Success {
|
||||||
ai.Stop()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
24
lib/ai.go
24
lib/ai.go
@ -19,7 +19,8 @@ type myAI struct {
|
|||||||
robots []*Robot
|
robots []*Robot
|
||||||
scenes []*scene
|
scenes []*scene
|
||||||
iscenes []IScene
|
iscenes []IScene
|
||||||
tickets Tickets //票池
|
tickets Tickets //票池
|
||||||
|
Obs Observer
|
||||||
useCount uint32 //计数(压入的用户数)
|
useCount uint32 //计数(压入的用户数)
|
||||||
useCountTotal uint32 //总数
|
useCountTotal uint32 //总数
|
||||||
lock sync.Mutex //合并数据锁
|
lock sync.Mutex //合并数据锁
|
||||||
@ -33,6 +34,7 @@ func NewAI(aip AIParam) (*myAI, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ai := &myAI{
|
ai := &myAI{
|
||||||
|
Obs: NewObserver(),
|
||||||
scenes: make([]*scene, 0),
|
scenes: make([]*scene, 0),
|
||||||
config: aip.Config,
|
config: aip.Config,
|
||||||
iscenes: aip.Scenes,
|
iscenes: aip.Scenes,
|
||||||
@ -78,18 +80,19 @@ func (m *myAI) Start() bool {
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
m.tickets.Take()
|
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)
|
atomic.StoreUint32(&m.useCount, 0)
|
||||||
time.Sleep(time.Duration(m.config.Global.IntervalS) * time.Second)
|
time.Sleep(time.Duration(m.config.Global.IntervalS) * time.Second)
|
||||||
}
|
}
|
||||||
go func() {
|
atomic.AddUint32(&m.useCount, 1)
|
||||||
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)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -98,6 +101,7 @@ func (m *myAI) Start() bool {
|
|||||||
for {
|
for {
|
||||||
total := atomic.LoadUint32(&m.useCountTotal)
|
total := atomic.LoadUint32(&m.useCountTotal)
|
||||||
if total == m.config.Global.UserCountTotal {
|
if total == m.config.Global.UserCountTotal {
|
||||||
|
m.Obs.Notify(EVENT_PROGRESS, uint32(0))
|
||||||
elipse := time.Since(start)
|
elipse := time.Since(start)
|
||||||
logrus.Info("开始生成测试报告")
|
logrus.Info("开始生成测试报告")
|
||||||
m.MergeResult()
|
m.MergeResult()
|
||||||
|
6
lib/event.go
Normal file
6
lib/event.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package lib
|
||||||
|
|
||||||
|
const (
|
||||||
|
EVENT_FLAG = "dispatch"
|
||||||
|
EVENT_PROGRESS = "progress"
|
||||||
|
)
|
56
lib/observer.go
Normal file
56
lib/observer.go
Normal 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...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2
main.go
2
main.go
@ -66,7 +66,7 @@ func setupLogger() (err error) {
|
|||||||
// FullTimestamp: true,
|
// FullTimestamp: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
logrus.SetLevel(logrus.DebugLevel)
|
logrus.SetLevel(logrus.InfoLevel)
|
||||||
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)
|
||||||
|
@ -99,6 +99,11 @@ func (mw *MainWindow) startContainer() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fprogress := widget.NewProgressBar()
|
||||||
|
fprogress.TextFormatter = func() string {
|
||||||
|
return fmt.Sprintf("%.2f / %.2f", fprogress.Value, fprogress.Max)
|
||||||
|
}
|
||||||
|
|
||||||
startBtn.Enable()
|
startBtn.Enable()
|
||||||
startBtn.OnTapped = func() {
|
startBtn.OnTapped = func() {
|
||||||
param := lib.AIParam{
|
param := lib.AIParam{
|
||||||
@ -110,9 +115,20 @@ func (mw *MainWindow) startContainer() {
|
|||||||
dialog.ShowError(err, mw.w)
|
dialog.ShowError(err, mw.w)
|
||||||
return
|
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)
|
content := container.NewCenter(startBtn)
|
||||||
mw.changeContent(content)
|
mw.changeContent(content)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user