From 4d1ca3bf957b14fee4ea0007868a8aa4536a5077 Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Tue, 13 Dec 2022 17:16:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=9C=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ai.go | 45 ++++++++++++++++++++++++--------------------- lib/base.go | 3 ++- lib/robot.go | 39 +++++++++++++++++++++++++-------------- test/robot_test.go | 14 +++++++------- 4 files changed, 58 insertions(+), 43 deletions(-) diff --git a/lib/ai.go b/lib/ai.go index 0b9e256..ad04c8d 100644 --- a/lib/ai.go +++ b/lib/ai.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "sort" "strings" "sync" "sync/atomic" @@ -18,24 +19,24 @@ type myAI struct { robots []*Robot scenes []*scene iscenes []IScene - tickets Tickets //票池 - useCount uint32 //计数(压入的用户数) - useCountTotal uint32 //总数 - lock sync.Mutex //合并数据锁 - config *storage.Config //配置 - ReportMap map[string]map[string]*Statistics //测试报告 key1:场景 key2:协议 + tickets Tickets //票池 + useCount uint32 //计数(压入的用户数) + useCountTotal uint32 //总数 + lock sync.Mutex //合并数据锁 + config *storage.Config //配置 + ReportMap map[int]map[string]*Statistics //测试报告 key1:场景 key2:协议 } func NewAI(aip AIParam) (*myAI, error) { - // if err := aip.Check(); err != nil { - // return nil, err - // } + if err := aip.Check(); err != nil { + return nil, err + } ai := &myAI{ scenes: make([]*scene, 0), config: aip.Config, iscenes: aip.Scenes, - ReportMap: make(map[string]map[string]*Statistics), + ReportMap: make(map[int]map[string]*Statistics), } if err := ai.init(); err != nil { @@ -88,7 +89,6 @@ func (m *myAI) Start() bool { m.AppendRobot(robot) robot.Start() atomic.AddUint32(&m.useCountTotal, 1) - // m.genReport(robot) }() } }() @@ -97,10 +97,7 @@ func (m *myAI) Start() bool { for { total := atomic.LoadUint32(&m.useCountTotal) if total == m.config.Global.UserCountTotal { - logrus.Debug("开始合并报告") m.MergeResult() - logrus.Debug("结束合并报告") - //打印报告 m.genReport() break } @@ -117,7 +114,7 @@ func (m *myAI) Stop() { func (m *myAI) MergeResult() { m.lock.Lock() defer m.lock.Unlock() - n := make(map[string]map[string]*Statistics) + n := make(map[int]map[string]*Statistics) for _, r := range m.robots { if len(m.ReportMap) == 0 { m.ReportMap = r.ReportMap @@ -140,7 +137,7 @@ func (m *myAI) MergeResult() { statis.MaxElapse = (a.MaxElapse + b.MaxElapse) / 2 statis.MinElapse = (a.MinElapse + b.MinElapse) / 2 statis.Route = a.Route - statis.SceneName = i + statis.SceneName = a.SceneName if n[i][k] == nil { n[i] = make(map[string]*Statistics) } @@ -175,20 +172,25 @@ func (m *myAI) MergeResult() { func (m *myAI) genReport() { var buf bytes.Buffer buf.WriteString("测试报告\n") - buf.WriteString(fmt.Sprintf("用户总数:%d \n", + buf.WriteString(fmt.Sprintf("用户总数:%d\n", m.config.Global.UserCountTotal)) var msgs []string - for k, routes := range m.ReportMap { - for r, d := range routes { + var i []int + for key, _ := range m.ReportMap { + i = append(i, key) + } + sort.Ints(i) + for _, routes := range i { + for r, d := range m.ReportMap[routes] { msgs = append(msgs, fmt.Sprintf("【%s】协议:%s 调用次数:%d 总耗时:%v 平均耗时:%v 最大耗时:%v 最小耗时:%v", - k, r, d.CallCount, d.ElapseTotal.String(), d.AvgElapse.String(), d.MaxElapse.String(), d.MinElapse.String())) + d.SceneName, r, d.CallCount, d.ElapseTotal.String(), d.AvgElapse.String(), d.MaxElapse.String(), d.MinElapse.String())) } } record := strings.Join(msgs, "\n") buf.WriteString(record) buf.WriteString("\n------------------------------------------------------------------------------------------------------\n") // logrus.WithField("res", buf.String()).Debug("报告内容") - file, err := os.OpenFile(filepath.Join("./", "report.log"), os.O_APPEND|os.O_CREATE, os.ModePerm) + file, err := os.OpenFile(filepath.Join("./", "report.log"), os.O_TRUNC|os.O_CREATE, os.ModePerm) if err != nil { logrus.Error(err) } @@ -197,4 +199,5 @@ func (m *myAI) genReport() { if _, err := file.WriteString(buf.String()); err != nil { logrus.Error(err) } + logrus.Debug("已生成测试报告") } diff --git a/lib/base.go b/lib/base.go index 7a13e0f..b369dad 100644 --- a/lib/base.go +++ b/lib/base.go @@ -30,7 +30,8 @@ type CallResult struct { SceneName string MainType string SubType string - Elapse time.Duration // 耗时。 + Elapse time.Duration // 耗时 + Num int } type Statistics struct { diff --git a/lib/robot.go b/lib/robot.go index 53b3f49..d015772 100644 --- a/lib/robot.go +++ b/lib/robot.go @@ -36,14 +36,14 @@ type Robot struct { account string sid string conn *websocket.Conn - data map[string]interface{} //机器人缓存数据 - status uint32 //状态 - lock sync.Mutex //数据缓存锁 - sceneQueue *Queue[IScene] //场景队列 - config *storage.Config //配置 - resultCh chan *CallResult //请求结果通道 - sceneResultCh chan *CallResult //场景结果通道 - ReportMap map[string]map[string]*Statistics //测试报告 key1:场景 key2:协议 + data map[string]interface{} //机器人缓存数据 + status uint32 //状态 + lock sync.Mutex //数据缓存锁 + sceneQueue *Queue[IScene] //场景队列 + config *storage.Config //配置 + resultCh chan *CallResult //请求结果通道 + sceneResultCh chan *CallResult //场景结果通道 + ReportMap map[int]map[string]*Statistics //测试报告 key1:场景 key2:协议 elipseTotal time.Duration } @@ -54,7 +54,7 @@ func NewRobot(config *storage.Config) *Robot { config: config, resultCh: make(chan *CallResult, 100), sceneResultCh: make(chan *CallResult, 50), - ReportMap: make(map[string]map[string]*Statistics), + ReportMap: make(map[int]map[string]*Statistics), } robot.Store("sid", config.Global.SId) return robot @@ -90,6 +90,7 @@ func (r *Robot) SendMsg(mainType, subType string, req proto.Message, rsp proto.M MainType: mainType, SubType: subType, Elapse: t, + Num: r.getSortNum(name), }) }() @@ -152,6 +153,17 @@ func (a *Robot) SetScenes(scenes []IScene) { } } +// 根据场景名称获取顺序号 +func (a *Robot) getSortNum(name string) int { + var num int + for _, v := range a.config.Scenes { + if v.Name == name { + return v.Num + } + } + return num +} + func (m *Robot) Start() bool { if len(m.sceneQueue.List()) == 0 { logrus.Warn("没有设置场景队列") @@ -191,12 +203,11 @@ func (m *Robot) Stop() bool { } func (m *Robot) syncCall() { - // var sceneElipseTotal time.Duration // 场景总耗时 for { scene, err := m.sceneQueue.Pop() if err != nil { logrus.WithField("err", err).Warn("所有场景执行结束") - // m.prepareToStop() + m.prepareToStop() // m.genReport(sceneElipseTotal) return } @@ -246,7 +257,7 @@ func (m *Robot) processResult() { m.lock.Lock() for r := range m.resultCh { head := fmt.Sprintf("%s.%s", r.MainType, r.SubType) - if routes, ok := m.ReportMap[r.SceneName]; ok { + if routes, ok := m.ReportMap[r.Num]; ok { if route, y := routes[head]; y { max := route.MaxElapse min := route.MinElapse @@ -281,7 +292,7 @@ func (m *Robot) processResult() { statis.AvgElapse = avg routes[head] = statis } - m.ReportMap[r.SceneName] = routes + m.ReportMap[r.Num] = routes } else { route := make(map[string]*Statistics) statis := &Statistics{ @@ -295,7 +306,7 @@ func (m *Robot) processResult() { avg := (statis.MaxElapse + statis.MinElapse) / 2 statis.AvgElapse = avg route[head] = statis - m.ReportMap[r.SceneName] = route + m.ReportMap[r.Num] = route } } diff --git a/test/robot_test.go b/test/robot_test.go index 6623215..4d64e8f 100644 --- a/test/robot_test.go +++ b/test/robot_test.go @@ -51,14 +51,14 @@ func TestA(t *testing.T) { func TestMerge(t *testing.T) { config := &storage.Config{ - Global: &storage.Global{UserCountTotal: 2, SId: "dfz"}, + Global: &storage.Global{UserCountTotal: 2, SId: "dfz", UserCount: 1}, } ma, _ := lib.NewAI(lib.AIParam{Config: config}) robot1 := lib.NewRobot(config) - robot1.ReportMap = make(map[string]map[string]*lib.Statistics) - robot1.ReportMap["登录"] = make(map[string]*lib.Statistics) - robot1.ReportMap["登录"]["user.login"] = &lib.Statistics{ + robot1.ReportMap = make(map[int]map[string]*lib.Statistics) + robot1.ReportMap[1] = make(map[string]*lib.Statistics) + robot1.ReportMap[1]["user.login"] = &lib.Statistics{ ElapseTotal: 5, CallCount: 1, AvgElapse: 1, @@ -70,9 +70,9 @@ func TestMerge(t *testing.T) { ma.AppendRobot(robot1) /////////////// robot2 := lib.NewRobot(config) - robot2.ReportMap = make(map[string]map[string]*lib.Statistics) - robot2.ReportMap["登录"] = make(map[string]*lib.Statistics) - robot2.ReportMap["登录"]["user.login"] = &lib.Statistics{ + robot2.ReportMap = make(map[int]map[string]*lib.Statistics) + robot2.ReportMap[1] = make(map[string]*lib.Statistics) + robot2.ReportMap[1]["user.login"] = &lib.Statistics{ ElapseTotal: 7, CallCount: 1, AvgElapse: 1,