结果排序
This commit is contained in:
parent
ba3c8963ce
commit
4d1ca3bf95
33
lib/ai.go
33
lib/ai.go
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@ -23,19 +24,19 @@ type myAI struct {
|
|||||||
useCountTotal uint32 //总数
|
useCountTotal uint32 //总数
|
||||||
lock sync.Mutex //合并数据锁
|
lock sync.Mutex //合并数据锁
|
||||||
config *storage.Config //配置
|
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) {
|
func NewAI(aip AIParam) (*myAI, error) {
|
||||||
// if err := aip.Check(); err != nil {
|
if err := aip.Check(); err != nil {
|
||||||
// return nil, err
|
return nil, err
|
||||||
// }
|
}
|
||||||
|
|
||||||
ai := &myAI{
|
ai := &myAI{
|
||||||
scenes: make([]*scene, 0),
|
scenes: make([]*scene, 0),
|
||||||
config: aip.Config,
|
config: aip.Config,
|
||||||
iscenes: aip.Scenes,
|
iscenes: aip.Scenes,
|
||||||
ReportMap: make(map[string]map[string]*Statistics),
|
ReportMap: make(map[int]map[string]*Statistics),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ai.init(); err != nil {
|
if err := ai.init(); err != nil {
|
||||||
@ -88,7 +89,6 @@ func (m *myAI) Start() bool {
|
|||||||
m.AppendRobot(robot)
|
m.AppendRobot(robot)
|
||||||
robot.Start()
|
robot.Start()
|
||||||
atomic.AddUint32(&m.useCountTotal, 1)
|
atomic.AddUint32(&m.useCountTotal, 1)
|
||||||
// m.genReport(robot)
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -97,10 +97,7 @@ func (m *myAI) Start() bool {
|
|||||||
for {
|
for {
|
||||||
total := atomic.LoadUint32(&m.useCountTotal)
|
total := atomic.LoadUint32(&m.useCountTotal)
|
||||||
if total == m.config.Global.UserCountTotal {
|
if total == m.config.Global.UserCountTotal {
|
||||||
logrus.Debug("开始合并报告")
|
|
||||||
m.MergeResult()
|
m.MergeResult()
|
||||||
logrus.Debug("结束合并报告")
|
|
||||||
//打印报告
|
|
||||||
m.genReport()
|
m.genReport()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -117,7 +114,7 @@ func (m *myAI) Stop() {
|
|||||||
func (m *myAI) MergeResult() {
|
func (m *myAI) MergeResult() {
|
||||||
m.lock.Lock()
|
m.lock.Lock()
|
||||||
defer m.lock.Unlock()
|
defer m.lock.Unlock()
|
||||||
n := make(map[string]map[string]*Statistics)
|
n := make(map[int]map[string]*Statistics)
|
||||||
for _, r := range m.robots {
|
for _, r := range m.robots {
|
||||||
if len(m.ReportMap) == 0 {
|
if len(m.ReportMap) == 0 {
|
||||||
m.ReportMap = r.ReportMap
|
m.ReportMap = r.ReportMap
|
||||||
@ -140,7 +137,7 @@ func (m *myAI) MergeResult() {
|
|||||||
statis.MaxElapse = (a.MaxElapse + b.MaxElapse) / 2
|
statis.MaxElapse = (a.MaxElapse + b.MaxElapse) / 2
|
||||||
statis.MinElapse = (a.MinElapse + b.MinElapse) / 2
|
statis.MinElapse = (a.MinElapse + b.MinElapse) / 2
|
||||||
statis.Route = a.Route
|
statis.Route = a.Route
|
||||||
statis.SceneName = i
|
statis.SceneName = a.SceneName
|
||||||
if n[i][k] == nil {
|
if n[i][k] == nil {
|
||||||
n[i] = make(map[string]*Statistics)
|
n[i] = make(map[string]*Statistics)
|
||||||
}
|
}
|
||||||
@ -178,17 +175,22 @@ func (m *myAI) genReport() {
|
|||||||
buf.WriteString(fmt.Sprintf("用户总数:%d\n",
|
buf.WriteString(fmt.Sprintf("用户总数:%d\n",
|
||||||
m.config.Global.UserCountTotal))
|
m.config.Global.UserCountTotal))
|
||||||
var msgs []string
|
var msgs []string
|
||||||
for k, routes := range m.ReportMap {
|
var i []int
|
||||||
for r, d := range routes {
|
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",
|
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")
|
record := strings.Join(msgs, "\n")
|
||||||
buf.WriteString(record)
|
buf.WriteString(record)
|
||||||
buf.WriteString("\n------------------------------------------------------------------------------------------------------\n")
|
buf.WriteString("\n------------------------------------------------------------------------------------------------------\n")
|
||||||
// logrus.WithField("res", buf.String()).Debug("报告内容")
|
// 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 {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
@ -197,4 +199,5 @@ func (m *myAI) genReport() {
|
|||||||
if _, err := file.WriteString(buf.String()); err != nil {
|
if _, err := file.WriteString(buf.String()); err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
|
logrus.Debug("已生成测试报告")
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,8 @@ type CallResult struct {
|
|||||||
SceneName string
|
SceneName string
|
||||||
MainType string
|
MainType string
|
||||||
SubType string
|
SubType string
|
||||||
Elapse time.Duration // 耗时。
|
Elapse time.Duration // 耗时
|
||||||
|
Num int
|
||||||
}
|
}
|
||||||
|
|
||||||
type Statistics struct {
|
type Statistics struct {
|
||||||
|
25
lib/robot.go
25
lib/robot.go
@ -43,7 +43,7 @@ type Robot struct {
|
|||||||
config *storage.Config //配置
|
config *storage.Config //配置
|
||||||
resultCh chan *CallResult //请求结果通道
|
resultCh chan *CallResult //请求结果通道
|
||||||
sceneResultCh chan *CallResult //场景结果通道
|
sceneResultCh chan *CallResult //场景结果通道
|
||||||
ReportMap map[string]map[string]*Statistics //测试报告 key1:场景 key2:协议
|
ReportMap map[int]map[string]*Statistics //测试报告 key1:场景 key2:协议
|
||||||
elipseTotal time.Duration
|
elipseTotal time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ func NewRobot(config *storage.Config) *Robot {
|
|||||||
config: config,
|
config: config,
|
||||||
resultCh: make(chan *CallResult, 100),
|
resultCh: make(chan *CallResult, 100),
|
||||||
sceneResultCh: make(chan *CallResult, 50),
|
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)
|
robot.Store("sid", config.Global.SId)
|
||||||
return robot
|
return robot
|
||||||
@ -90,6 +90,7 @@ func (r *Robot) SendMsg(mainType, subType string, req proto.Message, rsp proto.M
|
|||||||
MainType: mainType,
|
MainType: mainType,
|
||||||
SubType: subType,
|
SubType: subType,
|
||||||
Elapse: t,
|
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 {
|
func (m *Robot) Start() bool {
|
||||||
if len(m.sceneQueue.List()) == 0 {
|
if len(m.sceneQueue.List()) == 0 {
|
||||||
logrus.Warn("没有设置场景队列")
|
logrus.Warn("没有设置场景队列")
|
||||||
@ -191,12 +203,11 @@ func (m *Robot) Stop() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Robot) syncCall() {
|
func (m *Robot) syncCall() {
|
||||||
// var sceneElipseTotal time.Duration // 场景总耗时
|
|
||||||
for {
|
for {
|
||||||
scene, err := m.sceneQueue.Pop()
|
scene, err := m.sceneQueue.Pop()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithField("err", err).Warn("所有场景执行结束")
|
logrus.WithField("err", err).Warn("所有场景执行结束")
|
||||||
// m.prepareToStop()
|
m.prepareToStop()
|
||||||
// m.genReport(sceneElipseTotal)
|
// m.genReport(sceneElipseTotal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -246,7 +257,7 @@ func (m *Robot) processResult() {
|
|||||||
m.lock.Lock()
|
m.lock.Lock()
|
||||||
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.SceneName]; ok {
|
if routes, ok := m.ReportMap[r.Num]; ok {
|
||||||
if route, y := routes[head]; y {
|
if route, y := routes[head]; y {
|
||||||
max := route.MaxElapse
|
max := route.MaxElapse
|
||||||
min := route.MinElapse
|
min := route.MinElapse
|
||||||
@ -281,7 +292,7 @@ func (m *Robot) processResult() {
|
|||||||
statis.AvgElapse = avg
|
statis.AvgElapse = avg
|
||||||
routes[head] = statis
|
routes[head] = statis
|
||||||
}
|
}
|
||||||
m.ReportMap[r.SceneName] = routes
|
m.ReportMap[r.Num] = routes
|
||||||
} else {
|
} else {
|
||||||
route := make(map[string]*Statistics)
|
route := make(map[string]*Statistics)
|
||||||
statis := &Statistics{
|
statis := &Statistics{
|
||||||
@ -295,7 +306,7 @@ func (m *Robot) processResult() {
|
|||||||
avg := (statis.MaxElapse + statis.MinElapse) / 2
|
avg := (statis.MaxElapse + statis.MinElapse) / 2
|
||||||
statis.AvgElapse = avg
|
statis.AvgElapse = avg
|
||||||
route[head] = statis
|
route[head] = statis
|
||||||
m.ReportMap[r.SceneName] = route
|
m.ReportMap[r.Num] = route
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,14 +51,14 @@ func TestA(t *testing.T) {
|
|||||||
|
|
||||||
func TestMerge(t *testing.T) {
|
func TestMerge(t *testing.T) {
|
||||||
config := &storage.Config{
|
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})
|
ma, _ := lib.NewAI(lib.AIParam{Config: config})
|
||||||
|
|
||||||
robot1 := lib.NewRobot(config)
|
robot1 := lib.NewRobot(config)
|
||||||
robot1.ReportMap = make(map[string]map[string]*lib.Statistics)
|
robot1.ReportMap = make(map[int]map[string]*lib.Statistics)
|
||||||
robot1.ReportMap["登录"] = make(map[string]*lib.Statistics)
|
robot1.ReportMap[1] = make(map[string]*lib.Statistics)
|
||||||
robot1.ReportMap["登录"]["user.login"] = &lib.Statistics{
|
robot1.ReportMap[1]["user.login"] = &lib.Statistics{
|
||||||
ElapseTotal: 5,
|
ElapseTotal: 5,
|
||||||
CallCount: 1,
|
CallCount: 1,
|
||||||
AvgElapse: 1,
|
AvgElapse: 1,
|
||||||
@ -70,9 +70,9 @@ func TestMerge(t *testing.T) {
|
|||||||
ma.AppendRobot(robot1)
|
ma.AppendRobot(robot1)
|
||||||
///////////////
|
///////////////
|
||||||
robot2 := lib.NewRobot(config)
|
robot2 := lib.NewRobot(config)
|
||||||
robot2.ReportMap = make(map[string]map[string]*lib.Statistics)
|
robot2.ReportMap = make(map[int]map[string]*lib.Statistics)
|
||||||
robot2.ReportMap["登录"] = make(map[string]*lib.Statistics)
|
robot2.ReportMap[1] = make(map[string]*lib.Statistics)
|
||||||
robot2.ReportMap["登录"]["user.login"] = &lib.Statistics{
|
robot2.ReportMap[1]["user.login"] = &lib.Statistics{
|
||||||
ElapseTotal: 7,
|
ElapseTotal: 7,
|
||||||
CallCount: 1,
|
CallCount: 1,
|
||||||
AvgElapse: 1,
|
AvgElapse: 1,
|
||||||
|
Loading…
Reference in New Issue
Block a user