JSGServerProfile.cs 5.8 KB

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