JSGServerProfile.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using CommonAI.Zone;
  2. using CommonLang;
  3. using CommonLang.Log;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. //using System.Threading.Tasks;
  9. namespace CommonAI.ZoneServer.JSGModule
  10. {
  11. public class JSGServerProfile
  12. {
  13. public class JSGProfileData
  14. {
  15. public long times = 0;
  16. public long size = 0;
  17. }
  18. public class ZoneCreateInfo
  19. {
  20. public int areaId;
  21. public int mTotalTimes;
  22. public int mTotalTimeUse;
  23. }
  24. public class TriggerData
  25. {
  26. public long checkTimes = 0; // 检测次数
  27. public long triggerTimes = 0; // 触发次数
  28. public long timeUse = 0; // 触发耗时
  29. }
  30. public class JSGTriggerProfile
  31. {
  32. // 总触发次数<100 || 总耗时小于<3000 将会被过滤输出
  33. public int areaId;
  34. public TriggerData total = new TriggerData();
  35. public HashMap<string, TriggerData> profileData = new HashMap<string, TriggerData>();
  36. public JSGTriggerProfile(int areaId)
  37. {
  38. this.areaId = areaId;
  39. }
  40. }
  41. protected static readonly Logger log = LoggerFactory.GetDefLogger();
  42. private static readonly int STRIGGER_INTERVAL = 600 * 1000;
  43. //////////////////////////////////////////////////////////////////////////////////
  44. //数据统计
  45. private static HashMap<int, JSGProfileData> mSendProfile = new HashMap<int, JSGProfileData>();
  46. private static HashMap<int, JSGProfileData> mRecProfile = new HashMap<int, JSGProfileData>();
  47. //记录创建场景信息
  48. private static HashMap<int, ZoneCreateInfo> mZoneCreate = new HashMap<int, ZoneCreateInfo>();
  49. //场景触发器触发次数,耗时统计(场景id: 时间名,统计数据)
  50. private static HashMap<int, JSGTriggerProfile> mZoneTriggers = new HashMap<int, JSGTriggerProfile>();
  51. private static long mNextPrintTime;
  52. //////////////////////////////////////////////////////////////////////////////////
  53. public static void init()
  54. {
  55. mNextPrintTime = CommonLang.CUtils.CurrentTimeMS + STRIGGER_INTERVAL;
  56. }
  57. public static void RecordSend(int typeID, long msgSize)
  58. {
  59. JSGProfileData profile = mSendProfile.Get(typeID);
  60. if (profile == null)
  61. {
  62. profile = new JSGProfileData();
  63. mSendProfile.Put(typeID, profile);
  64. }
  65. profile.times++;
  66. profile.size += msgSize;
  67. }
  68. public static void RecordRecv(int typeID, long msgSize)
  69. {
  70. //log.Warn("收到消息:" + typeID);
  71. JSGProfileData profile = mRecProfile.Get(typeID);
  72. if (profile == null)
  73. {
  74. profile = new JSGProfileData();
  75. mRecProfile.Put(typeID, profile);
  76. }
  77. profile.times++;
  78. profile.size += msgSize;
  79. }
  80. /** 记录场景创建耗时 */
  81. public static void RecordZoneCreate(int areaId, int timeUse)
  82. {
  83. ZoneCreateInfo zoneInfo = mZoneCreate.Get(areaId);
  84. if (zoneInfo == null)
  85. {
  86. zoneInfo = new ZoneCreateInfo();
  87. zoneInfo.areaId = areaId;
  88. mZoneCreate.Put(areaId, zoneInfo);
  89. }
  90. zoneInfo.mTotalTimes++;
  91. zoneInfo.mTotalTimeUse += timeUse;
  92. }
  93. /** 记录场景触发器耗时 */
  94. public static void RecordTrigger(int areaId, string triggerName, int timeUse, bool isTrigger)
  95. {
  96. JSGTriggerProfile trigger = mZoneTriggers.Get(areaId);
  97. if (trigger == null)
  98. {
  99. trigger = new JSGTriggerProfile(areaId);
  100. mZoneTriggers.Put(areaId, trigger);
  101. }
  102. trigger.total.timeUse += timeUse;
  103. trigger.total.checkTimes++;
  104. TriggerData triggerData = trigger.profileData.Get(triggerName);
  105. if (triggerData == null)
  106. {
  107. triggerData = new TriggerData();
  108. trigger.profileData.Put(triggerName, triggerData);
  109. }
  110. triggerData.checkTimes++;
  111. triggerData.timeUse += timeUse;
  112. if (isTrigger)
  113. {
  114. trigger.total.triggerTimes++;
  115. triggerData.triggerTimes++;
  116. }
  117. }
  118. public static bool CheckPrintAllData()
  119. {
  120. if(mNextPrintTime > CommonLang.CUtils.localTimeMS)
  121. {
  122. return false;
  123. }
  124. mNextPrintTime = CommonLang.CUtils.CurrentTimeMS + STRIGGER_INTERVAL;
  125. log.Info(" --------------------- PackEvent send ------------------- ");
  126. foreach (KeyValuePair<int, JSGProfileData> kv in mSendProfile)
  127. {
  128. log.Info(" - - - - - MsgID = " + kv.Key + ",\t Size = " + kv.Value.size +
  129. ", \ttimes = " + kv.Value.times + ", \t avg = " + (kv.Value.size / kv.Value.times));
  130. }
  131. log.Info(" --------------------- PackEvent recv ------------------- ");
  132. foreach (KeyValuePair<int, JSGProfileData> kv in mRecProfile)
  133. {
  134. log.Info(" - - - - - MsgID = " + kv.Key + ",\t Size = " + kv.Value.size +
  135. ", \ttimes = " + kv.Value.times + ", \t avg = " + (kv.Value.size / kv.Value.times));
  136. }
  137. log.Info("----------------------创建场景耗时统计输出----------------------");
  138. foreach (ZoneCreateInfo info in mZoneCreate.Values)
  139. {
  140. log.Info("##创建场景耗时统计:" + info.areaId + ", " + info.mTotalTimes + ", " + info.mTotalTimeUse);
  141. }
  142. log.Info("----------------------场景事件耗时输出----------------------");
  143. foreach (JSGTriggerProfile info in mZoneTriggers.Values)
  144. {
  145. log.Info("##场景触发器总揽:" + info.areaId + ", 检测次数:" + info.total.checkTimes + ", 触发次数: " + info.total.triggerTimes + ", 耗时:" + info.total.timeUse);
  146. if (info.total.checkTimes < 1000 || info.total.timeUse < 1000)
  147. {
  148. continue;
  149. }
  150. foreach (KeyValuePair<string, TriggerData> kv in info.profileData)
  151. {
  152. log.Info("------场景触发器总揽:" + kv.Key + ", 检测次数:" + kv.Value.checkTimes + ", 触发次数: " + kv.Value.triggerTimes + ", 耗时:" + kv.Value.timeUse);
  153. }
  154. }
  155. log.Info(" ---------------------------------------------------- ");
  156. return true;
  157. }
  158. }
  159. }