This commit is contained in:
liwei1dao 2022-12-14 19:49:09 +08:00
parent 512e1a69bd
commit 7435ee1151
7 changed files with 130 additions and 161 deletions

View File

@ -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)
}
}

View File

@ -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
}
}
}

View File

@ -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}
}

View File

@ -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}
}

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"
@ -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,6 +273,7 @@ func (m *Robot) SendResult(result *CallResult) bool {
}
func (m *Robot) processResult() {
go func() {
for r := range m.resultCh {
head := fmt.Sprintf("%s.%s", r.MainType, r.SubType)
if routes, ok := m.ReportMap[r.Num]; ok {
@ -302,6 +311,34 @@ func (m *Robot) processResult() {
statis.AvgElapse = avg
routes[head] = statis
}
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
}
m.ReportMap[r.Num] = routes
} else {
route := make(map[string]*Statistics)
@ -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
}

View File

@ -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))
}