diff --git a/comm/const.go b/comm/const.go index 96e120aeb..338f92502 100644 --- a/comm/const.go +++ b/comm/const.go @@ -604,8 +604,9 @@ const ( Consumemoney = "consumemoney" //三消专属货币 ) const ( - Gold int32 = 1 //金币 - Diamond int32 = 2 //钻石 + Gold int32 = 1 //金币 + Diamond int32 = 2 //钻石 + Vouchers string = "10000035" //代金券 ) // 排行算虚拟币最低值 diff --git a/comm/imodule.go b/comm/imodule.go index 8acd93581..ee15e6472 100644 --- a/comm/imodule.go +++ b/comm/imodule.go @@ -166,6 +166,7 @@ type ( GetUserSessions(uids []string) []*pb.CacheUser //查询用户属性值 例如 金币 经验 QueryAttributeValue(uid string, attr string) (value int64) + QueryAttributeValues(uid string, attr map[string]int64) //添加/减少属性值 第四个参数控制是否推送给前端 // AddAttributeValue(session IUserSession, attr string, add int32, bPush bool) (errdata *pb.ErrorData) // 批量处理 diff --git a/modules/pay/api_delivery.go b/modules/pay/api_delivery.go index 268783cdb..67a08dc34 100644 --- a/modules/pay/api_delivery.go +++ b/modules/pay/api_delivery.go @@ -20,6 +20,7 @@ func (this *apiComp) DeliveryCheck(session comm.IUserSession, req *pb.PayDeliver func (this *apiComp) Delivery(session comm.IUserSession, req *pb.PayDeliveryReq) (errdata *pb.ErrorData) { var ( conf *cfg.GameRechargeData + need []*cfg.Gameatn order *pb.DBPayOrder err error ) @@ -44,12 +45,19 @@ func (this *apiComp) Delivery(session comm.IUserSession, req *pb.PayDeliveryReq) Price: conf.Amount, Amount: 1, } + need = []*cfg.Gameatn{{A: comm.ItemType, T: comm.Vouchers, N: conf.Amount / 100}} + + if errdata = this.module.ConsumeRes(session, need, true); errdata != nil { + return + } + if err = this.module.model.create(order); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Message: err.Error(), } } + if err = this.module.Rpc_ModulePayDelivery(context.Background(), &pb.HttpPayDeliveryReq{ Uid: session.GetUserId(), Orderid: order.Orderid, diff --git a/modules/robot/modulerobot_combat.go b/modules/robot/modulerobot_combat.go index 2facb075e..e7a4d93dc 100644 --- a/modules/robot/modulerobot_combat.go +++ b/modules/robot/modulerobot_combat.go @@ -41,7 +41,15 @@ func (this *ModuleRobot_Combat) OncePipeline(robot IRobot) (err error) { //机器人执行流 func (this *ModuleRobot_Combat) DoPipeline(robot IRobot) (err error) { - + var ( + errdata *pb.ErrorData + conf *cfg.GameCombatLevelData + ) + conf = this.GetCombatLevelData()[0] + if _, errdata = robot.SendMessage("combat", "in", &pb.CombatInReq{Level: conf.Id}); errdata != nil { + err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) + return + } return } diff --git a/modules/robot/modulerobot_practice.go b/modules/robot/modulerobot_practice.go index 512b5700f..dd9c607c9 100644 --- a/modules/robot/modulerobot_practice.go +++ b/modules/robot/modulerobot_practice.go @@ -30,24 +30,25 @@ func (this *ModuleRobot_Practice) Receive(robot IRobot, stype string, message pr return } func (this *ModuleRobot_Practice) OncePipeline(robot IRobot) (err error) { + if _, errdata := robot.SendMessage("practice", "info", &pb.PracticeInfoReq{}); errdata != nil { + err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) + return + } return } //机器人执行流 func (this *ModuleRobot_Practice) DoPipeline(robot IRobot) (err error) { - + if _, errdata := robot.SendMessage("practice", "info", &pb.PracticeInfoReq{}); errdata != nil { + err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) + return + } return } //做任务 func (this *ModuleRobot_Practice) DoTask(robot IRobot, taskconf *cfg.GameWorldTaskData, condconf *cfg.GameBuriedCondiData) (err error) { - var ( - errdata *pb.ErrorData - ) - if _, errdata = robot.SendMessage("practice", "info", &pb.PracticeInfoReq{}); errdata != nil { - err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) - return - } + switch comm.TaskType(condconf.Type) { case comm.Rtype149: for i := 1; i < 4; i++ { diff --git a/modules/robot/robot.go b/modules/robot/robot.go index 922b62aa5..d61fd1277 100644 --- a/modules/robot/robot.go +++ b/modules/robot/robot.go @@ -36,6 +36,8 @@ type Robot struct { cycle bool pipeline []*Pipeline //执行流水线 statistics []*RobotStatistics + receiveNum map[string]int32 //接收消息统计 + receiveSize map[string]int64 //接收消息统计 wtaskerror error awaitState int32 //状态 1没有等待 2等待回应 3关闭 } @@ -61,6 +63,8 @@ func (this *Robot) Init(addr string, client IClient) (err error) { this.await = make(chan *MessageResp) this.modules = make(map[core.M_Modules]IModuleRobot) this.statistics = make([]*RobotStatistics, 0, 100) + this.receiveNum = make(map[string]int32) + this.receiveSize = make(map[string]int64) this.modules[comm.ModuleUser] = new(ModuleRobot_User) this.modules[comm.ModuleSys] = new(ModuleRobot_Sys) this.modules[comm.ModuleGM] = new(ModuleRobot_GM) @@ -78,6 +82,7 @@ func (this *Robot) Init(addr string, client IClient) (err error) { this.modules[comm.ModuleCaravan] = new(ModuleRobot_Caravan) this.modules[comm.ModuleHoroscope] = new(ModuleRobot_Horoscope) this.modules[comm.ModuleCombat] = new(ModuleRobot_Combat) + this.modules[comm.ModuleSmithy] = new(ModuleRobot_Smithy) for _, v := range this.modules { v.Init() @@ -99,6 +104,8 @@ func (this *Robot) Receive(msg *pb.UserMessage) (err error) { if msgpath == "gateway.heartbeat" { //心跳 屏蔽掉 return } + this.receiveNum[msgpath]++ + this.receiveSize[msgpath] += int64(len(msg.Data.Value)) // log.Debug("[机器人 Resp]", log.Field{Key: "Account", Value: this.account}, log.Field{Key: "message", Value: msgpath}) //序列化用户消息对象 if message, err = msg.Data.UnmarshalNew(); err != nil { diff --git a/modules/robot/statisticalcomp.go b/modules/robot/statisticalcomp.go index 547820f90..952fb45bc 100644 --- a/modules/robot/statisticalcomp.go +++ b/modules/robot/statisticalcomp.go @@ -5,6 +5,7 @@ import ( "go_dreamfactory/lego/core" "go_dreamfactory/lego/core/cbase" "go_dreamfactory/lego/sys/log" + "go_dreamfactory/utils" "os" "sort" "sync" @@ -16,15 +17,17 @@ import ( */ type statisticalComp struct { cbase.ModuleCompBase - module *RobotModule - succclientNum int32 //链接成功客户端数 - failclientNum int32 //链接失败客户端数 - curonlineNum int32 //当前在线人数 - maxonlineNum int32 //最大在线人数 - robotdata map[string][]*RobotStatistics //机器人统计数据 - lock sync.RWMutex - start time.Time //开始时间 - end time.Time //结束时间 + module *RobotModule + succclientNum int32 //链接成功客户端数 + failclientNum int32 //链接失败客户端数 + curonlineNum int32 //当前在线人数 + maxonlineNum int32 //最大在线人数 + robotdata map[string][]*RobotStatistics //机器人统计数据 + receiveMsg map[string]int32 //接收消息数统计 + receiveMsgSize map[string]int64 //接收消息数统计 + lock sync.RWMutex + start time.Time //开始时间 + end time.Time //结束时间 } //组件初始化接口 @@ -32,6 +35,8 @@ func (this *statisticalComp) Init(service core.IService, module core.IModule, co this.ModuleCompBase.Init(service, module, comp, options) this.module = module.(*RobotModule) this.robotdata = make(map[string][]*RobotStatistics) + this.receiveMsg = make(map[string]int32) + this.receiveMsgSize = make(map[string]int64) return } @@ -61,6 +66,12 @@ func (this *statisticalComp) AddFailClient(robot *Robot, err error) { func (this *statisticalComp) RobotFinishedTest(robot *Robot) { this.lock.Lock() this.robotdata[robot.Account()] = robot.statistics + for k, v := range robot.receiveNum { + this.receiveMsg[k] += v + } + for k, v := range robot.receiveSize { + this.receiveMsgSize[k] += v + } this.curonlineNum-- this.succclientNum++ this.end = time.Now() @@ -70,11 +81,15 @@ func (this *statisticalComp) RobotFinishedTest(robot *Robot) { //输出报表 func (this *statisticalComp) OutReport() { var ( - messages map[string][]int64 = make(map[string][]int64) - totalmessage int32 - ok bool + messages map[string][]int64 = make(map[string][]int64) + receive map[string]int32 = make(map[string]int32) + receiveSize map[string]int64 = make(map[string]int64) + totalmessage int32 + totalreceive int32 + totalreceiveSize int64 + ok bool ) - this.lock.Lock() + this.lock.RLock() for _, datas := range this.robotdata { for _, v := range datas { totalmessage++ @@ -84,7 +99,15 @@ func (this *statisticalComp) OutReport() { messages[v.message] = append(messages[v.message], v.time) } } - this.lock.Unlock() + for k, v := range this.receiveMsg { + receive[k] = v + totalreceive += v + } + for k, v := range this.receiveMsgSize { + receiveSize[k] = v + totalreceiveSize += v + } + this.lock.RUnlock() file, err := os.OpenFile(this.module.options.OutFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { fmt.Println(err) @@ -97,6 +120,7 @@ func (this *statisticalComp) OutReport() { file.WriteString(fmt.Sprintf("最大同时在线人数: %d\n", this.maxonlineNum)) file.WriteString(fmt.Sprintf("压测执行时长: %.2f秒\n", this.end.Sub(this.start).Seconds())) file.WriteString(fmt.Sprintf("消息总请求数: %d\n", totalmessage)) + file.WriteString(fmt.Sprintf("消息总接收数: %d 接收消息总大小:%s\n", totalreceive, utils.FormatByesSize(totalreceiveSize))) file.WriteString(fmt.Sprintf("QPS: %.2f\n", float64(totalmessage)/this.end.Sub(this.start).Seconds())) file.WriteString("---消息压测详情----------------------------------------------------------------------------------------------------\n") for message, data := range messages { @@ -118,6 +142,10 @@ func (this *statisticalComp) OutReport() { } file.WriteString(fmt.Sprintf("消息名:%-20s 请求次数:%-5d 耗时最小:%-5dms 耗时最大:%-5dms 平均耗时:%-5.2fms 中位耗时:%-5.2fms \n", message, len(data), min, max, avg, median)) } + file.WriteString("---消息压测接收回包消息统计----------------------------------------------------------------------------------------------------\n") + for k, v := range receive { + file.WriteString(fmt.Sprintf("消息名:%-20s 请求次数:%-5d 请求总大小:%s\n", k, v, utils.FormatByesSize(receiveSize[k]))) + } } func (this *statisticalComp) run() { diff --git a/modules/user/module.go b/modules/user/module.go index e06dc3b62..955efb303 100644 --- a/modules/user/module.go +++ b/modules/user/module.go @@ -396,6 +396,71 @@ func (this *User) QueryAttributeValue(uid string, attr string) (value int64) { return } +func (this *User) QueryAttributeValues(uid string, attr map[string]int64) { + var ( + user *pb.DBUser + userEx *pb.DBUserExpand + err error + ) + + user, err = this.modelUser.GetUser(uid) + if err != nil { + return + } + userEx, err = this.GetUserExpand(uid) + if err != nil { + return + } + + if user == nil || userEx == nil { + return + } + for k, _ := range attr { + switch k { + case comm.ResGold: + attr[k] = user.Gold + case comm.ResExp: + attr[k] = user.Exp + case comm.VipExp: + attr[k] = user.Vipexp + case comm.StarCoin: + attr[k] = user.Starcoin + case comm.ResDiamond: + attr[k] = user.Diamond + case comm.ResPs: + attr[k] = int64(user.Ps) + case comm.SociatyCoin: + attr[k] = int64(userEx.Guildcoin) + case comm.ArenaCoin: + attr[k] = int64(userEx.Arenacoin) + case comm.ResFriend: + attr[k] = int64(userEx.FriendPoint) + case comm.Moongold: + attr[k] = int64(user.Moongold) + case comm.Talent1: + attr[k] = int64(user.Talent1) + case comm.Talent2: + attr[k] = int64(user.Talent2) + case comm.Talent3: + attr[k] = int64(user.Talent3) + case comm.Talent4: + attr[k] = int64(user.Talent4) + case comm.Merchantmoney: + attr[k] = int64(user.Merchantmoney) + case comm.Integral: + attr[k] = int64(user.Integral) + case comm.Profit: + attr[k] = int64(user.Profit) + case comm.Consumeexp: + attr[k] = int64(user.Consumeexp) + case comm.Consumemoney: + attr[k] = int64(user.Consumemoney) + } + } + + return +} + func (this *User) change(session comm.IUserSession, attrs map[string]int32) (atno []*pb.UserAtno, change *pb.UserResChangedPush, errdata *pb.ErrorData) { uid := session.GetUserId() var ( diff --git a/utils/strings.go b/utils/strings.go index 5466352fe..06af0493f 100644 --- a/utils/strings.go +++ b/utils/strings.go @@ -1,6 +1,7 @@ package utils import ( + "fmt" "strconv" "strings" @@ -95,3 +96,21 @@ func ToInt64(s string) int64 { } return 0 } + +// 字节的单位转换 保留两位小数 +func FormatByesSize(fileSize int64) (size string) { + if fileSize < 1024 { + //return strconv.FormatInt(fileSize, 10) + "B" + return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1)) + } else if fileSize < (1024 * 1024) { + return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024)) + } else if fileSize < (1024 * 1024 * 1024) { + return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024)) + } else if fileSize < (1024 * 1024 * 1024 * 1024) { + return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024)) + } else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) { + return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024)) + } else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024) + return fmt.Sprintf("%.2fPB", float64(fileSize)/float64(1024*1024*1024*1024*1024)) + } +}