修复报告

This commit is contained in:
wh_zcy 2022-12-13 18:39:12 +08:00
parent 4d1ca3bf95
commit 09f8cd1926
4 changed files with 31 additions and 18 deletions

View File

@ -74,7 +74,7 @@ func (m *myAI) Start() bool {
logrus.Warn("还未设置场景")
return false
}
logrus.Info("测试中...")
go func() {
for {
m.tickets.Take()
@ -97,6 +97,7 @@ func (m *myAI) Start() bool {
for {
total := atomic.LoadUint32(&m.useCountTotal)
if total == m.config.Global.UserCountTotal {
logrus.Info("开始生成测试报告")
m.MergeResult()
m.genReport()
break
@ -116,10 +117,10 @@ func (m *myAI) MergeResult() {
defer m.lock.Unlock()
n := make(map[int]map[string]*Statistics)
for _, r := range m.robots {
if len(m.ReportMap) == 0 {
m.ReportMap = r.ReportMap
if len(n) == 0 {
n = r.ReportMap
} else {
x := m.ReportMap //已存在的
x := n //已存在的
y := r.ReportMap //将要合并的
for i, v := range x {
@ -131,11 +132,11 @@ func (m *myAI) MergeResult() {
//判断协议是否相同
if k == l {
statis := &Statistics{}
statis.ElapseTotal = (a.ElapseTotal + b.ElapseTotal) / 2
statis.AvgElapse = (a.AvgElapse + b.AvgElapse) / 2
statis.CallCount = a.CallCount + b.CallCount
statis.MaxElapse = (a.MaxElapse + b.MaxElapse) / 2
statis.MinElapse = (a.MinElapse + b.MinElapse) / 2
statis.ElapseTotal = time.Duration(int64(a.ElapseTotal+b.ElapseTotal) / int64(2))
statis.AvgElapse = time.Duration(int64(a.AvgElapse+b.AvgElapse) / int64(2))
statis.CallCount = (a.CallCount + b.CallCount)
statis.MaxElapse = time.Duration(int64(a.MaxElapse+b.MaxElapse) / int64(2))
statis.MinElapse = time.Duration(int64(a.MinElapse+b.MinElapse) / int64(2))
statis.Route = a.Route
statis.SceneName = a.SceneName
if n[i][k] == nil {
@ -172,8 +173,8 @@ func (m *myAI) MergeResult() {
func (m *myAI) genReport() {
var buf bytes.Buffer
buf.WriteString("测试报告\n")
buf.WriteString(fmt.Sprintf("用户总数:%d\n",
m.config.Global.UserCountTotal))
buf.WriteString(fmt.Sprintf("用户总数:%d 单次投放人数:%d 投放间隔时间:%ds\n",
m.config.Global.UserCountTotal, m.config.Global.UserCount, m.config.Global.IntervalS))
var msgs []string
var i []int
for key, _ := range m.ReportMap {
@ -199,5 +200,5 @@ func (m *myAI) genReport() {
if _, err := file.WriteString(buf.String()); err != nil {
logrus.Error(err)
}
logrus.Debug("已生成测试报告")
logrus.Info("已生成测试报告")
}

View File

@ -206,7 +206,7 @@ func (m *Robot) syncCall() {
for {
scene, err := m.sceneQueue.Pop()
if err != nil {
logrus.WithField("err", err).Warn("所有场景执行结束")
logrus.WithField("err", err).Debug("所有场景执行结束")
m.prepareToStop()
// m.genReport(sceneElipseTotal)
return
@ -231,7 +231,7 @@ func (m *Robot) syncCall() {
func (m *Robot) prepareToStop() {
atomic.CompareAndSwapUint32(&m.status, STATUS_STARTED, STATUS_STOPPING)
logrus.Infof("关闭结果通道...")
logrus.Debug("关闭结果通道...")
close(m.resultCh)
atomic.StoreUint32(&m.status, STATUS_STOPPED)
}

View File

@ -66,10 +66,9 @@ func setupLogger() (err error) {
// FullTimestamp: true,
})
logrus.SetLevel(logrus.DebugLevel)
logrus.SetLevel(logrus.InfoLevel)
logrus.SetOutput(os.Stdout)
//设置output,默认为stderr,可以为任何io.Writer比如文件*os.File
file, err := os.OpenFile("robot.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
writers := []io.Writer{
file,

View File

@ -60,7 +60,7 @@ func TestMerge(t *testing.T) {
robot1.ReportMap[1] = make(map[string]*lib.Statistics)
robot1.ReportMap[1]["user.login"] = &lib.Statistics{
ElapseTotal: 5,
CallCount: 1,
CallCount: 2,
AvgElapse: 1,
MaxElapse: 4,
MinElapse: 1,
@ -82,7 +82,20 @@ func TestMerge(t *testing.T) {
SceneName: "登录",
}
ma.AppendRobot(robot2)
//////
robot3 := lib.NewRobot(config)
robot3.ReportMap = make(map[int]map[string]*lib.Statistics)
robot3.ReportMap[1] = make(map[string]*lib.Statistics)
robot3.ReportMap[1]["user.login"] = &lib.Statistics{
ElapseTotal: 8,
CallCount: 1,
AvgElapse: 3,
MaxElapse: 2,
MinElapse: 2,
Route: "user.login",
SceneName: "登录",
}
ma.AppendRobot(robot3)
ma.MergeResult()
fmt.Println(ma.ReportMap)