package gameLogMgr

import (
	"bytes"
	"fmt"
	"testing"
	"time"

	"goutil/debugUtil"
	"goutil/stringUtil"
	"goutil/timeUtil"
)

func TestWrite(t *testing.T) {
	debugUtil.SetDebug(true)

	brokerList := []string{"10.1.0.202:9092", "10.1.0.204:9092", "10.1.0.205:9092"}
	Start(brokerList, "", "")

	topic := "test2"
	serverGroupId := int32(20011)
	for i := 0; i < 5; i++ {
		Write(topic, serverGroupId, getGameLog(i))
	}

	time.Sleep(5 * time.Second)
	Stop()
}

func BenchmarkWrite(b *testing.B) {
	debugUtil.SetDebug(true)
	topic := "test2"
	serverGroupId := int32(20011)

	brokerList := []string{"10.1.0.202:9092", "10.1.0.204:9092", "10.1.0.205:9092"}
	Start(brokerList, "", "")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Write(topic, serverGroupId, getGameLog(i))
	}
	b.StopTimer()

	Stop()
}

func getGameLog(int2 int) string {
	//kafkaLog组装
	var buffer bytes.Buffer
	buffer.WriteString("{")
	buffer.WriteString(fmt.Sprintf("\"#account_id\":\"%s\"", "123456789123456789"))
	buffer.WriteString(",")
	buffer.WriteString(fmt.Sprintf("\"#time\":\"%s\"", time.Now().Format("2006-01-02 15:04:05")))
	buffer.WriteString(",")
	buffer.WriteString(fmt.Sprintf("\"#uuid\":\"%s\"", stringUtil.GetNewGUID()))
	buffer.WriteString(",")
	buffer.WriteString(fmt.Sprintf("\"#event_id\":\"\""))
	buffer.WriteString(",")
	buffer.WriteString(fmt.Sprintf("\"#type\":\"track\""))
	buffer.WriteString(",")
	buffer.WriteString(fmt.Sprintf("\"#event_name\":\"achievement_change_log\""))
	buffer.WriteString(",")
	buffer.WriteString(fmt.Sprintf("\"properties\":{\"PartnerId\":%d,\"ServerId\":%d,\"Crtime\":\"%s\",\"Crdate\":\"%s\"}", 600021, int2, timeUtil.ToDateTimeString2(time.Now()), timeUtil.ToDateString2(time.Now())))
	buffer.WriteString("}")

	return buffer.String()
}
