/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
- using System;
- using System.Diagnostics;
-
- namespace Delta.Utilities.Profiling
- {
- /// <summary>
- /// Performance tester helper class for profiling certain code paths
- /// many million times to compare different implementations. Heavily
- /// used for the MartixPerformance class.
- /// </summary>
- public class PerformanceTester
- {
- #region Delegates
- /// <summary>
- /// Just a boring void delegate with no parameters for profiling.
- /// </summary>
- public delegate void ProfileDelegate();
- #endregion
-
- #region Profile (Static)
- /// <summary>
- /// Profile some test code many million times and return how much time
- /// it took in ms.
- /// </summary>
- /// <param name="iterations">Iterations to execute the delegate</param>
- /// <param name="testDelegate">Test delegate to be executed</param>
- /// <returns>Number of milliseconds elapsed to run testDelegate iterations
- /// number of times</returns>
- public static int Profile(int iterations, ProfileDelegate testDelegate)
- {
- // Just call the test once before the profiling to do some setup to
- // prevent assembly loading and security policy checking which interfere
- // with our performance checking below!
- testDelegate();
-
- Stopwatch timer = new Stopwatch();
- timer.Start();
- for (int i = 0; i < iterations; i++)
- {
- testDelegate();
- }
- timer.Stop();
-
- numberOfProfileRuns++;
- totalProfileRunsMs += (int)timer.ElapsedMilliseconds;
-
- return (int)timer.ElapsedMilliseconds;
- }
- #endregion
-
- #region Profile10MilionTimes (Static)
- /// <summary>
- /// Helper to profile 10 million times and output the performance result.
- /// Profiles the code with 10 million iterations and output the result.
- /// </summary>
- /// <param name="profileTestName">Name for the test</param>
- /// <param name="testDelegate">Test delegate to execute</param>
- public static void Profile10MilionTimes(string profileTestName,
- ProfileDelegate testDelegate)
- {
- // Test 10 million times (not as quick, but much more accurate,
- // less fluctuations, especially on over clocked systems and i7)
- int ms = Profile(10 * 1000 * 1000, testDelegate);
- Console.WriteLine(ms + "ms for " + profileTestName +
- " (10 million calls)");
- }
- #endregion
-
- #region Profile1MilionTimes (Static)
- /// <summary>
- /// Profiles the code with 1 million iterations and output the result.
- /// </summary>
- /// <param name="profileTestName">Name for the test</param>
- /// <param name="testDelegate">Test delegate to execute</param>
- public static void Profile1MilionTimes(string profileTestName,
- ProfileDelegate testDelegate)
- {
- // Test 1 mio times (much quicker obviously, but not as accurate)
- int ms = Profile(0 * 1000 * 1000, testDelegate);
- Console.WriteLine(ms + "ms for " + profileTestName +
- " (1 million calls)");
- }
- #endregion
-
- #region ShowTotalProfileRuns (Static)
- /// <summary>
- /// Show total profile runs, which just displays the number of tests
- /// that were executed plus the total and average time spend in ms.
- /// After calling this numberOfProfileRuns and totalProfileRunsMs will
- /// both be reset to 0.
- /// </summary>
- public static void ShowTotalProfileRuns()
- {
- Console.WriteLine("In total " + numberOfProfileRuns + " Tests were " +
- "executed. Total time=" + totalProfileRunsMs +
- "ms, Average time=" +
- ((10 * totalProfileRunsMs / numberOfProfileRuns) / 10.0f) +
- "ms.");
-
- // Reset counters
- numberOfProfileRuns = 0;
- totalProfileRunsMs = 0;
- }
- #endregion
-
- #region Private
-
- #region numberOfProfileRuns (Private)
- /// <summary>
- /// Helpers for ShowTotalProfileRuns (just displays the number of tests
- /// that were executed plus the total and average time spend in ms).
- /// </summary>
- private static int numberOfProfileRuns;
- #endregion
-
- #region totalProfileRunsMs (Private)
- /// <summary>
- /// Helpers for ShowTotalProfileRuns (just displays the number of tests
- /// that were executed plus the total and average time spend in ms).
- /// </summary>
- private static int totalProfileRunsMs;
- #endregion
-
- #endregion
- }
- }