Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

NTime - Performance unit testing tool

, 30 Mar 2006
An article on a performance testing tool to test an application against its performance.
ntime_demo.zip
Installer
1.1
NTimeSetup.msi
Setup.Exe
Setup.Ini
2.0
NTimeSetup.msi
setup.exe
ntime_sdk.zip
NTime Framework SDK Documentation.chm
ntime_src.zip
src
Framework
License.rtf
ntime.snk
NTimeConsole
App.ico
NTimeGUI
folder.bmp
folder2.bmp
gray.bmp
green.bmp
NTime.ico
red.bmp
yellow.bmp
NTimeSetup
NTimeSetup.vdproj
PerformanceTests
Readme.rtf
ntime_src_11.zip
NTime_src_1.1
Framework
.VPUML
.history
.historyinfo_NTime.Framework.dll
NTime.Framework.dll.vpp.v1
NTime.Framework.dll.vpp
bin
Release
NTime.Framework.dll
ModelSupport
default.txvpck
NTime
Framework
Framework.txvpck
NTime.txvpck
mssccprj.scc
NTime.Framework
NTime.Framework.dll.csproj.user
NTime.Framework.dll.csproj.vspscc
vssver.scc
ntime.snk
NTimeAddin
bin
Release
NTimeAddin.dll
NTimeAddin.tlb
NTimeAddin.csproj.user
NTimeAddinSetup
NTimeAddinSetup.vdproj
NTimeConsole
App.ico
bin
Release
NTime.exe
NTimeConsole.exe
ModelSupport
mssccprj.scc
NTime.csproj.user
NTime.csproj.vspscc
vssver.scc
NTimeGUI
.VPUML
.history
.historyinfo_NTimeGUI
NTimeGUI.vpp.v1
NTimeGUI.vpp
bin
Release
NTimeGUI.exe
folder.bmp
folder2.bmp
gray.bmp
green.bmp
ModelSupport
default.txvpck
NTime
GUI
GUI.txvpck
NTime.txvpck
mssccprj.scc
NTime.ico
NTimeGUI.csproj.user
NTimeGUI.csproj.vspscc
out
doc
doc-files
default.gif
gendoc.gdc
model-tree
false.gif
model.tree
ST_Attribute.gif
ST_Class Diagram.gif
ST_Class.gif
ST_Operation.gif
ST_Package Diagram.gif
ST_Package.gif
ST_Project.gif
true.gif
navigation.jar
NTime
doc-files
NTime.gif
GUI
doc-files
Class Diagram1.gif
GUI.gif
red.bmp
vssver.scc
yellow.bmp
NTimeSetup
mssccprj.scc
NTimeSetup.vdproj
NTimeSetup.vdproj.vspscc
vssver.scc
PerformanceTests
bin
Release
PerformanceTests.dll
PerformanceTests.ntime
ModelSupport
PerformanceTests
mssccprj.scc
PerformanceTests.csproj.user
PerformanceTests.csproj.vspscc
vssver.scc
ntime_src_20.zip
NTime_src_2.0
Framework
.VPUML
.history
.historyinfo_NTime.Framework.dll
NTime.Framework.dll.vpp.v1
NTime.Framework.dll.vpp
bin
Release
NTime.Framework.dll
ModelSupport
default.txvpck
NTime
Framework
Framework.txvpck
NTime.txvpck
mssccprj.scc
NTime.Framework
NTime.Framework.dll
NTime.Framework.dll.csproj.user
NTime.Framework.dll.csproj.vspscc
Properties
vssver.scc
NTimeConsole
App.ico
bin
Release
NTime.exe
NTime.Framework.dll
NTimeConsole.exe
ModelSupport
mssccprj.scc
NTime.csproj.user
NTime.csproj.vspscc
Properties
vssver.scc
NTimeGUI
.VPUML
.history
.historyinfo_NTimeGUI
NTimeGUI.vpp.v1
NTimeGUI.vpp
bin
Release
NTime.Framework.dll
NTimeGUI.exe
folder.bmp
folder2.bmp
gray.bmp
green.bmp
ModelSupport
default.txvpck
NTime
GUI
GUI.txvpck
NTime.txvpck
mssccprj.scc
NTime.ico
NTimeGUI.csproj.user
NTimeGUI.csproj.vspscc
out
doc
doc-files
default.gif
gendoc.gdc
model-tree
false.gif
model.tree
ST_Attribute.gif
ST_Class Diagram.gif
ST_Class.gif
ST_Operation.gif
ST_Package Diagram.gif
ST_Package.gif
ST_Project.gif
true.gif
navigation.jar
NTime
doc-files
NTime.gif
GUI
doc-files
Class Diagram1.gif
GUI.gif
Properties
red.bmp
vssver.scc
yellow.bmp
NTimeSetup
mssccprj.scc
NTimeSetup.vdproj
NTimeSetup.vdproj.vspscc
vssver.scc
PerformanceTests
bin
Release
NTime.Framework.dll
PerformanceTests.dll
PerformanceTests.ntime
ModelSupport
PerformanceTests
mssccprj.scc
PerformanceTests.csproj.user
PerformanceTests.csproj.vspscc
Properties
vssver.scc
#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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 31 Mar 2006
Article Copyright 2004 by AdamSlosarski
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid