154 lines
3.2 KiB
Go

package main
import (
_ "context"
"encoding/base64"
_ "github.com/olivere/elastic/v7"
_ "github.com/olivere/elastic/v7/config"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"os"
"rocommon/service"
_ "rocommon/socket"
_ "rocommon/socket/http"
_ "rocommon/socket/tcp"
_ "rocommon/socket/websocket"
"rocommon/util"
"roserver/baseserver/model"
"roserver/baseserver/set"
testModel "roserver/test/model"
"runtime"
"strings"
"time"
)
type IBase interface {
SetVal()
GetBase() *abase
}
type abase struct {
aabase int
}
type a1 struct {
abase
aaa int
}
func (this *a1) SetVal() {
this.aabase = 1
}
func (this *a1) GetBase() *abase {
return &this.abase
}
func main() {
err := util.InitLog(1, 0, "./log", "bc", "")
runtime.GOMAXPROCS(runtime.NumCPU())
err = service.ServerCmd.Parse(os.Args[1:])
if err != nil {
util.InfoF("err=%v", err)
}
if *service.MaxOnlineNumParam > 0 {
testModel.MaxConnNum = *service.MaxOnlineNumParam
}
if *service.IPParam != "" {
testModel.TmpIpStr = *service.IPParam
}
log.Printf("ip=%v", testModel.TmpIpStr)
//读取机器人压测数据文件
TestRobotBenchmark(&testModel.BenchmarkMsgDataList)
util.InfoF("---------------------------LOGIN_BEGIN----------------------------")
//根据运行参数判断是否开启pprof,
if *service.Pprof != "" {
go http.ListenAndServe(*service.Pprof, http.DefaultServeMux)
}
go testModel.RobotMgr.WatchBenchmark()
count := 0
tick := time.NewTicker(time.Duration(1000 / *service.LoginFreq) * time.Millisecond)
for {
select {
case <-tick.C:
go testModel.NewRobotClient(*service.BeginIndexParam + count)
count++
}
if count >= *service.MaxOnlineNumParam {
break
}
}
/*for count < *service.MaxOnlineNumParam {
loginTime := util.GetCurrentTime()
for i := 0; i < *service.LoginFreq; i++ {
count++
go testModel.NewRobotClient(*service.BeginIndexParam + count)
}
diffTime := util.GetCurrentTime() - loginTime
if diffTime < 1000 {
time.Sleep(time.Millisecond * time.Duration(1000-diffTime))
}
}
*/
service.WaitExitSignal()
}
func TestRobotBenchmark(msgDtaList *[]*testModel.RobotBenchmarkSt) {
f, err := os.Open("./game.log")
if err != nil {
return
}
defer f.Close()
fd, err := ioutil.ReadAll(f)
if err != nil {
return
}
fileStr := string(fd)
uniMsgId := set.New(set.NonThreadSafe)
fileStrList := strings.Split(fileStr, "\n")
for idx := 0; idx < len(fileStrList); idx++ {
tmpStr := fileStrList[idx]
if !strings.Contains(tmpStr, "receivemsg") {
continue
}
tmpStrList := strings.Split(tmpStr, " ")
msgIdStr := getTextData(tmpStrList[5])
msgId, _ := model.Str2Num(msgIdStr)
msgDataStr := getTextData(tmpStrList[6])
msgData, _ := base64.StdEncoding.DecodeString(msgDataStr)
msgTimeStr := getTextData(tmpStrList[7])
msgTime, _ := model.Str2NumU64(msgTimeStr)
*msgDtaList = append(*msgDtaList, &testModel.RobotBenchmarkSt{
MsgTime: msgTime,
MsgId: uint32(msgId),
MsgData: msgData,
})
uniMsgId.Add(msgId)
}
//for _, data := range uniMsgId.List() {
// util.InfoF("receivemsg sendmsgid=%v", data.(int)+1)
//}
}
func getTextData(str string) string {
retStr := ""
findIdx := strings.Index(str, "=")
for idx := findIdx + 1; idx < len(str); idx++ {
retStr += string(str[idx])
}
return retStr
}