优化战斗代码

This commit is contained in:
liwei1dao 2024-03-01 17:53:00 +08:00
parent 2aa07e9c66
commit d3c73f0dd5
6 changed files with 36 additions and 25 deletions

View File

@ -1,9 +1,9 @@
并发数: 1000
消息总请求数: 100000
回合数: 4
战报InfoSize: 3.36KB
指令数: 214
消息总请求大小: 26.87KB
压测执行时长: 57.27
并发数: 2000
消息总请求数: 200000
回合数: 38
战报InfoSize: 1.72KB
指令数: 936
消息总请求大小: 48.59KB
压测执行时长: 103.22
0-5ms------------------5-10ms -------------------10-20ms -------------------------20-50ms-------------------->50ms
34859次 ------------------32413次 -------------------31566次 -------------------------1147 次-------------------->15
166409次 ------------------26867次 -------------------6120 次 -------------------------604 次-------------------->0

View File

@ -13,13 +13,13 @@ import (
"google.golang.org/protobuf/types/known/anypb"
)
func newClient(addr string, mgr IClientMgr, log log.ILogger) (c *client, err error) {
func newClient(i int32, addr string, mgr IClientMgr, log log.ILogger) (c *client, err error) {
msg := &pb.BattleRpcMessage{
Rid: 0,
Method: "Heartbeat",
}
data, _ := proto.Marshal(msg)
c = &client{addr: addr, mgr: mgr, log: log, seq: 1, state: 1, pending: make(map[uint64]*MessageCall), heartbeatpack: data}
c = &client{i: i, addr: addr, mgr: mgr, log: log, seq: 1, state: 1, pending: make(map[uint64]*MessageCall), heartbeatpack: data}
dialer := websocket.Dialer{}
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
if c.conn, _, err = dialer.DialContext(ctx, addr, nil); err != nil {
@ -31,6 +31,7 @@ func newClient(addr string, mgr IClientMgr, log log.ILogger) (c *client, err err
}
type client struct {
i int32
addr string
mgr IClientMgr
log log.ILogger

View File

@ -19,8 +19,9 @@ type battleClientMgrComp struct {
service core.IService
module *BattleTest
mutexs []sync.Mutex
clinet *client
i int
clinet []*client
lock sync.Mutex
index int32
}
//组件初始化接口
@ -34,38 +35,46 @@ func (this *battleClientMgrComp) Init(service core.IService, module core.IModule
func (this *battleClientMgrComp) Start() (err error) {
err = this.ModuleCompBase.Start()
this.clinet = make([]*client, this.options.ClientNum)
if this.options.OpenCheck {
if this.clinet, err = newClient(this.options.BattleServerAddr, this, this.options.Log); err != nil {
for i := int32(0); i < (this.options.ClientNum); i++ {
if this.clinet[i], err = newClient(i, this.options.BattleServerAddr, this, this.options.Log); err != nil {
return
}
}
}
return
}
//关闭客户端连接对象
func (this *battleClientMgrComp) Shutdown(c *client) {
this.clinet = nil
this.clinet[c.i] = nil
log.Errorf("战斗校验服务被关闭!")
}
func (this *battleClientMgrComp) reconnect() (err error) {
if this.clinet != nil {
func (this *battleClientMgrComp) getclient() (_client *client, err error) {
i := this.index % this.options.ClientNum
_client = this.clinet[i]
if _client != nil {
this.index++
return
}
if this.clinet, err = newClient(this.options.BattleServerAddr, this, this.options.Log); err != nil {
if _client, err = newClient(i, this.options.BattleServerAddr, this, this.options.Log); err != nil {
log.Errorln(err)
return
}
this.clinet[i] = _client
return
}
//校验战斗结果
func (this *battleClientMgrComp) CheckBattle(ctx context.Context, req *pb.BattleReport) (reply *pb.BattleCheckResults, err error) {
var client *client
reply = &pb.BattleCheckResults{}
if err = this.reconnect(); err != nil {
if client, err = this.getclient(); err != nil {
return
}
if err = this.clinet.callBattle(ctx, "Check", req, reply); err != nil {
if err = client.callBattle(ctx, "Check", req, reply); err != nil {
log.Errorln(err)
}
return

View File

@ -8,6 +8,7 @@ import (
type (
Options struct {
modules.Options
ClientNum int32
OpenCheck bool
BattleServerAddr string
OutFilePath string

View File

@ -65,10 +65,10 @@ func (this *pressuretestComp) Start() (err error) {
this.round = int64(this.report.Round)
datas, _ := proto.Marshal(this.report.Info)
this.battleinfosize = int64(len(datas))
// for _, v := range this.report.Outcmd {
// datas, _ := proto.Marshal(v)
// log.Debugf("指令:%s大小 %s:", v.Cmdtype, utils.FormatByesSize(int64(len(datas))))
// }
for _, v := range this.report.Outcmd {
datas, _ := proto.Marshal(v)
log.Debugf("指令:%s大小 %s:", v.Cmdtype, utils.FormatByesSize(int64(len(datas))))
}
this.cmdnum = int64(len(this.report.Outcmd))
this.workers = make([]*worker, this.module.options.Workers)
this.start = time.Now()

View File

@ -24,7 +24,7 @@ func (this *worker) Worker(wg *sync.WaitGroup) {
)
locp:
for {
time.Sleep(time.Millisecond * time.Duration(10+rand.Int31n(900)))
time.Sleep(time.Millisecond * time.Duration(100+rand.Int31n(900)))
stime = time.Now()
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
this.module.clientmgr.CheckBattle(ctx, this.report)