package main

import (
	"fmt"
	"runtime"
	"server/game"
	"server/game/mod/msg"
	"testing"
	"time"
)

//	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)
//			}
//		}
//	}
func BenchmarkGame(b *testing.B) {
	runtime.GOMAXPROCS(2)
	fmt.Print("BenchmarkGame")
	f := "wmz00%d"
	for i := 0; i < 10000; i++ {
		go func() {
			UserName := fmt.Sprintf(f, i)
			p1 := new(game.Player)
			err := p1.InitPlayer(UserName)
			if err != nil {
				fmt.Println(err)
			}
			game.G_GameLogicPtr.SetPlayer(p1)
			for {
				time.Sleep(1 * time.Second)
				game.Benchmark(p1)
			}
		}()
	}
	go func() {
		time.Sleep(20 * time.Second)
		game.G_GameLogicPtr.ChampshipMgrSend(&msg.Msg{
			Type: msg.HANDLE_TYPE_CHAMPSHIP_GROUP,
		})
	}()
	for {
		time.Sleep(1 * time.Second)
		printMemUsage()
	}
}
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
}
