结果排序

This commit is contained in:
wh_zcy 2022-12-13 17:16:04 +08:00
parent ba3c8963ce
commit 4d1ca3bf95
4 changed files with 58 additions and 43 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"sync/atomic"
@ -23,19 +24,19 @@ type myAI struct {
useCountTotal uint32 //总数
lock sync.Mutex //合并数据锁
config *storage.Config //配置
ReportMap map[string]map[string]*Statistics //测试报告 key1:场景 key2:协议
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)
}
@ -178,17 +175,22 @@ func (m *myAI) genReport() {
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("已生成测试报告")
}

View File

@ -30,7 +30,8 @@ type CallResult struct {
SceneName string
MainType string
SubType string
Elapse time.Duration // 耗时。
Elapse time.Duration // 耗时
Num int
}
type Statistics struct {

View File

@ -43,7 +43,7 @@ type Robot struct {
config *storage.Config //配置
resultCh chan *CallResult //请求结果通道
sceneResultCh chan *CallResult //场景结果通道
ReportMap map[string]map[string]*Statistics //测试报告 key1:场景 key2:协议
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
}
}

View File

@ -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,