PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/Profiling/PerformanceTester.cs

#
C# | 126 lines | 65 code | 13 blank | 48 comment | 1 complexity | 9f514441353386fb781ba802ddec3f2f MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Diagnostics;
  3. namespace Delta.Utilities.Profiling
  4. {
  5. /// <summary>
  6. /// Performance tester helper class for profiling certain code paths
  7. /// many million times to compare different implementations. Heavily
  8. /// used for the MartixPerformance class.
  9. /// </summary>
  10. public class PerformanceTester
  11. {
  12. #region Delegates
  13. /// <summary>
  14. /// Just a boring void delegate with no parameters for profiling.
  15. /// </summary>
  16. public delegate void ProfileDelegate();
  17. #endregion
  18. #region Profile (Static)
  19. /// <summary>
  20. /// Profile some test code many million times and return how much time
  21. /// it took in ms.
  22. /// </summary>
  23. /// <param name="iterations">Iterations to execute the delegate</param>
  24. /// <param name="testDelegate">Test delegate to be executed</param>
  25. /// <returns>Number of milliseconds elapsed to run testDelegate iterations
  26. /// number of times</returns>
  27. public static int Profile(int iterations, ProfileDelegate testDelegate)
  28. {
  29. // Just call the test once before the profiling to do some setup to
  30. // prevent assembly loading and security policy checking which interfere
  31. // with our performance checking below!
  32. testDelegate();
  33. Stopwatch timer = new Stopwatch();
  34. timer.Start();
  35. for (int i = 0; i < iterations; i++)
  36. {
  37. testDelegate();
  38. }
  39. timer.Stop();
  40. numberOfProfileRuns++;
  41. totalProfileRunsMs += (int)timer.ElapsedMilliseconds;
  42. return (int)timer.ElapsedMilliseconds;
  43. }
  44. #endregion
  45. #region Profile10MilionTimes (Static)
  46. /// <summary>
  47. /// Helper to profile 10 million times and output the performance result.
  48. /// Profiles the code with 10 million iterations and output the result.
  49. /// </summary>
  50. /// <param name="profileTestName">Name for the test</param>
  51. /// <param name="testDelegate">Test delegate to execute</param>
  52. public static void Profile10MilionTimes(string profileTestName,
  53. ProfileDelegate testDelegate)
  54. {
  55. // Test 10 million times (not as quick, but much more accurate,
  56. // less fluctuations, especially on over clocked systems and i7)
  57. int ms = Profile(10 * 1000 * 1000, testDelegate);
  58. Console.WriteLine(ms + "ms for " + profileTestName +
  59. " (10 million calls)");
  60. }
  61. #endregion
  62. #region Profile1MilionTimes (Static)
  63. /// <summary>
  64. /// Profiles the code with 1 million iterations and output the result.
  65. /// </summary>
  66. /// <param name="profileTestName">Name for the test</param>
  67. /// <param name="testDelegate">Test delegate to execute</param>
  68. public static void Profile1MilionTimes(string profileTestName,
  69. ProfileDelegate testDelegate)
  70. {
  71. // Test 1 mio times (much quicker obviously, but not as accurate)
  72. int ms = Profile(0 * 1000 * 1000, testDelegate);
  73. Console.WriteLine(ms + "ms for " + profileTestName +
  74. " (1 million calls)");
  75. }
  76. #endregion
  77. #region ShowTotalProfileRuns (Static)
  78. /// <summary>
  79. /// Show total profile runs, which just displays the number of tests
  80. /// that were executed plus the total and average time spend in ms.
  81. /// After calling this numberOfProfileRuns and totalProfileRunsMs will
  82. /// both be reset to 0.
  83. /// </summary>
  84. public static void ShowTotalProfileRuns()
  85. {
  86. Console.WriteLine("In total " + numberOfProfileRuns + " Tests were " +
  87. "executed. Total time=" + totalProfileRunsMs +
  88. "ms, Average time=" +
  89. ((10 * totalProfileRunsMs / numberOfProfileRuns) / 10.0f) +
  90. "ms.");
  91. // Reset counters
  92. numberOfProfileRuns = 0;
  93. totalProfileRunsMs = 0;
  94. }
  95. #endregion
  96. #region Private
  97. #region numberOfProfileRuns (Private)
  98. /// <summary>
  99. /// Helpers for ShowTotalProfileRuns (just displays the number of tests
  100. /// that were executed plus the total and average time spend in ms).
  101. /// </summary>
  102. private static int numberOfProfileRuns;
  103. #endregion
  104. #region totalProfileRunsMs (Private)
  105. /// <summary>
  106. /// Helpers for ShowTotalProfileRuns (just displays the number of tests
  107. /// that were executed plus the total and average time spend in ms).
  108. /// </summary>
  109. private static int totalProfileRunsMs;
  110. #endregion
  111. #endregion
  112. }
  113. }