修改统计计算

This commit is contained in:
wh_zcy 2022-12-13 19:26:16 +08:00
parent 09f8cd1926
commit bace8219ae
3 changed files with 41 additions and 17 deletions

View File

@ -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("报告内容")

View File

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

View File

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