diff --git a/busi/pay.go b/busi/pay.go index b64f098..62820cc 100644 --- a/busi/pay.go +++ b/busi/pay.go @@ -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) diff --git a/lib/ai.go b/lib/ai.go index 03ae90f..adbd95b 100644 --- a/lib/ai.go +++ b/lib/ai.go @@ -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() diff --git a/lib/event.go b/lib/event.go new file mode 100644 index 0000000..b34cd19 --- /dev/null +++ b/lib/event.go @@ -0,0 +1,6 @@ +package lib + +const ( + EVENT_FLAG = "dispatch" + EVENT_PROGRESS = "progress" +) diff --git a/lib/observer.go b/lib/observer.go new file mode 100644 index 0000000..a8f1a79 --- /dev/null +++ b/lib/observer.go @@ -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...) + } + } +} diff --git a/main.go b/main.go index 6412ab3..6e93c80 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/ui/mainwindow.go b/ui/mainwindow.go index f816312..71b6500 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -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) }