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.

114 lines
2.8 KiB

  1. //---------------------------------------------------------------------------
  2. // File: TimerTest.cool
  3. //
  4. // A basic class for inheriting Tests that want to get timing information.
  5. //---------------------------------------------------------------------------
  6. using System;
  7. using System.Runtime.InteropServices;
  8. public class TimerTest
  9. {
  10. public long m_Start;
  11. public long m_End;
  12. public long m_Freq;
  13. long m_Min;
  14. long m_Max;
  15. public long m_Count;
  16. public long m_Sum;
  17. [DllImport("KERNEL32.DLL", EntryPoint="QueryPerformanceCounter", SetLastError=true,
  18. CharSet=CharSet.Unicode, ExactSpelling=true,
  19. CallingConvention=CallingConvention.StdCall)]
  20. public static extern int QueryPerformanceCounter(ref long time);
  21. [DllImport("KERNEL32.DLL", EntryPoint="QueryPerformanceFrequency", SetLastError=true,
  22. CharSet=CharSet.Unicode, ExactSpelling=true,
  23. CallingConvention=CallingConvention.StdCall)]
  24. public static extern int QueryPerformanceFrequency(ref long freq);
  25. public TimerTest()
  26. {
  27. m_Start = m_End = 0;
  28. QueryPerformanceFrequency(ref m_Freq);
  29. m_Min = m_Max = m_Count = m_Sum = 0;
  30. }
  31. public void Start()
  32. {
  33. long i = 0;
  34. QueryPerformanceCounter(ref i);
  35. m_Start = i;
  36. // m_Start = GetMilliseconds();
  37. m_End = m_Start;
  38. }
  39. public void Stop()
  40. {
  41. long i = 0;
  42. QueryPerformanceCounter(ref i);
  43. m_End = i;
  44. m_Sum += (m_End - m_Start);
  45. m_Count++;
  46. //m_End = GetMilliseconds();
  47. }
  48. public long GetDuration() // in milliseconds.
  49. {
  50. return (m_End - m_Start);
  51. }
  52. public long GetMilliseconds()
  53. {
  54. long i = 0;
  55. QueryPerformanceCounter(ref i);
  56. return ((i * (long)1000) / m_Freq);
  57. }
  58. // These methods allow you to count up multiple iterations and
  59. // then get the median, average and percent variation.
  60. public void Count(long ms)
  61. {
  62. if (m_Min == 0) m_Min = ms;
  63. if (ms < m_Min) m_Min = ms;
  64. if (ms > m_Max) m_Max = ms;
  65. m_Sum += ms;
  66. m_Count++;
  67. }
  68. public long Min()
  69. {
  70. return m_Min;
  71. }
  72. public long Max()
  73. {
  74. return m_Max;
  75. }
  76. public double Median()
  77. {
  78. return TwoDecimals(m_Min + ((m_Max - m_Min)/2.0));
  79. }
  80. public double PercentError()
  81. {
  82. double spread = (m_Max - m_Min)/2.0;
  83. double percent = TwoDecimals((double)(spread*100.0)/(double)(m_Min));
  84. return percent;
  85. }
  86. public double TwoDecimals(double i)
  87. {
  88. return Math.Round(i * 100) / 100;
  89. }
  90. public long Average()
  91. {
  92. return m_Sum / m_Count;
  93. }
  94. public void Clear()
  95. {
  96. m_Start = m_End = m_Min = m_Max = m_Sum = m_Count = 0;
  97. }
  98. };