126 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			126 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| using System.Text;
 | |
| using System.Threading.Tasks;
 | |
| 
 | |
| namespace Volian.Base.Library
 | |
| {
 | |
| 	/// <summary>
 | |
| 	/// VolianTimer Class - Times from Open to Close
 | |
| 	/// Stores the results in a static list
 | |
| 	/// </summary>
 | |
| 	public class VolianTimer
 | |
| 	{
 | |
| 		private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
 | |
| 		static List<VolianTimer> _Timers = new List<VolianTimer>();
 | |
| 		/// <summary>
 | |
| 		/// User defined name - should be as specific as possible 
 | |
| 		/// Include Method Name, File Name and Line Number
 | |
| 		/// </summary>
 | |
| 		private string _Name;
 | |
| 		public string Name
 | |
| 		{
 | |
| 			get { return _Name; }
 | |
| 			set { _Name = value; }
 | |
| 		}
 | |
| 		/// <summary>
 | |
| 		/// Set on open
 | |
| 		/// </summary>
 | |
| 		private DateTime _Start;
 | |
| 		public DateTime Start
 | |
| 		{
 | |
| 			get { return _Start; }
 | |
| 			set { _Start = value; }
 | |
| 		}
 | |
| 		/// <summary>
 | |
| 		/// Calculate on Close
 | |
| 		/// </summary>
 | |
| 		private long _Ticks;
 | |
| 		public long Ticks
 | |
| 		{
 | |
| 			get { return _Ticks; }
 | |
| 			set { _Ticks = value; }
 | |
| 		}
 | |
| 		/// <summary>
 | |
| 		/// Number of times open/close has been run
 | |
| 		/// </summary>
 | |
| 		private int _Count;
 | |
| 		public int Count
 | |
| 		{
 | |
| 			get { return _Count; }
 | |
| 			set { _Count = value; }
 | |
| 		}
 | |
| 		/// <summary>
 | |
| 		/// Constructor
 | |
| 		/// </summary>
 | |
| 		public VolianTimer()
 | |
| 		{
 | |
| 			Count = 0;
 | |
| 			Ticks = 0;
 | |
| 		}
 | |
| 		/// <summary>
 | |
| 		/// Command Line Parameter /Timing turns timing on
 | |
| 		/// </summary>
 | |
| 		private static bool? _TimingsOn = null;
 | |
| 		public static bool TimingsOn
 | |
| 		{
 | |
| 			get 
 | |
| 			{
 | |
| 				if (_TimingsOn == null)
 | |
| 					_TimingsOn = Volian.Base.Library.VlnSettings.GetCommandFlag("Timing");
 | |
| 				return (bool) _TimingsOn; 
 | |
| 			}
 | |
| 		}
 | |
| 		/// <summary>
 | |
| 		/// Constructor with Module and line number
 | |
| 		/// </summary>
 | |
| 		/// <param name="module">Method Name, File Name</param>
 | |
| 		/// <param name="line">Code Line Number</param>
 | |
| 		public VolianTimer(string module, int line)
 | |
| 		{
 | |
| 			Count = 0;
 | |
| 			Ticks = 0;
 | |
| 			Name = string.Format("{0}:{1}", module, line);
 | |
| 			if(TimingsOn) _Timers.Add(this);
 | |
| 		}
 | |
| 		/// <summary>
 | |
| 		/// Start Timer
 | |
| 		/// </summary>
 | |
| 		public void Open()
 | |
| 		{
 | |
| 			Start = DateTime.Now;
 | |
| 			Count++;
 | |
| 		}
 | |
| 		/// <summary>
 | |
| 		/// Stop Timer record statistics
 | |
| 		/// </summary>
 | |
| 		public void Close()
 | |
| 		{
 | |
| 			DateTime end = DateTime.Now;
 | |
| 			TimeSpan ts = TimeSpan.FromTicks(end.Ticks - Start.Ticks);
 | |
| 			Ticks += ts.Ticks;
 | |
| 		}
 | |
| 		/// <summary>
 | |
| 		/// Show the results in the error log
 | |
| 		/// </summary>
 | |
| 		/// <returns></returns>
 | |
| 		public static string ShowTimers()
 | |
| 		{
 | |
| 			StringBuilder sb = new StringBuilder();
 | |
| 			if (TimingsOn)
 | |
| 			{
 | |
| 				sb.AppendLine(
 | |
| 					"===============================================\r\n" +
 | |
| 					"Count\tSeconds\tEvent\r\n" +
 | |
| 					"----------------------------------------------- ");
 | |
| 				foreach (VolianTimer tmr in _Timers)
 | |
| 					sb.AppendLine(string.Format("{0}\t{1:N1}\t{2}", tmr.Count, TimeSpan.FromTicks(tmr.Ticks).TotalSeconds, tmr.Name));
 | |
| 				sb.AppendLine("===============================================\r\n");
 | |
| 				_MyLog.InfoFormat(sb.ToString());
 | |
| 			}
 | |
| 			return sb.ToString();
 | |
| 		}
 | |
| 	}
 | |
| }
 | 
