From c3fe1f642739362359b3ec28694a369d0a43f6bf Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Wed, 14 Dec 2022 19:24:33 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=86=85=E9=83=A8=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- busi/core.go | 9 ++++++ busi/friend.go | 73 ++++++++++++++++++++++++++++++++++++++++++---- lib/robot.go | 58 +++++++++++++++++++++--------------- test/robot_test.go | 4 +-- 4 files changed, 114 insertions(+), 30 deletions(-) diff --git a/busi/core.go b/busi/core.go index 5ac8668..3c8139a 100644 --- a/busi/core.go +++ b/busi/core.go @@ -10,3 +10,12 @@ func Sleep(min, max time.Duration) { t := time.Duration(rand.Int63n(ulit)) + min time.Sleep(t) } + +func Retry(count int, f func() bool) { + for i := 0; i < count; i++ { + if f() { + break + } + Sleep(time.Second*1, time.Second*5) + } +} diff --git a/busi/friend.go b/busi/friend.go index 6118062..4456212 100644 --- a/busi/friend.go +++ b/busi/friend.go @@ -1,14 +1,21 @@ package busi import ( + "time" + + "github.com/Pallinder/go-randomdata" + "github.com/sirupsen/logrus" "legu.airobot/lib" "legu.airobot/pb" ) //好友场景 - var _ lib.IScene = (*FriendScene)(nil) +const ( + Friend_Maintype = "friend" +) + type FriendScene struct { lib.Action } @@ -21,9 +28,65 @@ func (f *FriendScene) Info() lib.SceneInfo { } func (f *FriendScene) Run(robot lib.IRobot) error { - req := &pb.FriendRandlistReq{} - rsp := &pb.FriendRandlistResp{} - robot.SendMsg("", "", req, rsp) - + friendList := friendRandlist(robot) + Sleep(time.Second, time.Second*3) + friendApply(robot, friendList) + Sleep(time.Second, time.Second*3) + friendAgree(robot) return nil } + +// 好友推荐 +func friendRandlist(robot lib.IRobot) *pb.FriendRandlistResp { + req := &pb.FriendRandlistReq{} + rsp := &pb.FriendRandlistResp{} + if code := robot.SendMsg(Friend_Maintype, "randlist", req, rsp); code != pb.ErrorCode_Success { + logrus.WithField("code", code).Error("好友推荐") + return nil + } + robot.Store("friend.randlist", rsp) + return rsp +} + +// 好友申请 +func friendApply(robot lib.IRobot, list *pb.FriendRandlistResp) { + req := &pb.FriendApplyReq{} + rsp := &pb.FriendApplyResp{} + + do := func() bool { + if list != nil && len(list.List) > 0 { + // 随件选择一个玩家 + randInt := randomdata.Number(0, len(list.List)) + req.FriendId = list.List[randInt].UserId + + if code := robot.SendMsg(Friend_Maintype, "apply", req, rsp); code != pb.ErrorCode_Success { + logrus.WithField("code", code).Error("好友申请") + } + } + return true + } + + Retry(3, do) +} + +// 好友申请同意 +func friendAgree(robot lib.IRobot) { + applyListReq := &pb.FriendApplyListReq{} + applyListRsp := &pb.FriendApplyListResp{} + + if code := robot.SendMsg(Friend_Maintype, "applylist", applyListReq, applyListRsp); code != pb.ErrorCode_Success { + logrus.WithField("code", code).Error("好友申请列表") + return + } + + //同意 + req := &pb.FriendAgreeReq{} + rsp := &pb.FriendAgreeResp{} + if applyListRsp != nil && len(applyListRsp.List) > 0 { + req.FriendIds = append(req.FriendIds, applyListRsp.List[0].UserId) + if code := robot.SendMsg(Friend_Maintype, "agree", req, rsp); code != pb.ErrorCode_Success { + logrus.WithField("code", code).Error("好友申请同意") + return + } + } +} diff --git a/lib/robot.go b/lib/robot.go index e5de1a5..631794e 100644 --- a/lib/robot.go +++ b/lib/robot.go @@ -218,29 +218,43 @@ func (m *Robot) Stop() bool { } func (m *Robot) syncCall() { - for { - scene, err := m.sceneQueue.Pop() - if err != nil { - logrus.WithField("err", err).Debug("所有场景执行结束") - m.prepareToStop() - return - } - m.scene = scene - info := m.scene.Info() + 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) + } + }() + 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) + 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() - } + m.elipseTotal += elapsedTime + logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计") + //显示场景结果 + m.processResult() + } + }() } func (m *Robot) prepareToStop() { @@ -267,8 +281,6 @@ func (m *Robot) SendResult(result *CallResult) bool { func (m *Robot) processResult() { go func() { - defer m.lock.Unlock() - m.lock.Lock() for r := range m.resultCh { head := fmt.Sprintf("%s.%s", r.MainType, r.SubType) if routes, ok := m.ReportMap[r.Num]; ok { diff --git a/test/robot_test.go b/test/robot_test.go index 64e347f..ed082d5 100644 --- a/test/robot_test.go +++ b/test/robot_test.go @@ -5,6 +5,7 @@ import ( "math" "testing" + "github.com/Pallinder/go-randomdata" "legu.airobot/busi/friend" "legu.airobot/lib" "legu.airobot/storage" @@ -157,6 +158,5 @@ func divide(a int, b int) int { func TestDiv(t *testing.T) { - res := lib.FormatFloatCommon(float64(3 / 10)) - fmt.Println(res) + fmt.Println(randomdata.Number(0, 5)) } From 26c497e03b1932127c74afd91894fc8682ea42bc Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Wed, 14 Dec 2022 19:39:09 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=8A=A5=E8=A1=A8?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ai.go | 2 +- lib/robot.go | 125 ++++++++++++++------------------------------------- 2 files changed, 34 insertions(+), 93 deletions(-) diff --git a/lib/ai.go b/lib/ai.go index af6aaab..8188873 100644 --- a/lib/ai.go +++ b/lib/ai.go @@ -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) diff --git a/lib/robot.go b/lib/robot.go index 631794e..07ed154 100644 --- a/lib/robot.go +++ b/lib/robot.go @@ -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 -} From afe52e807073ddd4dafa06f6815187a68f65ae8f Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Wed, 14 Dec 2022 19:40:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- busi/friend/friend_apply.go | 49 --------------------------------- busi/friend/friend_recommend.go | 38 ------------------------- 2 files changed, 87 deletions(-) delete mode 100644 busi/friend/friend_apply.go delete mode 100644 busi/friend/friend_recommend.go diff --git a/busi/friend/friend_apply.go b/busi/friend/friend_apply.go deleted file mode 100644 index 29b91d8..0000000 --- a/busi/friend/friend_apply.go +++ /dev/null @@ -1,49 +0,0 @@ -package friend - -import ( - "time" - - "google.golang.org/protobuf/proto" - "legu.airobot/lib" - "legu.airobot/pb" -) - -var _ lib.ICaller = (*FriendApply)(nil) - -type FriendApply struct { - lib.Action -} - -func (a *FriendApply) Info() lib.SceneInfo { - return lib.SceneInfo{ - Name: "friend.apply", - Desc: "好友申请", - } -} - -func (a *FriendApply) BuildReq(store lib.IStore, head *pb.UserMessage) lib.RawReq { - id := time.Now().UnixNano() - var req []byte - - b := store.Get("friend.apply") - - rsp := &pb.FriendRandlistResp{} - if err := proto.Unmarshal(b, rsp); err != nil { - panic(err) - } - - if len(rsp.List) == 0 { - return lib.RawReq{} - } - - if lib.ProtoMarshal(&pb.FriendApplyReq{ - FriendId: rsp.List[0].UserId, - }, head) { - data, err := proto.Marshal(head) - if err != nil { - panic(err) - } - req = data - } - return lib.RawReq{ID: id, Req: req} -} diff --git a/busi/friend/friend_recommend.go b/busi/friend/friend_recommend.go deleted file mode 100644 index 2c8e4c8..0000000 --- a/busi/friend/friend_recommend.go +++ /dev/null @@ -1,38 +0,0 @@ -package friend - -import ( - "time" - - "google.golang.org/protobuf/proto" - "legu.airobot/lib" - "legu.airobot/pb" -) - -var _ lib.ICaller = (*FriendRecommend)(nil) - -// 好友推荐 -type FriendRecommend struct { - lib.Action -} - -func (a *FriendRecommend) Info() lib.SceneInfo { - return lib.SceneInfo{ - Name: "friend.randlist", - Desc: "好友推荐", - } -} - -func (a *FriendRecommend) BuildReq(store lib.IStore, head *pb.UserMessage) lib.RawReq { - id := time.Now().UnixNano() - - var req []byte - if lib.ProtoMarshal(&pb.FriendRandlistReq{}, head) { - data, err := proto.Marshal(head) - if err != nil { - panic(err) - } - req = data - } - - return lib.RawReq{ID: id, Req: req} -}