diff --git a/lib/ai.go b/lib/ai.go index ad04c8d..272d52c 100644 --- a/lib/ai.go +++ b/lib/ai.go @@ -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("已生成测试报告") } diff --git a/lib/robot.go b/lib/robot.go index d015772..2a93ab2 100644 --- a/lib/robot.go +++ b/lib/robot.go @@ -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) } diff --git a/main.go b/main.go index f90fa1c..6e93c80 100644 --- a/main.go +++ b/main.go @@ -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, diff --git a/test/robot_test.go b/test/robot_test.go index 4d64e8f..a7fb797 100644 --- a/test/robot_test.go +++ b/test/robot_test.go @@ -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)