优化进度

This commit is contained in:
wh_zcy 2022-12-15 12:04:05 +08:00
parent 69339fe16c
commit ff53232858
5 changed files with 83 additions and 114 deletions

View File

@ -1,8 +1,6 @@
package busi package busi
import ( import (
"time"
"github.com/Pallinder/go-randomdata" "github.com/Pallinder/go-randomdata"
"legu.airobot/lib" "legu.airobot/lib"
"legu.airobot/pb" "legu.airobot/pb"
@ -35,6 +33,5 @@ func (l *LoginScene) Run(robot lib.IRobot) error {
if code := robot.SendMsg("user", "login", req, rsp); code == pb.ErrorCode_Success { if code := robot.SendMsg("user", "login", req, rsp); code == pb.ErrorCode_Success {
robot.Store("user.login", rsp) robot.Store("user.login", rsp)
} }
Sleep(time.Second*0, time.Second*1)
return nil return nil
} }

View File

@ -83,11 +83,10 @@ func (m *myAI) Start() bool {
m.tickets.Take(int32(m.config.Global.UserCount), m.config.Global.IntervalS) m.tickets.Take(int32(m.config.Global.UserCount), m.config.Global.IntervalS)
go func() { go func() {
robot := NewRobot(m) robot := NewRobot(m)
robot.SetScenes(m.iscenes) robot.SetScenes()
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))
}() }()
} }
}() }()

View File

