using CommonAI.Zone;
using CommonLang;
using CommonLang.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;

namespace CommonAI.ZoneServer.JSGModule
{
	public class JSGServerProfile
	{
		public class PackSizeProfile
		{
			public long times = 0;
			public long size = 0;
		}

		protected static readonly Logger log = LoggerFactory.GetDefLogger();

		//////////////////////////////////////////////////////////////////////////////////
		//数据统计		
		private static HashMap<int, PackSizeProfile> mSendProfile = new HashMap<int, PackSizeProfile>();
		private static long mLastShowSendTime;
		private static HashMap<int, PackSizeProfile> mRecProfile = new HashMap<int, PackSizeProfile>();
		private static long mLastShowRecTime;
		//////////////////////////////////////////////////////////////////////////////////

		public static void RecordSend(int typeID, long msgSize)
		{
			PackSizeProfile profile = mSendProfile.Get(typeID);
			if (profile == null)
			{
				profile = new PackSizeProfile();
				mSendProfile.Put(typeID, profile);
			}
			profile.times++;
			profile.size += msgSize;
		}

		public static void CheckPrintSendProfile()
		{
			long curTime = CommonLang.CUtils.localTimeMS;
			if (mLastShowSendTime == 0 || mLastShowSendTime - curTime > 360000)
			{
				mLastShowSendTime = curTime;
				log.Warn(" --------------------- PackEvent send ------------------- ");
				foreach (KeyValuePair<int, PackSizeProfile> kv in mSendProfile)
				{
					log.Warn(" - - - - - MsgID = " + kv.Key + ",\t Size = " + kv.Value.size +
						", \ttimes = " + kv.Value.times + ", \t avg = " + (kv.Value.size / kv.Value.times));
				}
				log.Warn(" ---------------------------------------------------- ");
			}
		}



		public static void RecordRecv(int typeID, long msgSize)
		{
			PackSizeProfile profile = mRecProfile.Get(typeID);
			if (profile == null)
			{
				profile = new PackSizeProfile();
				mRecProfile.Put(typeID, profile);
			}
			profile.times++;
			profile.size += msgSize;

			CheckPrintRecProfile();
		}

		private static void CheckPrintRecProfile()
		{
			long curTime = CommonLang.CUtils.localTimeMS;
			if (mLastShowRecTime == 0 || mLastShowRecTime - curTime > 360000)
			{
				mLastShowRecTime = curTime;
				log.Warn(" --------------------- PackEvent recv ------------------- ");
				foreach (KeyValuePair<int, PackSizeProfile> kv in mRecProfile)
				{
					log.Warn(" - - - - - MsgID = " + kv.Key + ",\t Size = " + kv.Value.size +
						", \ttimes = " + kv.Value.times + ", \t avg = " + (kv.Value.size / kv.Value.times));
				}
				log.Warn(" ---------------------------------------------------- ");
			}
		}
	}
}