| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package battle
- import (
- "f1-game/internal/code"
- "f1-game/internal/pb"
- "os"
- "testing"
- "time"
- cherryFile "github.com/cherry-game/cherry/extend/file"
- clog "github.com/cherry-game/cherry/logger"
- cprofile "github.com/cherry-game/cherry/profile"
- "github.com/nats-io/nats.go"
- "google.golang.org/protobuf/proto"
- )
- func initBattleParanReqData() []byte {
- path := "../../etc/profile/dhc.json"
- cprofile.Init(path, "4")
- config := cprofile.GetConfig("battle_record")
- if config.LastError() != nil {
- clog.Warnf("[TestBattle100Calls] cprofile property not exists.")
- return nil
- }
- fileName, _ := cherryFile.JoinPath(cprofile.Path(), config.GetConfig("file").GetString("succeedPathKey"), "dev_pve_1050471_1011_20251112193523.bin")
- reqData, err := os.ReadFile(fileName)
- if err != nil {
- clog.Debugf("[BattleValidation] readReqData read latest file error (may not exist): %v", err)
- return nil
- }
- return reqData
- }
- func TestBattle(t *testing.T) {
- reqData := initBattleParanReqData()
- if reqData == nil {
- clog.Debugf("[BattleValidation] readReqData is nil")
- return
- }
- conn := getConnect()
- defer conn.Close()
- BattleValidation(conn, reqData)
- }
- func getConnect() *nats.Conn {
- conn, err := nats.Connect("nats://192.168.1.232:4222,nats://192.168.1.232:4223,nats://192.168.1.232:4224")
- if err != nil {
- clog.Warnf("[BattleValidation] nats connect error. err = %v", err)
- return nil
- }
- return conn
- }
- func BattleValidation(conn *nats.Conn, reqData []byte) bool {
- rsp, err := conn.Request(BATTLE_VERIFY_SUBJECT, reqData, 5*time.Second)
- if err != nil {
- clog.Warnf("[BattleValidation] Request replyData error. err = %v", err)
- return false
- }
- reply := &pb.BattleReplay{}
- err = proto.Unmarshal(rsp.Data, reply)
- if err != nil {
- clog.Warnf("[BattleValidation] Unmarshal replyData error. err = %v", err)
- return false
- }
- if code.IsFail(reply.ErrorCode) {
- clog.Warnf("[BattleValidation] battle fail. errorCode = %v, errorText = %v", reply.ErrorCode, reply.ErrorText)
- return false
- }
- // clog.Debugf("[BattleValidation] result = %v", reply)
- return true
- }
- func TestBattle100Calls(t *testing.T) {
- reqData := initBattleParanReqData()
- if reqData == nil {
- clog.Debugf("[TestBattle100Calls] readReqData is nil")
- return
- }
- conn := getConnect()
- defer conn.Close()
- const iterations = 1000
- var total time.Duration
- min := time.Duration(1<<63 - 1)
- var max time.Duration
- var success int
- for i := 1; i <= iterations; i++ {
- start := time.Now()
- result := BattleValidation(conn, reqData)
- dur := time.Since(start)
- total += dur
- if dur < min {
- min = dur
- }
- if dur > max {
- max = dur
- }
- if result {
- success++
- }
- clog.Debugf("[TestBattle100Calls] iter=%d duration=%v success=%v", i, dur, result)
- }
- avg := time.Duration(int64(total) / int64(iterations))
- clog.Debugf("[TestBattle100Calls] total=%v avg=%v min=%v max=%v success=%d/%d", total, avg, min, max, success, iterations)
- }
|