From 4fcf45941383cbf3d7dff65f4595737f133a1f07 Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Tue, 13 Dec 2022 09:52:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95=E6=8A=A5?= =?UTF-8?q?=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/robot.go | 107 +++++++++++++++++++++++++++-------------------- ui/mainwindow.go | 12 +++--- 2 files changed, 68 insertions(+), 51 deletions(-) diff --git a/lib/robot.go b/lib/robot.go index 804f13e..3b42f1f 100644 --- a/lib/robot.go +++ b/lib/robot.go @@ -1,7 +1,11 @@ package lib import ( + "bytes" "fmt" + "io/fs" + "io/ioutil" + "path/filepath" "sort" "strings" "sync" @@ -9,7 +13,6 @@ import ( "time" "github.com/gorilla/websocket" - "github.com/nacos-group/nacos-sdk-go/common/logger" "github.com/sirupsen/logrus" "google.golang.org/protobuf/proto" "legu.airobot/pb" @@ -171,7 +174,6 @@ func (m *Robot) Start() bool { m.syncCall() - logrus.Debug("机器人运行了") return true } @@ -184,6 +186,7 @@ func (m *Robot) syncCall() { scene, err := m.sceneQueue.Pop() if err != nil { logrus.WithField("err", err).Error("所有场景执行结束") + // m.prepareToStop() return } @@ -197,15 +200,15 @@ func (m *Robot) syncCall() { info := scene.Info() logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计") //结束 - m.prepareToStop() + // m.prepareToStop() //显示场景结果 - + m.ShowResult() } } func (m *Robot) prepareToStop() { atomic.CompareAndSwapUint32(&m.status, STATUS_STARTED, STATUS_STOPPING) - logger.Infof("Closing result channel...") + logrus.Infof("关闭结果通道...") close(m.resultCh) atomic.StoreUint32(&m.status, STATUS_STOPPED) } @@ -226,45 +229,11 @@ func (m *Robot) SendResult(result *CallResult) bool { } func (m *Robot) ShowResult() { - // statistics := &Statistics{} - // max := statistics.MaxElapse - // min := statistics.MinElapse - routes := make(map[string]*Statistics) - for r := range m.resultCh { - head := fmt.Sprintf("%s.%s", r.MainType, r.SubType) - if len(routes) == 0 { - statis := &Statistics{ - Route: head, - ElapseTotal: r.Elapse, - MaxElapse: r.Elapse, - MinElapse: r.Elapse, - CallCount: 1, - } - avg := (statis.MaxElapse + statis.MinElapse) / 2 - statis.AvgElapse = avg - routes[head] = statis - } else { - if route, ok := routes[head]; ok { - max := route.MaxElapse - min := route.MinElapse - if r.Elapse > max { - max = r.Elapse - } - - if r.Elapse < min { - min = r.Elapse - } - statis := &Statistics{ - Route: head, - ElapseTotal: route.ElapseTotal + r.Elapse, - MaxElapse: max, - MinElapse: min, - CallCount: route.CallCount + 1, - } - avg := (statis.MaxElapse + statis.MinElapse) / 2 - statis.AvgElapse = avg - routes[head] = statis - } else { + go func() { + routes := make(map[string]*Statistics) + for r := range m.resultCh { + head := fmt.Sprintf("%s.%s", r.MainType, r.SubType) + if len(routes) == 0 { statis := &Statistics{ Route: head, ElapseTotal: r.Elapse, @@ -275,11 +244,57 @@ func (m *Robot) ShowResult() { avg := (statis.MaxElapse + statis.MinElapse) / 2 statis.AvgElapse = avg routes[head] = statis + } else { + if route, ok := routes[head]; ok { + max := route.MaxElapse + min := route.MinElapse + if r.Elapse > max { + max = r.Elapse + } + + if r.Elapse < min { + min = r.Elapse + } + statis := &Statistics{ + Route: head, + ElapseTotal: route.ElapseTotal + r.Elapse, + MaxElapse: max, + MinElapse: min, + CallCount: route.CallCount + 1, + } + avg := (statis.MaxElapse + statis.MinElapse) / 2 + statis.AvgElapse = avg + routes[head] = statis + } else { + statis := &Statistics{ + Route: head, + ElapseTotal: r.Elapse, + MaxElapse: r.Elapse, + MinElapse: r.Elapse, + CallCount: 1, + } + avg := (statis.MaxElapse + statis.MinElapse) / 2 + statis.AvgElapse = avg + routes[head] = statis + } } } - } - //TODO 将统计结果写入文件 + // 将统计结果写入文件 + var msgs []string + for k, v := range routes { + msgs = append(msgs, fmt.Sprintf("协议:%s 调用次数:%d 总耗时:%v 平均耗时:%v 最大耗时:%v 最小耗时:%v", + k, v.CallCount, v.ElapseTotal.String(), v.AvgElapse.String(), v.MaxElapse.String(), v.MinElapse.String())) + } + record := strings.Join(msgs, "\n") + var buf bytes.Buffer + buf.WriteString("测试报告\n") + buf.WriteString(record) + if err := ioutil.WriteFile(filepath.Join("./", "report.txt"), buf.Bytes(), fs.ModePerm); err != nil { + logrus.WithField("err", err).Error("测试报告") + } + }() + } func (m *Robot) printIgnoredResult(result *CallResult, cause string) { diff --git a/ui/mainwindow.go b/ui/mainwindow.go index bc23a0e..7cf17aa 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -143,6 +143,7 @@ func (mw *MainWindow) configContainer() { loadForm := func() { wsAddrEntry.Text = config.WsAddr sidEntry.Text = config.SId + userTotalEntry.Text = cast.ToString(config.UserCountTotal) userCountEntry.Text = cast.ToString(config.UserCount) timeoutEntry.Text = cast.ToString(config.TimeoutMs) intervalEntry.Text = cast.ToString(config.IntervalS) @@ -155,11 +156,12 @@ func (mw *MainWindow) configContainer() { saveBtn := widget.NewButtonWithIcon("保存", theme.DocumentSaveIcon(), nil) saveBtn.OnTapped = func() { global := &storage.Global{ - WsAddr: wsAddrEntry.Text, - SId: sidEntry.Text, - UserCount: cast.ToUint32(userCountEntry.Text), - TimeoutMs: cast.ToInt32(timeoutEntry.Text), - IntervalS: cast.ToInt32(intervalEntry.Text), + WsAddr: wsAddrEntry.Text, + SId: sidEntry.Text, + UserCountTotal: cast.ToUint32(userTotalEntry.Text), + UserCount: cast.ToUint32(userCountEntry.Text), + TimeoutMs: cast.ToInt32(timeoutEntry.Text), + IntervalS: cast.ToInt32(intervalEntry.Text), } mw.config.Global = global if err := mw.storage.StoreConfig(mw.config); err != nil {