From 512e1a69bd4e160ca7ff9d909a59ba1a2f624744 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 14 Dec 2022 19:43:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- busi/arena.go | 8 +++-- busi/chat.go | 4 +-- busi/hero.go | 64 ++++++++++++++++++++-------------- busi/login.go | 3 ++ busi/mfantasy.go | 6 ++-- busi/pay.go | 18 +++++----- busi/resmgr.go | 10 ++++-- busi/shop.go | 13 +++---- lib/robot.go | 90 ++++++++++++++++++++++-------------------------- main.go | 1 + 10 files changed, 119 insertions(+), 98 deletions(-) diff --git a/busi/arena.go b/busi/arena.go index fb3dd75..e0d04e5 100644 --- a/busi/arena.go +++ b/busi/arena.go @@ -39,12 +39,16 @@ func (f *ArenaScene) Run(ai lib.IRobot) (err error) { return } } - + if code = ai.SendMsg("arena", "info", &pb.ArenaInfoReq{}, &pb.ArenaInfoResp{}); code != pb.ErrorCode_Success { + ai.Stop() + return + } + Sleep(time.Second*0, time.Second*1) if code = ai.SendMsg("arena", "matche", &pb.ArenaMatcheReq{}, matche); code != pb.ErrorCode_Success { ai.Stop() return } - + Sleep(time.Second*0, time.Second*1) for _, v := range matche.Players { if code = ai.SendMsg("arena", "challenge", &pb.ArenaChallengeReq{ Playerid: v.Uid, diff --git a/busi/chat.go b/busi/chat.go index fec5d69..31ea5bb 100644 --- a/busi/chat.go +++ b/busi/chat.go @@ -36,12 +36,12 @@ func (f *ChatScene) Run(ai lib.IRobot) (err error) { ai.Stop() return } - time.Sleep(time.Second * 1) + Sleep(time.Second*0, time.Second*3) //工会聊天 if resp := ai.Get("user.login"); resp != nil { userlogin = resp.(*pb.UserLoginResp) - if userlogin.Ex.SociatyId != "" { + if userlogin.Ex != nil && userlogin.Ex.SociatyId != "" { if code = ai.SendMsg("chat", "send", &pb.ChatSendReq{ Channel: pb.ChatChannel_Union, TargetId: userlogin.Ex.SociatyId, diff --git a/busi/hero.go b/busi/hero.go index 2fe5111..66dfb36 100644 --- a/busi/hero.go +++ b/busi/hero.go @@ -26,44 +26,56 @@ func (f *HeroScene) Info() lib.SceneInfo { func (f *HeroScene) Run(robot lib.IRobot) error { f.AddHero(robot, "25001") // 加阿宝 + Sleep(time.Second*0, time.Second*1) f.AddHero(robot, "44911") // 升级精灵 + Sleep(time.Second*0, time.Second*1) f.AddHero(robot, "45921") // 技能精灵 + Sleep(time.Second*0, time.Second*1) f.AddHero(robot, "45921") // 技能精灵 + Sleep(time.Second*0, time.Second*1) // f.AddAttrGold(robot) // 加金币 // f.AddAttrDiamond(robot) // 加钻石 - time.Sleep(10 * time.Millisecond) // 等待10ms - f.HeroLvUp(robot) // 英雄升级 - time.Sleep(10 * time.Millisecond) // 等待10ms - f.HeroSkillLvUp(robot) // 技能升级 + f.HeroLvUp(robot) // 英雄升级 + Sleep(time.Second*0, time.Second*1) + f.HeroSkillLvUp(robot) // 技能升级 // 升星 - time.Sleep(10 * time.Millisecond) // 等待10ms + Sleep(time.Second*0, time.Second*1) f.HeroStarUp(robot) - + Sleep(time.Second*0, time.Second*1) // time.Sleep(50 * time.Millisecond) // f.HeroResonance(robot) // 共鸣 f.AddItem(robot, "50001") // 觉醒材料 + Sleep(time.Second*0, time.Second*1) f.HeroAwaken(robot) - + Sleep(time.Second*0, time.Second*1) f.AddItem(robot, "30001") // 抽卡材料 - f.HeroDraw(robot) // 普通十连 - + Sleep(time.Second*0, time.Second*1) + f.HeroDraw(robot) // 普通十连 + Sleep(time.Second*0, time.Second*1) // 购买抽卡道具 f.HeroDraw(robot) - + Sleep(time.Second*0, time.Second*1) // 卡牌融合 f.AddHero(robot, "25004") // 加英雄 + Sleep(time.Second*0, time.Second*1) f.AddHero(robot, "25004") // 加英雄 + Sleep(time.Second*0, time.Second*1) f.AddHero(robot, "25004") // 加英雄 + Sleep(time.Second*0, time.Second*1) f.AddHero(robot, "25004") // 加英雄 + Sleep(time.Second*0, time.Second*1) f.HeroFusion(robot) - - f.AddItem(robot, "525001") + Sleep(time.Second*0, time.Second*1) + f.AddItem(robot, "25001") + Sleep(time.Second*0, time.Second*1) f.HeroTalent(robot) + Sleep(time.Second*0, time.Second*1) f.HeroTalentV2(robot) // 学习其他天赋 + Sleep(time.Second*0, time.Second*1) return nil } @@ -156,19 +168,21 @@ func (f *HeroScene) HeroLvUp(robot lib.IRobot) error { break } } - // 升级 - lvReq := &pb.HeroStrengthenUplvReq{ - HeroObjID: lvcard.Id, - ExpCards: []*pb.MapStringInt32{}, - } - lvReq.ExpCards = append(lvReq.ExpCards, &pb.MapStringInt32{ - Key: expCard.Id, - Value: 1, - }) - lvRsp := &pb.HeroStrengthenUplvResp{} - code = robot.SendMsg("hero", "strengthenuplv", lvReq, lvRsp) - if code != pb.ErrorCode_Success { - return nil + if lvcard != nil { + // 升级 + lvReq := &pb.HeroStrengthenUplvReq{ + HeroObjID: lvcard.Id, + ExpCards: []*pb.MapStringInt32{}, + } + lvReq.ExpCards = append(lvReq.ExpCards, &pb.MapStringInt32{ + Key: expCard.Id, + Value: 1, + }) + lvRsp := &pb.HeroStrengthenUplvResp{} + code = robot.SendMsg("hero", "strengthenuplv", lvReq, lvRsp) + if code != pb.ErrorCode_Success { + return nil + } } return nil diff --git a/busi/login.go b/busi/login.go index ad8d513..d1a4808 100644 --- a/busi/login.go +++ b/busi/login.go @@ -1,6 +1,8 @@ package busi import ( + "time" + "github.com/Pallinder/go-randomdata" "legu.airobot/lib" "legu.airobot/pb" @@ -33,5 +35,6 @@ func (l *LoginScene) Run(robot lib.IRobot) error { if code := robot.SendMsg("user", "login", req, rsp); code == pb.ErrorCode_Success { robot.Store("user.login", rsp) } + Sleep(time.Second*0, time.Second*1) return nil } diff --git a/busi/mfantasy.go b/busi/mfantasy.go index 6cd36fc..93bd990 100644 --- a/busi/mfantasy.go +++ b/busi/mfantasy.go @@ -40,17 +40,17 @@ func (f *MfantasyScene) Run(ai lib.IRobot) (err error) { return } } - + Sleep(time.Second*0, time.Second*1) if code = ai.SendMsg("gm", "cmd", &pb.GMCmdReq{ Cmod: "bingo:moon,1", }, &pb.GMCmdResp{}); code != pb.ErrorCode_Success { ai.Stop() } - + Sleep(time.Second*0, time.Second*1) if code = ai.SendMsg("moonfantasy", "getlist", &pb.MoonfantasyGetListReq{}, mflist); code != pb.ErrorCode_Success { ai.Stop() } - + Sleep(time.Second*0, time.Second*1) for _, v := range mflist.Dfantasys { if code = ai.SendMsg("moonfantasy", "battle", &pb.MoonfantasyBattleReq{ Mid: v.Monster, diff --git a/busi/pay.go b/busi/pay.go index 62820cc..2ddc97d 100644 --- a/busi/pay.go +++ b/busi/pay.go @@ -31,21 +31,21 @@ func (f *PayScene) Run(ai lib.IRobot) (err error) { }, &pb.PayDailyBuyResp{}); code != pb.ErrorCode_Success { return } - time.Sleep(time.Second) + Sleep(time.Second*0, time.Second*1) if code = ai.SendMsg("pay", "dailybuy", &pb.PayDailyBuyReq{ Id: 2, }, &pb.PayDailyBuyResp{}); code != pb.ErrorCode_Success { ai.Stop() return } - time.Sleep(time.Second) + Sleep(time.Second*0, time.Second*1) - if code = ai.SendMsg("privilege", "buyyueka", &pb.PrivilegeBuyYuekaReq{ - CID: "yueka_lv1", - }, &pb.PrivilegeBuyYuekaResp{}); code != pb.ErrorCode_Success { - ai.Stop() - return - } - time.Sleep(time.Second) + // if code = ai.SendMsg("privilege", "buyyueka", &pb.PrivilegeBuyYuekaReq{ + // CID: "yueka_lv1", + // }, &pb.PrivilegeBuyYuekaResp{}); code != pb.ErrorCode_Success { + // ai.Stop() + // return + // } + // time.Sleep(time.Second) return } diff --git a/busi/resmgr.go b/busi/resmgr.go index 75e285d..759ab37 100644 --- a/busi/resmgr.go +++ b/busi/resmgr.go @@ -1,6 +1,8 @@ package busi import ( + "time" + "legu.airobot/lib" "legu.airobot/pb" ) @@ -29,6 +31,7 @@ func (f *ResMgrScene) Run(ai lib.IRobot) (err error) { }, &pb.GMCmdResp{}); code != pb.ErrorCode_Success { ai.Stop() } + Sleep(time.Second*0, time.Second*1) herolistreq := &pb.HeroListReq{} herolistresp := &pb.HeroListResp{} if code = ai.SendMsg("hero", "list", herolistreq, herolistresp); code != pb.ErrorCode_Success { @@ -36,13 +39,14 @@ func (f *ResMgrScene) Run(ai lib.IRobot) (err error) { return } ai.Store("hero.list", herolistresp) - + Sleep(time.Second*0, time.Second*1) itemlistreq := &pb.ItemsGetlistReq{} itemlistresp := &pb.ItemsGetlistResp{} - if code = ai.SendMsg("item", "list", itemlistreq, itemlistresp); code != pb.ErrorCode_Success { + if code = ai.SendMsg("items", "list", itemlistreq, itemlistresp); code != pb.ErrorCode_Success { ai.Stop() return } - ai.Store("item.list", itemlistresp) + ai.Store("items.list", itemlistresp) + Sleep(time.Second*0, time.Second*1) return nil } diff --git a/busi/shop.go b/busi/shop.go index 3b9f67e..8099131 100644 --- a/busi/shop.go +++ b/busi/shop.go @@ -25,7 +25,7 @@ func (f *ShopScene) Info() lib.SceneInfo { func (f *ShopScene) Run(ai lib.IRobot) (err error) { var ( code pb.ErrorCode - shops *pb.ShopGetListResp + shops *pb.ShopGetListResp = &pb.ShopGetListResp{} ) if code = ai.SendMsg("gm", "cmd", &pb.GMCmdReq{ Cmod: "bingo:attr,gold,100000", @@ -42,11 +42,12 @@ func (f *ShopScene) Run(ai lib.IRobot) (err error) { } for _, v := range shops.Goods { if v.LeftBuyNum > 0 { - ai.SendMsg("shop", "buy", &pb.ChatSendReq{ - Channel: pb.ChatChannel_World, - Content: "hello! are you good?", - }, shops) - time.Sleep(time.Second * 1) + ai.SendMsg("shop", "buy", &pb.ShopBuyReq{ + ShopType: pb.ShopType_GoldShop, + GoodsId: v.GoodsId, + BuyNum: 1, + }, &pb.ShopBuyResp{}) + Sleep(time.Second*0, time.Second*1) } } return nil diff --git a/lib/robot.go b/lib/robot.go index e5de1a5..acf31cc 100644 --- a/lib/robot.go +++ b/lib/robot.go @@ -207,7 +207,8 @@ func (m *Robot) Start() bool { //设置状态为已启动 atomic.StoreUint32(&m.status, STATUS_STARTED) - + //显示场景结果 + go m.processResult() m.syncCall() return true @@ -238,8 +239,6 @@ func (m *Robot) syncCall() { m.elipseTotal += elapsedTime logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计") - //显示场景结果 - m.processResult() } } @@ -266,49 +265,31 @@ 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 { - 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 + 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 } - m.ReportMap[r.Num] = routes + + 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 { - route := make(map[string]*Statistics) statis := &Statistics{ Route: head, SceneName: r.SceneName, @@ -319,12 +300,25 @@ func (m *Robot) processResult() { } avg := (statis.MaxElapse + statis.MinElapse) / 2 statis.AvgElapse = avg - route[head] = statis - m.ReportMap[r.Num] = route + routes[head] = statis } + m.ReportMap[r.Num] = routes + } else { + route := make(map[string]*Statistics) + 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 + route[head] = statis + m.ReportMap[r.Num] = route } - - }() + } } // 将统计结果写入文件 diff --git a/main.go b/main.go index 6412ab3..0a781bc 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ func init() { &busi.ArenaScene{}, &busi.ReddotScene{}, &busi.PayScene{}, + &busi.MfantasyScene{}, ) } From 7435ee115182185db7960d523e7b7defefbf78ae Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 14 Dec 2022 19:49:09 +0800 Subject: [PATCH 2/2] 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)) }