Click here to Skip to main content
15,894,106 members
Articles / Programming Languages / C#

NPerf, A Performance Benchmark Framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.92/5 (44 votes)
25 Jan 20044 min read 232.7K   705   139  
NPerf is a framework for benchmarking classes and methods, that tastes like NUnit.
//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.1.4322.573
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------


namespace System.Perf 
{
	using System;
	using System.Collections;
	using NPerf.Framework;
	
	
	/// <summary>
	/// TODO - Add class summary
	/// </summary>
	/// <remarks>
	/// 	created by - dehalleux
	/// 	created on - 26/01/2004 13:39:48
	/// </remarks>
	[PerfTester(typeof(IDictionary),15
		 ,Description = "IDictionary interface benchmark, addition and insertion"
		 ,FeatureDescription="Number of elements")]
	public class EmptyDictionaryTester
	{
		private int count;
		private Random rnd = new Random();
		
		internal int CollectionCount(int testIndex)
		{
			int n = 0;
			
			if (testIndex < 0)
				n=10;
			else
				n = (int)Math.Pow(2,testIndex);
			
			return n;			
		}		
		
		[PerfRunDescriptor]
		public double RunDescription(int testIndex)
		{
			return (double)CollectionCount(testIndex);
		}
		
		[PerfSetUp]
		public void SetUp(int index, IDictionary dic)
		{
			this.count = CollectionCount(index);	
		}
		
		[PerfTest]
		public void Add(IDictionary dic)
		{
			for(int i = 0;i<this.count;++i)
				dic.Add(rnd.Next(),null);
		}
		
		[PerfTest]
		public void RandomItem(IDictionary dic)
		{
			for(int i = 0;i<this.count;++i)
			{
				dic[rnd.Next()] = null;
			}
		}
	}

	[PerfTester(typeof(IDictionary),14
		 ,Description = "IDictionary interface benchmark, contains and enumeration"
		 ,FeatureDescription="Number of elements")]
	public class ContainsDictionaryTester
	{
		private int count;
		private Random rnd = new Random();
		
		internal int CollectionCount(int testIndex)
		{
			int n = 0;
			
			if (testIndex < 0)
				n=10;
			else
				n = (int)Math.Pow(2,testIndex);
			
			return n;			
		}		
		
		[PerfRunDescriptor]
		public double RunDescription(int testIndex)
		{
			return (double)CollectionCount(testIndex);
		}
		
		[PerfSetUp]
		public void SetUp(int index, IDictionary dic)
		{
			this.count = CollectionCount(index);	
			for(int i = 0;i<this.count;++i)
				dic[rnd.Next()]=null; 
		}
		
		[PerfTest]
		public void Contains(IDictionary dic)
		{
			foreach(DictionaryEntry de in dic)
			{
				bool b = dic.Contains(de.Key);
			}
		}
		
		[PerfTest]
		public void Enumerate(IDictionary dic)
		{
			foreach(DictionaryEntry de in dic)
			{
				object o = de.Key;
			}
		}
	}

	[PerfTester(typeof(IDictionary),10)] 
	public class DictionaryTester 
	{     
		private int count = 0;
		private Random rnd = new Random();
		[PerfRunDescriptor] 
		public double Count(int index) 
		{
			return index*1000;
		}

		[PerfSetUp]
		public void SetUp(int index, IDictionary dic)
		{
			this.count = (int)Math.Floor(Count(index));
		}

		[PerfTest]
		public void ItemAssign(IDictionary dic)
		{
			for(int i =0;i<this.count;++i)
				dic[rnd.Next()]=null;
		}
	}


}

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