Click here to Skip to main content
15,881,248 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;
using System.Xml;
using System.Xml.Serialization;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.IO;

namespace NPerf.Cons
{
	using NPerf.Core;
	using NPerf.Core.Collections;
	using NPerf.Report;
	using NPerf.Core.Tracer;

	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			try
			{
				ClArguments cl = new ClArguments();
				cl.AddParameter(new ClParameter("testera","ta","Tester Assembly",false,false));
				cl.AddParameter(new ClParameter("testeda","tdfa","Tested Assembly",false,false));
				cl.AddParameter(new ClParameter("testedapartial","tdfap","Tested Assembly Parital Name",false,false));
				cl.AddParameter(new ClParameter("ignoredtype","it","Ingored types",false,false));
				cl.AddParameter(new ClParameter("verbose","v","Verbose",true,true));
				cl.AddParameter(new ClParameter("outputtype","ot","Output type",true,false));
				cl.AddParameter(new ClParameter("xml","x","Output as XML",true,true));

				cl.Parse(args);
				cl.ProcessHelp();


				// loading testers
				if (!cl.ContainsDuplicate("ta"))
					throw new Exception("You did not specify a tester assembly");

				// load testers
				PerfTesterCollection testers = new PerfTesterCollection();
				foreach(String name in cl.Duplicates("ta"))
				{
					Console.WriteLine("Load tester assembly: {0}",name);
					PerfTester.FromAssembly(
						  testers,
						  Assembly.LoadFrom(name)
						  );
				}
				if (testers.Count == 0)
					throw new Exception("Could not find any tester class");

				// load tested
				foreach(PerfTester tester in testers)
				{
					if (tester.IsIgnored)
						continue;		
			
					if (cl.ContainsDuplicate("tdfa"))
					{
						foreach(String name in cl.Duplicates("tdfa"))
						{
							Console.WriteLine("Load tested assembly: {0}",name);
							tester.LoadTestedTypes(Assembly.LoadFrom(name));
						}
					}

					if (cl.ContainsDuplicate("tdfap"))
					{
						foreach(String name in cl.Duplicates("tdfap"))
						{
							Console.WriteLine("Load tested assembly: {0}",name);
							tester.LoadTestedTypes(Assembly.LoadWithPartialName(name));
						}
					}

					Console.WriteLine("Tested types:");
					foreach(Type t in tester.TestedTypes)
					{
						Console.WriteLine("\t{0}",t.Name);
					}

					// removing types
					if (cl.ContainsDuplicate("it"))
					{
						foreach(String name in cl.Duplicates("it"))
						{
							Console.Write("Ignoring type: <{0}>",name);
							Type t = Type.GetType(name);
							if (t==null)
								Console.WriteLine(" - could not load type.");
							else
							{
								if (tester.TestedTypes.Contains(t))
								{
									Console.WriteLine(" removed");
									tester.TestedTypes.Remove(t);
								}
								else
									Console.WriteLine("-- could not remove {0}",t.Name);
							}
						}
					}
				}

				// run test
				ChartReport chart = new ChartReport(800,400);
				chart.Colors.Map = ColorMap.Jet;
				ImageFormat im = ImageFormat.Png;

				if (cl.ContainsUnique("ot"))
				{
					switch(cl.Unique("ot").ToLower())
					{
					case "png":
							im = ImageFormat.Png;
							break;
					case "gif":
						im = ImageFormat.Gif;
						break;
					case "jpeg":
						im = ImageFormat.Jpeg;
						break;
					case "bmp":
						im = ImageFormat.Bmp;
						break;
					case "emf":
						im = ImageFormat.Emf;
						break;
					}
				}

				foreach(PerfTester tester in testers)
				{
					if (tester.IsIgnored)
						continue;

					TextWriterTracer tracer = new TextWriterTracer();
					tracer.Attach(tester);
										
					PerfTestSuite suite = tester.RunTests();

					if (cl.ContainsUnique("xml"))
					{
						using (FileStream file = File.OpenWrite(tester.TestedType.Name + "." + suite.Name + ".xml"))
						{
							StreamWriter writer = new StreamWriter(file);
							suite.ToXml(writer);
							writer.Close();
						}
					}
										
					IDictionary bmps = chart.Render(suite);
					foreach(DictionaryEntry de in bmps)
					{
						PerfTest test = (PerfTest)de.Key;
						Bitmap bmp = (Bitmap)de.Value;
						bmp.Save(tester.TestedType.Name + "." + test.Name + "." + im.ToString().ToLower(),im);
					}
				}
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}
		}
	}
}

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