PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/Utilities/Profiling/PerformanceTester.cs

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