From 7435ee115182185db7960d523e7b7defefbf78ae Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 14 Dec 2022 19:49:09 +0800 Subject: [PATCH] Merge branch 'master' of http://git.legu.cc/zhaochangyuan/airobot --- busi/core.go | 9 +++ busi/friend.go | 73 ++++++++++++++++++-- busi/friend/friend_apply.go | 49 -------------- busi/friend/friend_recommend.go | 38 ----------- lib/ai.go | 2 +- lib/robot.go | 116 ++++++++++++++------------------ test/robot_test.go | 4 +- 7 files changed, 130 insertions(+), 161 deletions(-) delete mode 100644 busi/friend/friend_apply.go delete mode 100644 busi/friend/friend_recommend.go 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/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} -} 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 acf31cc..9f6bff2 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" @@ -219,6 +214,18 @@ func (m *Robot) Stop() bool { } func (m *Robot) syncCall() { + 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 { @@ -240,6 +247,7 @@ func (m *Robot) syncCall() { m.elipseTotal += elapsedTime logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计") } + } func (m *Robot) prepareToStop() { @@ -265,14 +273,43 @@ func (m *Robot) SendResult(result *CallResult) bool { } func (m *Robot) processResult() { - for r := range m.resultCh { - head := fmt.Sprintf("%s.%s", r.MainType, r.SubType) - if routes, ok := m.ReportMap[r.Num]; ok { - if route, y := routes[head]; y { - max := route.MaxElapse - min := route.MinElapse - if r.Elapse > max { - max = r.Elapse + go func() { + for r := range m.resultCh { + head := fmt.Sprintf("%s.%s", r.MainType, r.SubType) + if routes, ok := m.ReportMap[r.Num]; ok { + if route, y := routes[head]; y { + max := route.MaxElapse + min := route.MinElapse + if r.Elapse > max { + max = r.Elapse + } + + if r.Elapse < min { + min = r.Elapse + } + statis := &Statistics{ + Route: head, + SceneName: r.SceneName, + 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, + SceneName: r.SceneName, + ElapseTotal: r.Elapse, + MaxElapse: r.Elapse, + MinElapse: r.Elapse, + CallCount: 1, + } + avg := (statis.MaxElapse + statis.MinElapse) / 2 + statis.AvgElapse = avg + routes[head] = statis } if r.Elapse < min { @@ -321,62 +358,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 -} 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)) }