PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/WorldView/Utilities/Profiler.cs

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