Click here to Skip to main content
15,868,016 members
Articles / Web Development / HTML

NTime - Performance unit testing tool

Rate me:
Please Sign up or sign in to vote.
4.73/5 (27 votes)
30 Mar 20066 min read 430.5K   8.2K   163  
An article on a performance testing tool to test an application against its performance
#region Copyright (c) 2004, Adam Slosarski
/************************************************************************************
'
' Copyright  2004 Adam Slosarski
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the authors be held liable for any damages arising from the use of this 
' software.
' 
' Permission is granted to anyone to use this software for any purpose, including 
' commercial applications, and to alter it and redistribute it freely, subject to the 
' following restrictions:
'
' 1. The origin of this software must not be misrepresented; you must not claim that 
' you wrote the original software. If you use this software in a product, an 
' acknowledgment (see the following) in the product documentation is required.
'
' Portions Copyright  2004 Adam Slosarski
'
' 2. Altered source versions must be plainly marked as such, and must not be 
' misrepresented as being the original software.
'
' 3. This notice may not be removed or altered from any source distribution.
'
'***********************************************************************************/
#endregion
using System;

namespace NTime.Framework
{
	/// <summary>
	/// Time period constants used by <see cref="TimerHitCountTestAttribute"/> and <see cref="TimerDurationTestAttribute"/>.
	/// </summary>
	/// <example>
	/// Followed example shows how to set time period to 10 times per second in <see cref="TimerHitCountTestAttribute"/>.
	/// <code>
	/// [TimerHitCountTest(10, Unit = TimePeriod.Second)]
	/// </code>
	/// </example>
	/// <remarks>Time periods are customizable units for different time frames of code performance tests.
	/// Sometimes you need to keep your hit count of method execution to be not less than 300 times per second, this
	/// would be typical scenario for real server applications. Other time frame would be used in critical sections, therefore
	/// time period should be set to nanoseconds probably.
	/// </remarks>
	public enum TimePeriod {
		/// <summary>
		/// Time period is set to one nanosecond. Use this value in critical sections to collect performance test.
		/// </summary>
		Nanosecond,
		/// <summary>
		/// Time period is set to one microsecond. This is default value for <see cref="TimerDurationTestAttribute"/>.
		/// </summary>
		Microsecond,
		/// <summary>
		/// Time period is set to one millisecond.
		/// </summary>
		Millisecond,
		/// <summary>
		/// Time period is set to one second. This is default value for <see cref="TimerHitCountTestAttribute"/>.
		/// </summary>
		Second,
		/// <summary>
		/// Time period is set to one minute. You can use this value in complex task as database transactions.
		/// </summary>
		Minute,
		/// <summary>
		/// Time period is set to one hour.
		/// </summary>
		Hour};

