package main import ( "fmt" "runtime" "server/game" "server/game/mod/msg" "testing" ) // func TestBenchInit(t *testing.T) { // f := "wmz00%d" // for i := 0; i < 10000; i++ { // UserName := fmt.Sprintf(f, i) // game.G_GameLogicPtr.Db_AccountInfo.UserName = UserName // game.G_GameLogicPtr.Db_AccountInfo.UserPassword = "123456" // if game.G_GameLogicPtr.NewAccountInsertDataToDB() { // fmt.Printf("UserName:%s\n init success", UserName) // } // } // } /* * cluster 消息处理基准测试 36716 34961 ns/op 1690 B/op 38 allocs/op */ func BenchmarkClusterMsg(b *testing.B) { game.ClusterMgrInit() runtime.GOMAXPROCS(8) game.G_getGameLogic() for i := 0; i < b.N; i++ { m := &msg.Msg{ HandleType: msg.HANDLE_MOD_PLAYER_LOGIN, Extra: 0, } game.MessageHandle(m) } } func BenchmarkLog(b *testing.B) { runtime.GOMAXPROCS(2) t := game.G_getGameLogic() for i := 0; i < 10000; i++ { t.AddLog(&game.Log{ EventName: "test_log", Param: map[string]interface{}{ "test_key": "test_value", }, }) } t.MLogManager.Close() } func BenchmarkLog2(b *testing.B) { // 准备耗时但不需要计入基准的初始化 runtime.GOMAXPROCS(2) // 假设有个函数 NewGameLogicForBenchmark() 创建全新、独立的对象 b.ReportAllocs() for i := 0; i < b.N; i++ { // 每次迭代创建独立对象,避免副作用 t := game.G_getGameLogic() b.StartTimer() // 被测操作:向日志管理器添加 1000 条日志 for j := 0; j < 1000; j++ { t.AddLog(&game.Log{ EventName: "test_log", Param: map[string]interface{}{ "test_key": "test_value", }, }) } b.StopTimer() t.MLogManager.Close() } } func printMemUsage() { var m runtime.MemStats runtime.ReadMemStats(&m) // 输出内存使用情况 fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc)) fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc)) fmt.Printf("\tSys = %v MiB", bToMb(m.Sys)) fmt.Printf("\tNumGC = %v\n", m.NumGC) var playerCount int game.G_GameLogicPtr.M_Players.Range(func(key, value interface{}) bool { playerCount++ return true }) fmt.Printf("\tPlayerNum = %v\n", playerCount) } func bToMb(b uint64) uint64 { return b / 1024 / 1024 }