Click here to Skip to main content
15,881,281 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 231K   705   139  
NPerf is a framework for benchmarking classes and methods, that tastes like NUnit.
using System;
using System.Reflection;


namespace NPerf.Core
{
	using NPerf.Core.Collections;

	/// <summary>
	/// Summary description for TypeHelper.
	/// </summary>
	public sealed class TypeHelper
	{
		internal TypeHelper()
		{}

		public static bool HasCustomAttribute(Type t,Type customAttributeType)
		{
			if (t==null)
				throw new ArgumentNullException("t");
			if (customAttributeType==null)
				throw new ArgumentNullException("customAttributeType");

			return t.GetCustomAttributes(customAttributeType,true).Length!=0;
		}

		public static bool HasCustomAttribute(MethodInfo t,Type customAttributeType)
		{
			if (t==null)
				throw new ArgumentNullException("t");
			if (customAttributeType==null)
				throw new ArgumentNullException("customAttributeType");

			return t.GetCustomAttributes(customAttributeType,true).Length!=0;
		}

		public static Object GetFirstCustomAttribute(Type t, Type customAttributeType)
		{
			if (t==null)
				throw new ArgumentNullException("t");
			if (customAttributeType==null)
				throw new ArgumentNullException("customAttributeType");

			Object[] attrs = t.GetCustomAttributes(customAttributeType,true);
			if (attrs.Length==0)
				throw new ArgumentException("type does not have custom attribute");
			return attrs[0];
		}

		public static Object GetFirstCustomAttribute(MethodInfo mi, Type customAttributeType)
		{
			if (mi==null)
				throw new ArgumentNullException("mi");
			if (customAttributeType==null)
				throw new ArgumentNullException("customAttributeType");

			Object[] attrs = mi.GetCustomAttributes(customAttributeType,true);
			if (attrs.Length==0)
				throw new ArgumentException("type does not have custom attribute");
			return attrs[0];
		}

		public static MethodInfo GetAttributedMethod(Type t, Type customAttributeType)
		{
			if (t==null)
				throw new ArgumentNullException("t");
			if (customAttributeType==null)
				throw new ArgumentNullException("customAttributeType");

			foreach(MethodInfo m in t.GetMethods())
			{
				if (HasCustomAttribute(m,customAttributeType))
					return m;
			}

			return null;
		}

		public static AttributedMethodCollection GetAttributedMethods(Type t, Type customAttributeType)
		{
			if (t==null)
				throw new ArgumentNullException("t");
			if (customAttributeType==null)
				throw new ArgumentNullException("customAttributeType");

			return new AttributedMethodCollection(t,customAttributeType);
		}
		
		public static void CheckSignature(MethodInfo mi, Type returnType, params Type[] argumentTypes)
		{
			if (mi==null)
				throw new ArgumentNullException("mi");
			if (returnType==null)
				throw new ArgumentNullException("returnType");
			
			if (mi.ReturnType!=returnType)
				throw new ArgumentException("return type do not match");

			CheckArguments(mi,argumentTypes);		
		}
		
		public static void CheckArguments(MethodInfo mi, params Type[] argumentTypes)
		{
			if (mi==null)
				throw new ArgumentNullException("mi");
			foreach(Type t in argumentTypes)
				if (t==null)
					throw new ArgumentNullException("argumentType");
			
			ParameterInfo[] pis = mi.GetParameters();
			if (pis.Length != argumentTypes.Length)
				throw new ArgumentException("number of arguments do not match");
			
			for(int i = 0; i< pis.Length;++i)
			{
				if (pis[i].ParameterType != argumentTypes[i])
					throw new ArgumentException("argument type do not match");
			}			
		}
	}
}

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