Leaked source code of windows server 2003
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.

256 lines
6.2 KiB

  1. function Pad(str, cSpaces)
  2. {
  3. var strDigits = '';
  4. var i;
  5. if (cSpaces < 0)
  6. {
  7. for(i = 0; (-cSpaces) > str.length; ++i)
  8. str += ' ';
  9. }
  10. else
  11. {
  12. for(i = 0; cSpaces > str.length; ++i)
  13. str = ' ' + str;
  14. }
  15. return str;
  16. }
  17. function Array_ToTable(cColWidth)
  18. {
  19. var i;
  20. var str = "";
  21. var strSeperator = "";
  22. if (cColWidth == null)
  23. cColWidth = 8;
  24. for(i = 0; i < this.length; ++i)
  25. {
  26. if (arguments[i] != null)
  27. cColWidth = arguments[i];
  28. str += Pad(this[i].toString(), cColWidth) + strSeperator;
  29. }
  30. return str;
  31. }
  32. function String_ToRow()
  33. {
  34. return this;
  35. }
  36. function BeginTable()
  37. {
  38. var newwin = new Object();
  39. newwin.AppendRow = Table_AppendRow;
  40. newwin.EndTable = Table_EndTable;
  41. return newwin;
  42. }
  43. function Table_AppendRow(aData)
  44. {
  45. var str = aData.__toTable(-g_StopWatch.hWatches.__nNameSpace, g_StopWatch.hWatches.__nColSpace);
  46. LogMsg(str.__toRow(), 1);
  47. }
  48. function Table_EndTable()
  49. {
  50. LogMsg("",1);
  51. }
  52. var g_StopWatch = new Object();
  53. g_StopWatch.NAMESPACE = 32;
  54. g_StopWatch.COLSPACE = 12;
  55. g_StopWatch.MAXNAMELENGTH = 64;
  56. Array.prototype.__toTable = Array_ToTable;
  57. String.prototype.__toRow = String_ToRow;
  58. function CompareRows(Item1, Item2, col)
  59. {
  60. if (Item1[col] == Item2[col])
  61. return 0;
  62. if (Item1[col] != null && Item2[col] != null)
  63. {
  64. if (Item1[col] < Item2[col])
  65. return -1;
  66. return 1;
  67. }
  68. if (Item1[col] != null)
  69. return -1;
  70. if (Item2[col] != null)
  71. return 1;
  72. return 0;
  73. }
  74. function DumpTimes()
  75. {
  76. var obj;
  77. var str = "";
  78. var aRows = new Array();
  79. var i;
  80. var objTable = BeginTable();
  81. objTable.AppendRow(["name", "total", "max", "min", "avg", "count"]);
  82. for(obj in g_StopWatch.hWatches)
  83. {
  84. if (!g_StopWatch.hWatches.__isPublicMember(obj))
  85. continue;
  86. aRows[aRows.length] = g_StopWatch.hWatches[obj].Elapsed();
  87. }
  88. objTable.AppendRow(["Sorted by name"]);
  89. aRows.sort(function(a,b) {return CompareRows(a,b,0);});
  90. for(i = 0; i < aRows.length; ++i)
  91. objTable.AppendRow(aRows[i]);
  92. objTable.AppendRow(["Sorted by total"]);
  93. aRows.sort(function(a,b) {return CompareRows(a,b,1);});
  94. for(i = 0; i < aRows.length; ++i)
  95. objTable.AppendRow(aRows[i]);
  96. objTable.AppendRow([]);
  97. objTable.EndTable();
  98. }
  99. function BeginWatch(strName)
  100. {
  101. var obj;
  102. var re = /[<>&]/ig
  103. strName = strName.replace(re, '.');
  104. if (strName.length > g_StopWatch.MAXNAMELENGTH)
  105. strName = strName.slice(0, g_StopWatch.MAXNAMELENGTH);
  106. if (g_StopWatch["hWatches"] == null)
  107. {
  108. g_StopWatch.hWatches = new Object();
  109. g_StopWatch.hWatches.__nNameSpace = g_StopWatch.NAMESPACE;
  110. g_StopWatch.hWatches.__nColSpace = g_StopWatch.COLSPACE;
  111. if (g_StopWatch.hWatches.__isPublicMember == null)
  112. {
  113. g_StopWatch.hWatches.__isPublicMember = function(member)
  114. { return '__' != member.substr(0,2); }
  115. }
  116. }
  117. if (strName.length + 1 > g_StopWatch.hWatches.__nNameSpace)
  118. {
  119. g_StopWatch.hWatches.__nNameSpace = strName.length + 1;
  120. }
  121. if (g_StopWatch.hWatches[strName] == null)
  122. obj = g_StopWatch.hWatches[strName] = new StopWatch(strName);
  123. else
  124. obj = g_StopWatch.hWatches[strName];
  125. obj.Start();
  126. return obj;
  127. }
  128. function AddCounter(strName, nCount)
  129. {
  130. var obj;
  131. var re = /[<>&]/ig
  132. strName = strName.replace(re, '.');
  133. if (strName.length > g_StopWatch.MAXNAMELENGTH)
  134. strName = strName.slice(0, g_StopWatch.MAXNAMELENGTH);
  135. if (g_StopWatch["hWatches"] == null)
  136. {
  137. g_StopWatch.hWatches = new Object();
  138. g_StopWatch.hWatches.__nNameSpace = g_StopWatch.NAMESPACE;
  139. g_StopWatch.hWatches.__nColSpace = g_StopWatch.COLSPACE;
  140. if (g_StopWatch.hWatches.__isPublicMember == null)
  141. {
  142. g_StopWatch.hWatches.__isPublicMember = function(member)
  143. { return '__' != member.substr(0,2); }
  144. }
  145. }
  146. if (strName.length + 1 > g_StopWatch.hWatches.__nNameSpace)
  147. {
  148. g_StopWatch.hWatches.__nNameSpace = strName.length + 1;
  149. }
  150. if (g_StopWatch.hWatches[strName] == null)
  151. obj = g_StopWatch.hWatches[strName] = new ObjCounter(strName);
  152. else
  153. obj = g_StopWatch.hWatches[strName];
  154. obj.AddCount(nCount);
  155. return obj;
  156. }
  157. function ObjCounter(strName)
  158. {
  159. ObjCounter.prototype.AddCount = function(x)
  160. {
  161. this.nCount++;
  162. this.nTotal += x;
  163. if (x > this.nMax)
  164. this.nMax = x;
  165. if (this.nMin == -1 || x < this.nMin)
  166. this.nMin = x;
  167. }
  168. ObjCounter.prototype.Elapsed = function(y)
  169. {
  170. return [this.strName, this.nTotal, this.nMax, this.nMin, this.nTotal / this.nCount, this.nCount];
  171. }
  172. this.strName = strName;
  173. this.nCount = 0;
  174. this.nTotal = 0;
  175. this.nMax = 0;
  176. this.nMin = -1;
  177. }
  178. function StopWatch(strName)
  179. {
  180. StopWatch.prototype.Start = StopWatch_Start;
  181. StopWatch.prototype.Stop = StopWatch_Stop;
  182. StopWatch.prototype.Elapsed = StopWatch_Elapsed;
  183. this.strName = strName;
  184. this.elapsed = 0;
  185. this.nCount = 0;
  186. this.startTime = 0;
  187. this.endTime = 0;
  188. this.maxTime = 0;
  189. this.minTime = -1;
  190. }
  191. function StopWatch_Start()
  192. {
  193. this.startTime = (new Date()).getTime();
  194. }
  195. function StopWatch_Stop()
  196. {
  197. this.endTime = (new Date()).getTime();
  198. var thistime = this.endTime - this.startTime;
  199. this.elapsed += thistime;
  200. if (this.minTime == -1 || thistime < this.minTime)
  201. this.minTime = thistime;
  202. if (thistime > this.maxTime)
  203. this.maxTime = thistime;
  204. ++this.nCount;
  205. }
  206. function StopWatch_Elapsed()
  207. {
  208. var secs = this.elapsed / 1000;
  209. /* name, total, max, avg, iterations */
  210. return [this.strName, secs, this.maxTime / 1000, this.minTime / 1000,(Math.floor(secs / this.nCount * 1000) / 1000), this.nCount];
  211. }