You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
4.7 KiB
190 lines
4.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Sog;
|
|
using Sog.Log;
|
|
namespace Mail
|
|
{
|
|
|
|
/// <summary>
|
|
/// 服务器统计类
|
|
/// </summary>
|
|
public class MessageTaskStat
|
|
{
|
|
|
|
//每个消息发送,接收信息
|
|
private SortedDictionary<int, NetMsgStat> m_statNetMsgMap;
|
|
|
|
private int TotalSendNum;
|
|
private int TotalRecvNum;
|
|
private long TotalSendLength;
|
|
private long TotalRecvLength;
|
|
|
|
private long m_lastLogTime;
|
|
private int m_logInterval;
|
|
|
|
//是否全部消息统计
|
|
private bool m_isAllMsgStat;
|
|
private HashSet<int> m_needStatMsgs;
|
|
|
|
private object m_threadLock = new object();
|
|
|
|
public MessageTaskStat()
|
|
{
|
|
|
|
//缺省60秒log一次
|
|
m_logInterval = 60 * 1000;
|
|
|
|
|
|
m_needStatMsgs = new HashSet<int>();
|
|
m_statNetMsgMap = new SortedDictionary<int, NetMsgStat>();
|
|
}
|
|
|
|
public bool IsNeedStat(int iMsgID)
|
|
{
|
|
if(m_isAllMsgStat)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return m_needStatMsgs.Contains(iMsgID);
|
|
}
|
|
|
|
public void AddNeedMsg(int iMsgID)
|
|
{
|
|
if(!m_needStatMsgs.Contains(iMsgID))
|
|
{
|
|
m_needStatMsgs.Add(iMsgID);
|
|
}
|
|
}
|
|
|
|
public void ResetNeedMsgList(bool isAllMsgStat)
|
|
{
|
|
m_needStatMsgs.Clear();
|
|
m_isAllMsgStat = isAllMsgStat;
|
|
}
|
|
|
|
public void Tick(long tNowMs)
|
|
{
|
|
if (m_lastLogTime == 0)
|
|
{
|
|
m_lastLogTime = tNowMs;
|
|
}
|
|
|
|
if (tNowMs < m_lastLogTime + m_logInterval)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_lastLogTime = tNowMs;
|
|
|
|
LogHead();
|
|
|
|
LogContent();
|
|
|
|
LogTail();
|
|
|
|
|
|
}
|
|
|
|
private void LogContent_imp()
|
|
{
|
|
LogNetMsg();
|
|
}
|
|
|
|
private void LogContent()
|
|
{
|
|
lock (m_threadLock)
|
|
{
|
|
LogContent_imp();
|
|
}
|
|
}
|
|
|
|
//搞个头,看起来方便点
|
|
private void LogHead()
|
|
{
|
|
TraceLog.Stat("MessageTaskStat begin =================================================================");
|
|
}
|
|
|
|
private void LogTail()
|
|
{
|
|
TraceLog.Stat("MessageTaskStat end.\n");
|
|
}
|
|
|
|
//如果不存在则创建
|
|
private NetMsgStat GetNetMsgStatByID(int iMsgID)
|
|
{
|
|
if (!m_statNetMsgMap.ContainsKey(iMsgID))
|
|
{
|
|
NetMsgStat stat = new NetMsgStat();
|
|
stat.MsgID = iMsgID;
|
|
|
|
m_statNetMsgMap.Add(iMsgID, stat);
|
|
}
|
|
|
|
return m_statNetMsgMap[iMsgID];
|
|
}
|
|
|
|
|
|
public void OnNetRecv(int iMsgID, int iDataLength, long time)
|
|
{
|
|
lock (m_threadLock)
|
|
{
|
|
NetMsgStat stat = GetNetMsgStatByID(iMsgID);
|
|
|
|
stat.RecvLength += iDataLength;
|
|
stat.RecvNum += 1;
|
|
stat.RecvTotalTime += time;
|
|
stat.RecvMaxTime = stat.RecvMaxTime < time ? time : stat.RecvMaxTime;
|
|
if (stat.RecvNum == 1) //是第一次
|
|
{
|
|
stat.RecvMinTime = time;
|
|
|
|
}
|
|
else
|
|
{
|
|
stat.RecvMinTime = stat.RecvMinTime > time ? time : stat.RecvMinTime;
|
|
}
|
|
|
|
|
|
TotalRecvLength += iDataLength;
|
|
TotalRecvNum += 1;
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//网络消息,接受,发送数量和长度
|
|
private void LogNetMsg()
|
|
{
|
|
TraceLog.Stat("TotalSendNum:{0,-10} TotalRecvNum:{1,-10} TotalSendLength:{2,-12} TotalRecvLength:{3,-12}",
|
|
TotalSendNum, TotalRecvNum, TotalSendLength, TotalRecvLength);
|
|
|
|
//清空
|
|
TotalRecvLength = 0;
|
|
TotalRecvNum = 0;
|
|
TotalSendLength = 0;
|
|
TotalSendNum = 0;
|
|
|
|
TraceLog.Stat("MsgID SendNum RecvNum SendLength RecvLength RecvTotalTime RecvMaxTime RecvMinTime RecvAvgTime");
|
|
|
|
foreach (var item in m_statNetMsgMap.Values)
|
|
{
|
|
long RecvAvgTime = item.RecvNum <= 0 ? 0 : item.RecvTotalTime / item.RecvNum;
|
|
TraceLog.Stat("{0,-10}{1,-10}{2,-10}{3,-15}{4,-15}{5,-18}{6,-16}{7,-16}{8,-16}",
|
|
item.MsgID, item.SendNum, item.RecvNum, item.SendLength, item.RecvLength, item.RecvTotalTime, item.RecvMaxTime, item.RecvMinTime, RecvAvgTime);
|
|
|
|
//log完后清空这项数值
|
|
item.Clear();
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|