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

NPerf, A Performance Benchmark Framework for .NET

, 25 Jan 2004
NPerf is a framework for benchmarking classes and methods, that tastes like NUnit.
nperf_bin.zip
Release
NPerf.Cons.exe
NPerf.Core.dll
NPerf.Framework.chm
NPerf.Framework.dll
NPerf.Report.dll
scpl.dll
System.Perf.dll
nperf_demo.zip
Releases
Unpackaged
nperf-1.1-Release
nperf
bin
Release
nperf-doc-1.1.chm
NPerf.Cons.exe
NPerf.Core.dll
NPerf.Framework.dll
NPerf.Report.dll
scpl.dll
System.Perf.dll
nperf_src.zip
src
build
NAnt
i18n.xsl
ICSharpCode.SharpCvsLib.dll
ICSharpCode.SharpZipLib.dll
log4net.dll
NAnt.Contrib.Tasks.dll
NAnt.Core.dll
NAnt.DotNetTasks.dll
NAnt.exe
NAnt.NUnit.dll
NAnt.NUnit1Tasks.dll
NAnt.NUnit2ReportTasks.dll
NAnt.NUnit2Tasks.dll
NAnt.SourceControlTasks.dll
NAnt.VisualCppTasks.dll
NAnt.VSNetTasks.dll
NAnt.Win32Tasks.dll
NAnt.ZipTasks.dll
NDoc.Core.dll
NDoc.Documenter.Msdn.dll
NDoc.Documenter.NAnt.dll
NUnit-Frame.xsl
NUnit-NoFrame.xsl
nunit.framework.dll
NUnitCore.dll
toolkit.xsl
NPerf.Cons
Collections
App.ico
NPerf.Cons.cmbx
NPerf.Cons.prjx
NPerf.Core
Collections
Monitoring
Tracers
NPerf.Core.cmbx
NPerf.Core.prjx
NPerf.Framework
NPerf.Framework.cmbx
NPerf.Framework.prjx
NPerf.Report
scpl
scpl.dll
System.Perf
NPerf.Framework.chm
nperf.ndoc
nperf.png
/*
* Arguments class: application arguments interpreter
*
* Authors:		R. LOPES
* Contributors:	R. LOPES
* Created:		25 October 2002
* Modified:		28 October 2002
*
* Version:		1.0
*/

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.IO;

namespace NPerf.Cons
{
	using NPerf.Cons.Collections;

	public class ClParameter
	{
		private string name;
		private string shortName;
		private string description;
		private bool unique;
		private bool alone;
		
		public ClParameter(string name, string shortName,string description, bool unique, bool alone)
		{
			if (name==null)
				throw new ArgumentNullException("name");
			if (shortName==null)
				throw new ArgumentNullException("shortName");
			if (description==null)
				throw new ArgumentNullException("description");

			this.unique = unique;
			this.alone = alone;
			this.name = name;
			this.shortName = shortName;
			this.description = description;
		}
		
		public bool IsUnique
		{
			get
			{
				return this.unique || this.alone;
			}
		}
		
		public bool IsAlone
		{
			get
			{
				return  this.alone;
			}
		}
		
		public string Name
		{
			get
			{
				return this.name;
			}
		}
		
		public string ShortName
		{
			get
			{
				return this.shortName;
			}
		}
		
		public string Description
		{
			get
			{
				return this.description;
			}
		}
		
		public string ToHelpString()
		{
			return String.Format("--{0} -{1}, {2} {3} {4}",
				this.Name,
				this.ShortName,
				this.Description,
				this.IsUnique ?"unique": "multiple",
				this.IsAlone ? "alone": "with value"
				);
		}
	}
	
	/// <summary>
	/// Arguments class
	/// </summary>
	public class ClArguments
	{
		// Variables
		private ClParameterDictionary parameters;
		private StringDictionary uniqueParameters;
		private StringStringCollectionDictionary duplicateParameters;

		public ClArguments()
		{
			this.parameters = new ClParameterDictionary();	
			this.uniqueParameters = new StringDictionary();
			this.duplicateParameters = new StringStringCollectionDictionary();
			
			AddParameter(new ClParameter("help","h","Show this help",true,true));
		}
		
		public void AddParameter(ClParameter param)
		{
			this.parameters.Add(param);
		}
		
