From bace8219aebbb5a33880daf863cfda2c586fe1f6 Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Tue, 13 Dec 2022 19:26:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BB=9F=E8=AE=A1=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ai.go | 28 +++++++++++++++++++--------- lib/robot.go | 1 - test/robot_test.go | 29 ++++++++++++++++++++++------- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/lib/ai.go b/lib/ai.go index 272d52c..03ae90f 100644 --- a/lib/ai.go +++ b/lib/ai.go @@ -94,12 +94,14 @@ func (m *myAI) Start() bool { }() go func() { + start := time.Now() for { total := atomic.LoadUint32(&m.useCountTotal) if total == m.config.Global.UserCountTotal { + elipse := time.Since(start) logrus.Info("开始生成测试报告") m.MergeResult() - m.genReport() + m.genReport(elipse) break } } @@ -132,11 +134,19 @@ func (m *myAI) MergeResult() { //判断协议是否相同 if k == l { statis := &Statistics{} - statis.ElapseTotal = time.Duration(int64(a.ElapseTotal+b.ElapseTotal) / int64(2)) + statis.ElapseTotal = time.Duration(int64(a.ElapseTotal + b.ElapseTotal)) 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)) + max := a.MaxElapse + if max < b.MaxElapse { + max = b.MaxElapse + } + statis.MaxElapse = max + min := a.MinElapse + if min > b.MinElapse { + min = b.MinElapse + } + statis.MinElapse = min statis.Route = a.Route statis.SceneName = a.SceneName if n[i][k] == nil { @@ -170,11 +180,7 @@ func (m *myAI) MergeResult() { m.ReportMap = n } -func (m *myAI) genReport() { - var buf bytes.Buffer - buf.WriteString("测试报告\n") - buf.WriteString(fmt.Sprintf("用户总数:%d 单次投放人数:%d 投放间隔时间:%ds\n", - m.config.Global.UserCountTotal, m.config.Global.UserCount, m.config.Global.IntervalS)) +func (m *myAI) genReport(elipse time.Duration) { var msgs []string var i []int for key, _ := range m.ReportMap { @@ -188,6 +194,10 @@ func (m *myAI) genReport() { } } record := strings.Join(msgs, "\n") + var buf bytes.Buffer + buf.WriteString("测试报告\n") + buf.WriteString(fmt.Sprintf("用户总数:%d 单次投放人数:%d 投放间隔时间:%ds 共计时间:%s\n", + m.config.Global.UserCountTotal, m.config.Global.UserCount, m.config.Global.IntervalS, elipse.String())) buf.WriteString(record) buf.WriteString("\n------------------------------------------------------------------------------------------------------\n") // logrus.WithField("res", buf.String()).Debug("报告内容") diff --git a/lib/robot.go b/lib/robot.go index 2a93ab2..67c5310 100644 --- a/lib/robot.go +++ b/lib/robot.go @@ -208,7 +208,6 @@ func (m *Robot) syncCall() { if err != nil { logrus.WithField("err", err).Debug("所有场景执行结束") m.prepareToStop() - // m.genReport(sceneElipseTotal) return } m.scene = scene diff --git a/test/robot_test.go b/test/robot_test.go index a7fb797..df408c7 100644 --- a/test/robot_test.go +++ b/test/robot_test.go @@ -61,9 +61,9 @@ func TestMerge(t *testing.T) { robot1.ReportMap[1]["user.login"] = &lib.Statistics{ ElapseTotal: 5, CallCount: 2, - AvgElapse: 1, + AvgElapse: 2, MaxElapse: 4, - MinElapse: 1, + MinElapse: 2, Route: "user.login", SceneName: "登录", } @@ -75,9 +75,9 @@ func TestMerge(t *testing.T) { robot2.ReportMap[1]["user.login"] = &lib.Statistics{ ElapseTotal: 7, CallCount: 1, - AvgElapse: 1, + AvgElapse: 3, MaxElapse: 5, - MinElapse: 2, + MinElapse: 4, Route: "user.login", SceneName: "登录", } @@ -89,15 +89,30 @@ func TestMerge(t *testing.T) { robot3.ReportMap[1]["user.login"] = &lib.Statistics{ ElapseTotal: 8, CallCount: 1, - AvgElapse: 3, - MaxElapse: 2, - MinElapse: 2, + AvgElapse: 2, + MaxElapse: 6, + MinElapse: 3, Route: "user.login", SceneName: "登录", } ma.AppendRobot(robot3) ma.MergeResult() + robot4 := lib.NewRobot(config) + robot4.ReportMap = make(map[int]map[string]*lib.Statistics) + robot4.ReportMap[1] = make(map[string]*lib.Statistics) + robot4.ReportMap[1]["user.login"] = &lib.Statistics{ + ElapseTotal: 8, + CallCount: 1, + AvgElapse: 2, + MaxElapse: 8, + MinElapse: 2, + Route: "user.login", + SceneName: "登录", + } + ma.AppendRobot(robot4) + ma.MergeResult() + fmt.Println(ma.ReportMap) for i, v := range ma.ReportMap { fmt.Println(i)