优化进度

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
import (
"time"
"github.com/Pallinder/go-randomdata"
"legu.airobot/lib"
"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 {
robot.Store("user.login", rsp)
}
Sleep(time.Second*0, time.Second*1)
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)
go func() {
robot := NewRobot(m)
robot.SetScenes(m.iscenes)
robot.SetScenes()
m.AppendRobot(robot)
robot.Start()
atomic.AddUint32(&m.useCountTotal, 1)
m.Obs.Notify(EVENT_PROGRESS, int32(1))
}()
}
}()

View File

@ -27,21 +27,19 @@ type IRobot interface {
type Robot struct {
IStore
ai *myAI
scene IScene //场景
account string
sid string
conn *websocket.Conn
data map[string]interface{} //机器人缓存数据
status uint32 //状态
cache sync.Mutex
lock sync.Mutex //结果锁
sceneQueue *Queue[IScene] //场景队列
config *storage.Config //配置
resultCh chan *CallResult //请求结果通道
// sceneResultCh chan *CallResult //场景结果通道
ai *myAI
scene IScene //场景
account string //账号
sid string //区服编号
conn *websocket.Conn //连接对象
data map[string]interface{} //机器人缓存数据
status uint32 //状态
cache sync.Mutex //缓存锁
sceneQueue *Queue[IScene] //场景队列
config *storage.Config //配置
resultCh chan *CallResult //请求结果通道
ReportMap map[int]map[string]*Statistics //测试报告 key1:场景序号 key2:协议
elipseTotal time.Duration
elipseTotal time.Duration //场景总耗时
}
func NewRobot(ai *myAI) *Robot {
@ -51,8 +49,7 @@ func NewRobot(ai *myAI) *Robot {
sceneQueue: NewQueue[IScene](),
config: ai.config,
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)
return robot
@ -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
sort.SliceStable(scensConf, func(i, j int) bool {
return scensConf[i].Num < scensConf[j].Num
})
for _, conf := range scensConf {
for _, v := range scenes {
for _, v := range a.ai.iscenes {
info := v.Info()
if conf.Name == info.Name {
if conf.Loop == 0 || conf.Loop == 1 {
@ -214,19 +211,19 @@ func (m *Robot) Stop() bool {
}
func (m *Robot) syncCall() {
defer func() {
if p := recover(); p != nil {
err, ok := interface{}(p).(error)
var errMsg string
if ok {
errMsg = fmt.Sprintf("调用时Panic! (error: %s)", err)
} else {
errMsg = fmt.Sprintf("调用时Panic! (clue: %#v)", p)
}
logrus.Error(errMsg)
}
}()
for {
defer func() {
if p := recover(); p != nil {
err, ok := interface{}(p).(error)
var errMsg string
if ok {
errMsg = fmt.Sprintf("调用时Panic! (error: %s)", err)
} else {
errMsg = fmt.Sprintf("调用时Panic! (clue: %#v)", p)
}
logrus.Error(errMsg)
}
}()
scene, err := m.sceneQueue.Pop()
if err != nil {
logrus.WithField("err", err).Debug("所有场景执行结束")
@ -243,16 +240,15 @@ func (m *Robot) syncCall() {
continue
}
elapsedTime := time.Since(start)
m.elipseTotal += elapsedTime
logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计")
m.ai.Obs.Notify(EVENT_PROGRESS, int32(1))
}
}
func (m *Robot) prepareToStop() {
atomic.CompareAndSwapUint32(&m.status, STATUS_STARTED, STATUS_STOPPING)
logrus.Debug("关闭结果通道...")
close(m.resultCh)
atomic.StoreUint32(&m.status, STATUS_STOPPED)
}
@ -273,47 +269,31 @@ func (m *Robot) SendResult(result *CallResult) bool {
}
func (m *Robot) processResult() {
go func() {
for r := range m.resultCh {
head := fmt.Sprintf("%s.%s", r.MainType, r.SubType)
if routes, ok := m.ReportMap[r.Num]; ok {
if route, y := routes[head]; y {
max := route.MaxElapse
min := route.MinElapse
if r.Elapse > max {
max = r.Elapse
}
if r.Elapse < min {
min = r.Elapse
}
statis := &Statistics{
Route: head,
SceneName: r.SceneName,
ElapseTotal: route.ElapseTotal + r.Elapse,
MaxElapse: max,
MinElapse: min,
CallCount: route.CallCount + 1,
}
avg := (statis.MaxElapse + statis.MinElapse) / 2
statis.AvgElapse = avg
routes[head] = statis
} else {
statis := &Statistics{
Route: head,
SceneName: r.SceneName,
ElapseTotal: r.Elapse,
MaxElapse: r.Elapse,
MinElapse: r.Elapse,
CallCount: 1,
}
avg := (statis.MaxElapse + statis.MinElapse) / 2
statis.AvgElapse = avg
routes[head] = statis
for r := range m.resultCh {
head := fmt.Sprintf("%s.%s", r.MainType, r.SubType)
if routes, ok := m.ReportMap[r.Num]; ok {
if route, y := routes[head]; y {
max := route.MaxElapse
min := route.MinElapse
if r.Elapse > max {
max = r.Elapse
}
m.ReportMap[r.Num] = routes
if r.Elapse < min {
min = r.Elapse
}
statis := &Statistics{
Route: head,
SceneName: r.SceneName,
ElapseTotal: route.ElapseTotal + r.Elapse,
MaxElapse: max,
MinElapse: min,
CallCount: route.CallCount + 1,
}
avg := (statis.MaxElapse + statis.MinElapse) / 2
statis.AvgElapse = avg
routes[head] = statis
} else {
route := make(map[string]*Statistics)
statis := &Statistics{
Route: head,
SceneName: r.SceneName,
@ -324,12 +304,25 @@ func (m *Robot) processResult() {
}
avg := (statis.MaxElapse + statis.MinElapse) / 2
statis.AvgElapse = avg
route[head] = statis
m.ReportMap[r.Num] = route
routes[head] = statis
}
m.ReportMap[r.Num] = routes
} else {
route := make(map[string]*Statistics)
statis := &Statistics{
Route: head,
SceneName: r.SceneName,
ElapseTotal: r.Elapse,
MaxElapse: r.Elapse,
MinElapse: r.Elapse,
CallCount: 1,
}
avg := (statis.MaxElapse + statis.MinElapse) / 2
statis.AvgElapse = avg
route[head] = statis
m.ReportMap[r.Num] = route
}
}()
}
}
func (m *Robot) printIgnoredResult(result *CallResult, cause string) {

View File

@ -6,34 +6,10 @@ import (
"testing"
"github.com/Pallinder/go-randomdata"
"legu.airobot/busi/friend"
"legu.airobot/lib"
"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) {
q := lib.NewQueue[int]()

View File

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