95 lines
2.7 KiB
C#
95 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Volian.Base.Library
|
|
{
|
|
/// <summary>
|
|
/// VolianTimer Class - Times from Open to Close
|
|
/// Stores the results in a static list
|
|
/// </summary>
|
|
public class VolianTimer
|
|
{
|
|
#pragma warning disable S6669
|
|
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
#pragma warning restore S6669
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
|
|
static List<VolianTimer> _Timers = new List<VolianTimer>();
|
|
|
|
public string Name { get; set; }
|
|
public DateTime Start { get; set; }
|
|
public long Ticks { get; set; }
|
|
public int Count { get; set; }
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
public VolianTimer()
|
|
{
|
|
Count = 0;
|
|
Ticks = 0;
|
|
}
|
|
/// <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>
|
|
/// 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>
|
|
/// 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();
|
|
}
|
|
}
|
|
}
|