Click here to Skip to main content
15,896,154 members
Articles / Web Development / ASP.NET

MbUnit : Generative Unit Test Framework

Rate me:
Please Sign up or sign in to vote.
4.70/5 (38 votes)
15 Apr 20046 min read 222.1K   496   120  
A new highly flexible unit test framework with new fixtures
 using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;

namespace GUnit.Core.Monitoring
{
	/// <summary>
	/// A high performance timer
	/// </summary>
	/// <remarks>
	/// High Precision Timer based on Win32 methods.
	/// </remarks>
	/// <example>
	/// This example times the execution of a method:
	/// <code>
	/// TimeMonitor timer = new TimeMonitor();
	/// timer.Start();
	///    ... // execute code
	/// timer.Stop();
	/// 
	/// Console.WriteLine("Duration: {0}",timer.Duration);
	/// </example>
	public class TimeMonitor
	{
		[DllImport("Kernel32.dll")]
		private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);  

		[DllImport("Kernel32.dll")]
		private static extern bool QueryPerformanceFrequency(out long lpFrequency);
		
		private long startTime, stopTime;
		private long now;
		private long freq;
		
		/// <summary>Default constructor</summary>
		/// <remarks>Initializes the timer.</remarks>
		public TimeMonitor()
		{
			startTime = 0;
			stopTime  = 0;

			if (QueryPerformanceFrequency(out freq) == false)
			{
				// high-performance counter not supported 
				throw new Win32Exception(); 
			}
		}
		
		/// <summary>Gets the clock frequency</summary>
		/// <value>Clock frequency</value>
		public long Frequency
		{
			get
			{
				return this.freq;
			}
		}
		
		/// <summary>Starts the timer</summary>
		/// <remarks>Resets the duration and starts the timer</remarks>
		public void Start()
		{
			// lets do the waiting threads there work
			Thread.Sleep(0);  

			QueryPerformanceCounter(out startTime);
		}
		
		/// <summary>Stops the timer</summary>
		/// <remarks>Stops the timer</remarks>
		public void Stop()
		{
			QueryPerformanceCounter(out stopTime);
		}
		
		/// <summary>Gets the current duration value without stopping the timer</summary>
		/// <value>Current duration value</value>
		public double Now
		{
			get
			{
				QueryPerformanceCounter(out now);
				return (double)(now - startTime) / (double) freq;
			}		
		}

		/// <summary>Gets the timed duration value in seconds</summary>
		/// <value>Timer duration</value>
		public double Duration
		{
			get
			{
				return (double)(stopTime - startTime) / (double) freq;
			}
		}
	}
}

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


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions