/WorldView/Utilities/Profiler.cs
C# | 148 lines | 108 code | 34 blank | 6 comment | 16 complexity | 7a440fb8049a6b0dd5093c37657066d5 MD5 | raw file
1using System; 2using System.Collections.Generic; 3 4namespace MoreTerra.Utilities 5{ 6 public class Profiler 7 { 8 private Dictionary<Int32, String> openIDs; 9 private Dictionary<String, Dictionary<Int32, pTimeSpan>> profileList; 10 private Int32 currentID; 11 12 #region Helper Structures 13 class pTimeSpan 14 { 15 public DateTime Start; 16 public DateTime End; 17 public String Note; 18 19 public pTimeSpan(DateTime newDateTime, String newNote) 20 { 21 Start = newDateTime; 22 End = new DateTime(0); 23 24 if (String.IsNullOrEmpty(newNote)) 25 Note = newNote; 26 else 27 Note = ""; 28 } 29 30 public TimeSpan Elapsed() 31 { 32 if (End.CompareTo(Start) < 0) 33 return new TimeSpan(0); 34 35 return End - Start; 36 } 37 } 38 #endregion 39 40 #region Constructors 41 public Profiler() 42 { 43 openIDs = new Dictionary<Int32, String>(); 44 profileList = new Dictionary<String, Dictionary<Int32, pTimeSpan>>(); 45 currentID = 0; 46 } 47 #endregion 48 49 public void Clear() 50 { 51 openIDs.Clear(); 52 profileList.Clear(); 53 currentID = 0; 54 } 55 56 public Int32 Start(String pName, String useNote = null) 57 { 58 pTimeSpan pts = new pTimeSpan(DateTime.Now, useNote); 59 60 openIDs.Add(currentID, pName); 61 62 if (profileList.ContainsKey(pName) == false) 63 profileList[pName] = new Dictionary<Int32, pTimeSpan>(); 64 65 profileList[pName].Add(currentID, pts); 66 67 return ++currentID; 68 } 69 70 public void Stop(Int32 id) 71 { 72 String pName; 73 Dictionary<Int32, pTimeSpan> pList; 74 75 if (openIDs.ContainsKey(id) == false) 76 // Throw exception. 77 return; 78 79 pName = openIDs[id]; 80 81 if (profileList.ContainsKey(pName) == false) 82 // Throw exception. 83 return; 84 85 pList = profileList[pName]; 86 87 if (pList.ContainsKey(id) == false) 88 // Throw exception. 89 return; 90 91 pList[id].End = DateTime.Now; 92 93 openIDs.Remove(id); 94 } 95 96 public void Stop(String pName) 97 { 98 Int32 lastID = -1; 99 100 // We really should walk the tree backwards. 101 foreach (KeyValuePair<Int32, String> kvp in openIDs) 102 { 103 if (kvp.Value == pName) 104 lastID = kvp.Key; 105 } 106 107 if (lastID == -1) 108 // Throw Exception. 109 return; 110 111 112 Stop(lastID); 113 } 114 115 public TimeSpan Total(String pName) 116 { 117 TimeSpan t = new TimeSpan(0); 118 Dictionary<Int32, pTimeSpan> pList; 119 120 if (profileList.ContainsKey(pName) == false) 121 // Throw exception. 122 return t; 123 124 pList = profileList[pName]; 125 126 foreach (KeyValuePair<Int32, pTimeSpan> kvp in pList) 127 { 128 t += kvp.Value.Elapsed(); 129 } 130 131 return t; 132 } 133 134 #region Overrides 135 public override string ToString() 136 { 137 String returnVal = ""; 138 139 foreach (KeyValuePair<String, Dictionary<Int32, pTimeSpan>> kvp in profileList) 140 { 141 returnVal = returnVal + kvp.Key + " : " + Total(kvp.Key).ToString() + Environment.NewLine; 142 } 143 144 return returnVal; 145 } 146 #endregion 147 } 148}