@ -29,19 +29,17 @@ type Robot struct {
IStore IStore
ai *myAI ai *myAI
scene IScene //场景 scene IScene //场景
account string account string //账号
sid string sid string //区服编号
conn *websocket.Conn conn *websocket.Conn //连接对象
data map[string]interface{} //机器人缓存数据 data map[string]interface{} //机器人缓存数据
status uint32 //状态 status uint32 //状态
cache sync.Mutex cache sync.Mutex //缓存锁
lock sync.Mutex //结果锁
sceneQueue *Queue[IScene] //场景队列 sceneQueue *Queue[IScene] //场景队列
config *storage.Config //配置 config *storage.Config //配置
resultCh chan *CallResult //请求结果通道 resultCh chan *CallResult //请求结果通道
// sceneResultCh chan *CallResult //场景结果通道
ReportMap map[int]map[string]*Statistics //测试报告 key1:场景序号 key2:协议 ReportMap map[int]map[string]*Statistics //测试报告 key1:场景序号 key2:协议
elipseTotal time.Duration elipseTotal time.Duration //场景总耗时
} }
func NewRobot(ai *myAI) *Robot { func NewRobot(ai *myAI) *Robot {
@ -51,7 +49,6 @@ func NewRobot(ai *myAI) *Robot {
sceneQueue: NewQueue[IScene](), sceneQueue: NewQueue[IScene](),
config: ai.config, config: ai.config,
resultCh: make(chan *CallResult, 1000), resultCh: make(chan *CallResult, 1000),
// sceneResultCh: make(chan *CallResult, 50),
ReportMap: make(map[int]map[string]*Statistics), ReportMap: make(map[int]map[string]*Statistics),
} }
robot.Store("sid", ai.config.Global.SId) robot.Store("sid", ai.config.Global.SId)
@ -140,13 +137,13 @@ func (r *Robot) SendMsg(mainType, subType string, req proto.Message, rsp proto.M
} }
// 设置场景队列 // 设置场景队列
func (a *Robot) SetScenes(scenes []IScene) { func (a *Robot) SetScenes() {
scensConf := a.config.Scenes scensConf := a.config.Scenes
sort.SliceStable(scensConf, func(i, j int) bool { sort.SliceStable(scensConf, func(i, j int) bool {
return scensConf[i].Num < scensConf[j].Num return scensConf[i].Num < scensConf[j].Num
}) })
for _, conf := range scensConf { for _, conf := range scensConf {
for _, v := range scenes { for _, v := range a.ai.iscenes {
info := v.Info() info := v.Info()
if conf.Name == info.Name { if conf.Name == info.Name {
if conf.Loop == 0 || conf.Loop == 1 { if conf.Loop == 0 || conf.Loop == 1 {
@ -214,6 +211,7 @@ func (m *Robot) Stop() bool {
} }
func (m *Robot) syncCall() { func (m *Robot) syncCall() {
for {
defer func() { defer func() {
if p := recover(); p != nil { if p := recover(); p != nil {
err, ok := interface{}(p).(error) err, ok := interface{}(p).(error)
@ -226,7 +224,6 @@ func (m *Robot) syncCall() {
logrus.Error(errMsg) logrus.Error(errMsg)
} }
}() }()
for {
scene, err := m.sceneQueue.Pop() scene, err := m.sceneQueue.Pop()
if err != nil { if err != nil {
logrus.WithField("err", err).Debug("所有场景执行结束") logrus.WithField("err", err).Debug("所有场景执行结束")
@ -243,16 +240,15 @@ func (m *Robot) syncCall() {
continue continue
} }
elapsedTime := time.Since(start) elapsedTime := time.Since(start)
m.elipseTotal += elapsedTime m.elipseTotal += elapsedTime
logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计") logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计")
m.ai.Obs.Notify(EVENT_PROGRESS, int32(1))
} }
} }
func (m *Robot) prepareToStop() { func (m *Robot) prepareToStop() {
atomic.CompareAndSwapUint32(&m.status, STATUS_STARTED, STATUS_STOPPING) atomic.CompareAndSwapUint32(&m.status, STATUS_STARTED, STATUS_STOPPING)
logrus.Debug("关闭结果通道...")
close(m.resultCh) close(m.resultCh)
atomic.StoreUint32(&m.status, STATUS_STOPPED) atomic.StoreUint32(&m.status, STATUS_STOPPED)
} }
@ -273,7 +269,6 @@ func (m *Robot) SendResult(result *CallResult) bool {
} }
func (m *Robot) processResult() { func (m *Robot) processResult() {
go func() {
for r := range m.resultCh { for r := range m.resultCh {
head := fmt.Sprintf("%s.%s", r.MainType, r.SubType) head := fmt.Sprintf("%s.%s", r.MainType, r.SubType)
if routes, ok := m.ReportMap[r.Num]; ok { if routes, ok := m.ReportMap[r.Num]; ok {
@ -328,8 +323,6 @@ func (m *Robot) processResult() {
m.ReportMap[r.Num] = route m.ReportMap[r.Num] = route
} }
} }
}()
} }
func (m *Robot) printIgnoredResult(result *CallResult, cause string) { func (m *Robot) printIgnoredResult(result *CallResult, cause string) {

View File

@ -6,34 +6,10 @@ import (
"testing" "testing"
"github.com/Pallinder/go-randomdata" "github.com/Pallinder/go-randomdata"
"legu.airobot/busi/friend"
"legu.airobot/lib" "legu.airobot/lib"
"legu.airobot/storage" "legu.airobot/storage"
) )
func TestAction(t *testing.T) {
aip := lib.AIParam{}
ai, _ := lib.NewAI(aip)
// 创建场景
scene := lib.NewScene(ai, lib.SceneParam{
Name: "场景1",
Desc: "好友",
})
//为场景选择caller
scene.AddCaller(&friend.FriendRecommend{})
//加机器人
// ai.AddRobot(scene)
//运行机器人
// for _, v := range ai.GetRobots("场景1") {
// v.Start()
// }
}
func TestA(t *testing.T) { func TestA(t *testing.T) {
q := lib.NewQueue[int]() q := lib.NewQueue[int]()

View File

@ -33,7 +33,7 @@ type MainWindow struct {
statusbar *statusBar //状态栏 statusbar *statusBar //状态栏
mainMenu *mainMenu //菜单 mainMenu *mainMenu //菜单
progress *widget.ProgressBar //进度条 progress *widget.ProgressBar //进度条
progressCount int //进度条计数 progressCounter int //进度条计数
tipLabel *widget.Label //提示Label tipLabel *widget.Label //提示Label
startBtn *widget.Button //开始按钮 startBtn *widget.Button //开始按钮
} }
@ -135,12 +135,16 @@ func (mw *MainWindow) showProgress(data interface{}, args ...interface{}) {
count := data.(int32) count := data.(int32)
if count == 0 { //表示结束 if count == 0 { //表示结束
mw.startBtn.Enable() mw.startBtn.Enable()
mw.progressCount = 0 mw.progressCounter = 0
mw.tipLabel.SetText("已生成报表") mw.tipLabel.SetText("已生成报表")
} }
max := int(mw.config.Global.UserCountTotal) * len(mw.config.Scenes)
//计算进度 //计算进度
mw.progressCount += int(count) mw.progressCounter += int(count)
expr := fmt.Sprintf("%v/%v", mw.progressCount, mw.config.Global.UserCountTotal) expr := fmt.Sprintf("%v/%v", mw.progressCounter, max)
logrus.Debug(expr)
r, err := engine.ParseAndExec(expr) r, err := engine.ParseAndExec(expr)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)