	/// <summary>
	/// Specifies timing usage for user class.
	/// This class cannot be inherited.
	/// </summary>
	/// <remarks>This is main attribute your class must have to enable perfomance tests.
	/// </remarks>
	/// <example>Mark your class as follow, to make it dedicated to run performance tests.
	/// <code>
	/// [TimerFixture]
	///	public class ServerPerformanceTest
	/// {
	///		...
	/// }
	/// </code></example>
	[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
	public sealed class TimerFixtureAttribute : Attribute
	{
	}

	/// <summary>
	/// Specifies hit count test for the method.
	/// This class cannot be inherited.
	/// </summary>
	/// <remarks>This attribute marks methods in <see cref="TimerFixtureAttribute"/> attributed class to provide
	/// hit count performance tests in methods. Performance is completed when hit count is above minimum value per certain time period, otherwise test fails.
	/// It is useful performance test to keep your application code more responsible to clients. Default time period is one second.</remarks>
	/// <example>Attribute method for hit count performance test of minimum 20 calls per millisecond is shown below.
	/// <code>
	/// [TimerFixture]
	/// public class TestPerformance
	/// {
	///		[TimerHitCountTest(20, Unit = Millisecond)]
	///		public void RunTest()
	///		{
	///			...
	///		}
	/// }
	/// </code></example>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
	public sealed class TimerHitCountTestAttribute : Attribute
	{
		/// <summary>
		/// Minimum hits per second (or other time period) that must occur, otherwise performance test fails.
		/// </summary>
		public int ExpectedHits;
		/// <summary>
		/// Number of concurrent threads that call method.
		/// </summary>
		public int Threads;
		/// <summary>
		/// Time period for hit count calculating.
		/// </summary>
		public TimePeriod Unit;

		/// <summary>
		/// Initializes a new instance of attribute class to enable method hit count test.
		/// Optionally specify <see cref="TimerHitCountTestAttribute.Threads"/> for multithreaded tests and
		/// <see cref="TimerHitCountTestAttribute.Unit"/> with one of available <see cref="TimePeriod"/> constant to change time unit (defaults to second).
		/// </summary>
		/// <param name="expectedHits">Minimum hits per second (or other time period) that must occur, otherwise performance test fails.</param>
		public TimerHitCountTestAttribute(int expectedHits)
		{
			ExpectedHits = expectedHits;
			Threads = 1;
			Unit = TimePeriod.Second;
		}
	}

	/// <summary>
	/// Specifies duration test.
	/// This class cannot be inherited.
	/// </summary>
	/// <remarks>This attribute marks methods in <see cref="TimerFixtureAttribute"/> class to provide
	/// duration performance tests. Performance test is completed when time execution is less than minimum provided time in certain time unit, otherwise test fails.
	/// It is useful performance test to keep your application code run fast. Default time unit is microseconds.</remarks>
	/// <example>Attribute method for duration performance test of maximum 60 nanoseconds is shown below.
	/// <code>
	/// [TimerFixture]
	/// public class TestPerformance
	/// {
	///		[TimerDurationTest(60, Unit = Nanosecond)]
	///		public void RunTest()
	///		{
	///			...
	///		}
	/// }
	/// </code></example>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
	public sealed class TimerDurationTestAttribute : Attribute
	{
		/// <summary>
		/// Minimum time (defaults to microseconds) the method has to be executed, otherwise performance test fails.
		/// </summary>
		public int Duration;
		/// <summary>
		/// Number of concurrent threads that call method.
		/// </summary>
		public int Threads;
		/// <summary>
		/// Time period for duration test.
		/// </summary>
		public TimePeriod Unit;

		/// <summary>
		/// Initializes attribute class for method time execution.
		/// Optionally specify <see cref="TimerDurationTestAttribute.Threads"/> for multithreaded tests and
		/// <see cref="TimerDurationTestAttribute.Unit"/> with one of available <see cref="TimePeriod"/> constant to change measure (defaults to microseconds).
		/// </summary>
		/// <param name="duration">Minimum time (defaults to microseconds) the method has to be executed, otherwise performance test fails.</param>
		public TimerDurationTestAttribute(int duration)
		{
			Duration = duration;
			Threads = 1;
			Unit = TimePeriod.Microsecond;
		}
	}

	/// <summary>
	/// Specifies method to test against performance counters.
	/// This class cannot be inherited.
	/// </summary>
	/// <remarks>This attribute marks methods in <see cref="TimerFixtureAttribute"/> class to provide
	/// custom performance counter tests in methods. Performance test is completed when average performance counter value is in range of minimum and maximum value set (set at least one value), otherwise test fails.
	/// It is useful performance test to keep your application run smoothly with many performance counter checks. For more information refer to <see cref="System.Diagnostics.PerformanceCounter"/>.
	/// Note that you can specify in a special string '*' in member <see cref="TimerCounterTestAttribute.InstanceName"/> to fetch actual application name i.e. either NTimeGUI or NTime.
	/// </remarks>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
	public sealed class TimerCounterTestAttribute : Attribute
	{
		/// <summary>
		/// The name of the performance counter category for performance counter.
		/// </summary>
		public string CategoryName;
		/// <summary>
		/// The name of the performance counter that is associated with PerformanceCounter instance.
		/// </summary>
		public string CounterName;
		/// <summary>
		/// The instance name for performance counter, note that you can specify here special string '*' to fetch actual application name i.e. either NTimeGUI or NTime.
		/// </summary>
		public string InstanceName;
		/// <summary>
		/// The computer name for performance counter. Do not use this member, because is not supported.
		/// </summary>
		public string MachineName;
		/// <summary>
		/// Number of concurrent threads that call method.
		/// </summary>
		public int Threads;
		/// <summary>
		/// Minimum value that performance counter may have to accept performance test.
		/// </summary>
		public int MinimumValue;
		/// <summary>
		/// Maximum value that performance counter may have to accept performance test.
		/// </summary>
		public int MaximumValue;
		/// <summary>
		/// Initializes attribute class to test method against performance counters.
		/// Optionally specify <see cref="TimerCounterTestAttribute.Threads"/> for multithreaded
		/// tests, <see cref="TimerCounterTestAttribute.InstanceName"/>, <see cref="TimerCounterTestAttribute.MachineName"/>,
		/// minimum and/or maximum acceptable value for perfomance counter <see cref="TimerCounterTestAttribute.MinimumValue"/>, <see cref="TimerCounterTestAttribute.MaximumValue"/>.
		/// See also <see cref="System.Diagnostics.PerformanceCounter"/> class.
		/// <seealso cref="System.Diagnostics.PerformanceCounter"/>
		/// </summary>
		/// <param name="categoryName">The name of the performance counter category for performance counter.</param>
		/// <param name="counterName">The name of the performance counter that is associated with PerformanceCounter instance.</param>
		public TimerCounterTestAttribute(string categoryName, string counterName)
		{
			Threads = 1;
			CategoryName = categoryName;
			CounterName = counterName;
			MinimumValue = 0;
			MaximumValue = 0;
		}
	}

	/// <summary>
	/// Specifies place to set up environment with TimerFixture class.
	/// This class cannot be inherited.
	/// </summary>
	/// <remarks>This attribute when used with method prepares additional set up for your performance test methods.
	/// It is not required to specify this attribute always, some methods may still work without TimerSetUp attribute.
	/// Every <see cref="TimerFixtureAttribute"/> class may have its own TimerSetUpAttribute or <see cref="TimerTearDownAttribute"/> as well.</remarks>
	/// <example>Mark your class as follows:
	/// <code>
	/// [TimerSetUp]
	///	public void PerformanceSetUpRoutine
	/// {
	///		...
	/// }
	/// </code></example>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
	public sealed class TimerSetUpAttribute : Attribute
	{
	}

	/// <summary>
	/// Specifies place to tear down environment with TimerFixture class.
	/// This class cannot be inherited.
	/// </summary>
	/// <remarks>This attribute when used with method frees additional objects created by <see cref="TimerSetUpAttribute"/> class.
	/// It is not required to specify this attribute always, some methods may still work without TimerTearDown attribute.
	/// Every <see cref="TimerFixtureAttribute"/> class may have its own TimerTearDownAttribute or <see cref="TimerSetUpAttribute"/> as well.</remarks>
	/// <example>Mark your class as follows:
	/// <code>
	/// [TimerTearDown]
	///	public void PerformanceTearDownRoutine
	/// {
	///		...
	/// }
	/// </code></example>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
	public sealed class TimerTearDownAttribute : Attribute
	{
	}

	/// <summary>
	/// Specifies place for global set up environment for whole performance test.
	/// This class cannot be inherited.
	/// </summary>
	/// <remarks>This attribute when used with method prepares main set up for your performance test.
	/// It is not required to specify this attribute.</remarks>
	/// <example>Mark your class as follows:
	/// <code>
	/// [TimerFixtureSetUp]
	///	public void PerformanceGlobalSetUpRoutine
	/// {
	///		...
	/// }
	/// </code></example>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
	public sealed class TimerFixtureSetUpAttribute : Attribute
	{
	}

	/// <summary>
	/// Specifies place to globally tear down environment for whole performance test.
	/// This class cannot be inherited.
	/// </summary>
	/// <remarks>This attribute when used with method at the end of performance test frees additional objects created by <see cref="TimerFixtureSetUpAttribute"/> class.
	/// It is not required to specify this attribute.</remarks>
	/// <example>Mark your class as follows:
	/// <code>
	/// [TimerFixtureTearDown]
	///	public void PerformanceGlobalTearDownRoutine
	/// {
	///		...
	/// }
	/// </code></example>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
	public sealed class TimerFixtureTearDownAttribute : Attribute
	{
	}

	/// <summary>
	/// Instructs performance test tool to skip performance test and mark it as a warning information.
	/// This class cannot be inherited.
	/// </summary>
	/// <remarks>Code you are working on sometimes may be not optimized to pass it by performance test, therefore
	/// you should replace actually marked test attribute with TimerIgnore attribute for this reason.</remarks>
	/// <example>Mark your class as follows:
	/// <code>
	/// // [TimerHitCountTest(20, Unit = Millisecond)] // removed from performance check
	/// [TimerIgnore("Implementing new algorithm since it's crashing for now")]
	///	public void QuickSort
	/// {
	///		...
	/// }
	/// </code></example>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
	public sealed class TimerIgnoreAttribute : Attribute
	{
		/// <summary>
		/// Warning text in performance test tool to indicate that method cannot be checked against perfomance test due to some algorithm problems.
		/// </summary>
		public string Info;
		/// <summary>
		/// Initializes attribute class to disable performance test and generate warning information.
		/// </summary>
		/// <param name="ignoreInfo">Sets warning text in performance test tool to indicate that method cannot be checked against perfomance test due to some algorithm problems.</param>
		public TimerIgnoreAttribute(string ignoreInfo)
		{
			Info = ignoreInfo;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
Poland Poland
Born in Poland, living there as employeed developer, in free time writing much .net stuff and designing applications.

Comments and Discussions