加入报表时间

This commit is contained in:
wh_zcy 2022-12-14 19:39:09 +08:00
parent c3fe1f6427
commit 26c497e03b
2 changed files with 34 additions and 93 deletions

View File

@ -195,7 +195,7 @@ func (m *myAI) genReport(elipse time.Duration) {
}
record := strings.Join(msgs, "\n")
var buf bytes.Buffer
buf.WriteString("测试报告\n")
buf.WriteString(fmt.Sprintf("测试报告 时间:%v\n", time.Now().Format("2006-01-02 15:04:05")))
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)

View File

@ -1,13 +1,8 @@
package lib
import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"path/filepath"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
@ -218,43 +213,42 @@ func (m *Robot) Stop() bool {
}
func (m *Robot) syncCall() {
go func() {
defer func() {
if p := recover(); p != nil {
err, ok := interface{}(p).(error)
var errMsg string
if ok {
errMsg = fmt.Sprintf("调用时Panic! (error: %s)", err)
} else {
errMsg = fmt.Sprintf("调用时Panic! (clue: %#v)", p)
}
logrus.Error(errMsg)
defer func() {
if p := recover(); p != nil {
err, ok := interface{}(p).(error)
var errMsg string
if ok {
errMsg = fmt.Sprintf("调用时Panic! (error: %s)", err)
} else {
errMsg = fmt.Sprintf("调用时Panic! (clue: %#v)", p)
}
}()
for {
scene, err := m.sceneQueue.Pop()
if err != nil {
logrus.WithField("err", err).Debug("所有场景执行结束")
m.prepareToStop()
return
}
m.scene = scene
info := m.scene.Info()
start := time.Now()
//这里执行会花很长时间
if err := scene.Run(m); err != nil {
logrus.WithField("err", err).Error("执行业务时发生错误")
continue
}
elapsedTime := time.Since(start)
m.elipseTotal += elapsedTime
logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计")
//显示场景结果
m.processResult()
logrus.Error(errMsg)
}
}()
for {
scene, err := m.sceneQueue.Pop()
if err != nil {
logrus.WithField("err", err).Debug("所有场景执行结束")
m.prepareToStop()
return
}
m.scene = scene
info := m.scene.Info()
start := time.Now()
//这里执行会花很长时间
if err := scene.Run(m); err != nil {
logrus.WithField("err", err).Error("执行业务时发生错误")
continue
}
elapsedTime := time.Since(start)
m.elipseTotal += elapsedTime
logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计")
//显示场景结果
m.processResult()
}
}
func (m *Robot) prepareToStop() {
@ -339,62 +333,9 @@ func (m *Robot) processResult() {
}()
}
// 将统计结果写入文件
// Deprecated
func (m *Robot) genReport(e time.Duration) {
var buf bytes.Buffer
buf.WriteString("测试报告\n")
buf.WriteString(fmt.Sprintf("用户总数:%d 场景总耗时:%s\n",
m.config.Global.UserCountTotal, e.String()))
var msgs []string
for k, routes := range m.ReportMap {
// buf.WriteString(fmt.Sprintf("【%s】\n", k))
for r, d := range routes {
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()))
}
}
record := strings.Join(msgs, "\n")
buf.WriteString(record)
buf.WriteString("\n------------------------------------------------------------------------------\n")
// logrus.WithField("res", buf.String()).Debug("报告内容")
if err := ioutil.WriteFile(filepath.Join("./", "report.log"), buf.Bytes(), fs.ModePerm); err != nil {
logrus.WithField("err", err).Error("测试报告")
}
}
func (m *Robot) printIgnoredResult(result *CallResult, cause string) {
resultMsg := fmt.Sprintf(
"MainType=%s, SubType=%s, Elapse=%v",
result.MainType, result.SubType, result.Elapse)
logrus.Warnf("Ignored result: %s. (cause: %s)\n", resultMsg, cause)
}
// Deprecated
func (m *Robot) checkResp(data []byte, rsp proto.Message) bool {
msg := &pb.UserMessage{}
if err := proto.Unmarshal(data, msg); err != nil {
logrus.Error("pb解析失败")
return false
}
methodStr := msg.Data.TypeUrl
methodName := SubStr(methodStr, 20, len(methodStr))
if strings.HasSuffix(methodName, "Push") {
if methodName == "NotifyErrorNotifyPush" {
push := &pb.NotifyErrorNotifyPush{}
if !ProtoUnmarshal(msg, push) {
logrus.Error("pb解析失败")
return false
}
logrus.WithField("methodName", methodName).WithField("code", push.Code).Debug("收到错误推送")
}
return false
} else {
if !ProtoUnmarshal(msg, rsp) {
return false
}
}
return true
}