		public string ToHelpString()
		{
			StringWriter sw =  new StringWriter();
			
			sw.WriteLine("Command Line Parameter Help");
			sw.WriteLine("---------------------------");
			sw.WriteLine("Parameters can be called by setting -- with their full name,");
			sw.WriteLine("or - and their short name:");
			sw.WriteLine("   --help or -h");
			sw.WriteLine("Parameters can be followed by a value:");
			sw.WriteLine("   --input data");
			sw.WriteLine();
			sw.WriteLine("Name\tShortName\tDescription\tUnique\tAlone");
			foreach(DictionaryEntry de in this.parameters.Parameters)
			{
				ClParameter p = (ClParameter)de.Value;
				sw.WriteLine(p.ToHelpString());
			}
			
			return sw.ToString();
		}
		
		public void Parse(string[] Args)
		{
			try
			{
				Regex spliter=new Regex(
					@"^-{1,2}|^/|=|:",
					RegexOptions.IgnoreCase|RegexOptions.Compiled
					);
				Regex remover= new Regex(
					@"^['""]?(.*?)['""]?$",
					RegexOptions.IgnoreCase|RegexOptions.Compiled
					);
				string parameter=null;
				string[] parts;

				foreach(string Txt in Args)
				{
					// Look for new parameters (-,/ or --) and a possible enclosed value (=,:)
					parts=spliter.Split(Txt,3);
					switch(parts.Length)
					{
							// Found a value (for the last parameter found (space separator))
						case 1:
							if(parameter!=null)
							{
								if (CheckParameter(parameter))
								{
									parts[0]=remover.Replace(parts[0],"$1");								
									AddPair(parameter,parts[0]);
								}
								parameter=null;
							}
							// else Error: no parameter waiting for a value (skipped)
							break;
							// Found just a parameter
						case 2:
							// The last parameter is still waiting. With no value, set it to true.
							if(parameter!=null)
							{
								if (CheckParameter(parameter))
								{
									// add to unique, alone parameters
									AddUnique(parameter);								
								}
							}
							parameter=parts[1];
							break;
							// Parameter with enclosed value
						case 3:
							// The last parameter is still waiting. With no value, set it to true.
							if(parameter!=null)
							{
								AddUnique(parameter);
							}
							parameter=parts[1];
							// Remove possible enclosing characters (",')
							if(CheckParameter(parameter))
							{
								parts[2]=remover.Replace(parts[2],"$1");
								AddPair(parameter,parts[2]);
							}
							parameter=null;
							break;
					}
				}
				// In case a parameter is still waiting
				if(parameter!=null)
				{
					if(CheckParameter(parameter)) 
						AddUnique(parameter);
				}
			}
			catch(Exception)
			{
				Console.WriteLine(ToHelpString());
				throw;
			}
		}

		internal void AddPair(string key, string value)
		{
			ClParameter p = this.parameters[key];
			if (p.IsUnique)
			{
				if (this.uniqueParameters.ContainsKey(key))
					throw new Exception("Duplicate unique argument " + key);

				this.uniqueParameters.Add(p.Name,value.TrimEnd(' '));
				this.uniqueParameters.Add(p.ShortName,value.TrimEnd(' '));
			}
			else
			{
				if (!this.duplicateParameters.Contains(key))
				{
					this.duplicateParameters.Add(p.Name, new StringCollection());
					this.duplicateParameters.Add(p.ShortName, new StringCollection());
				}
			
				((StringCollection)this.duplicateParameters[p.Name]).Add(value.TrimEnd(' '));
				((StringCollection)this.duplicateParameters[p.ShortName]).Add(value.TrimEnd(' '));
			}
		}

		internal void AddUnique(string key)
		{
			if (this.uniqueParameters.ContainsKey(key))
				Console.WriteLine("Duplicate parameter: {0}", key);
			else
			{
				ClParameter p = this.parameters[key];
				this.uniqueParameters.Add(p.Name,"");
				this.uniqueParameters.Add(p.ShortName,"");
			}
		}
		
		internal bool CheckParameter(string key)
		{
			if (!this.parameters.Contains(key))
			{
				Console.WriteLine("Unknown parameter: {0}",key);
				return false;
			}
			return true;				
		}
		
		// Retrieve a parameter value if it exists
		public bool ContainsUnique(string key)
		{
			return this.uniqueParameters.ContainsKey(key);
		}
		
		public bool ContainsDuplicate(string key)
		{
			return this.duplicateParameters.Contains(key);
		}

		public String Unique(string key)
		{
			return this.uniqueParameters[key];
		}	

		public StringCollection Duplicates(string key)
		{
			return (StringCollection)this.duplicateParameters[key];
		}

		public void ProcessHelp()
		{
			if (this.uniqueParameters.ContainsKey("h"))
				Console.WriteLine(ToHelpString());
		}
	}
}

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

Jonathan de Halleux
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).

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 26 Jan 2004
Article Copyright 2004 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid