Files
SourceCode/PROMS/Volian.Base.Library/VlnTimer.cs
T

172 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
namespace Volian.Base.Library
{
public class VlnTimer
{
public DateTime LastTime { get; set; } = DateTime.Now;
public string LastProcess { get; set; } = "Initialize";
public string ActiveProcess
{
get { return LastProcess; }
set
{
DateTime tNow = DateTime.Now;
TimeSpan ts = tNow - LastTime;
LastTime = tNow;
if (!ElapsedTimes.ContainsKey(LastProcess))
ElapsedTimes.Add(LastProcess, ts);
else
ElapsedTimes[LastProcess] += ts;
LastProcess = value;
}
}
public Dictionary<string, TimeSpan> ElapsedTimes { get; set; } = new Dictionary<string, TimeSpan>();
public void ShowElapsedTimes()
{
ActiveProcess = "fini";
Console.WriteLine("'Process'\t'Elapsed'");
foreach (string proc in ElapsedTimes.Keys)
Console.WriteLine("'{0}'\t{1}", proc, ElapsedTimes[proc].TotalSeconds);
}
public void ShowElapsedTimes(string title)
{
Console.WriteLine(title);
ShowElapsedTimes();
}
}
public static class ProfileTimer
{
#pragma warning disable S6669
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#pragma warning restore S6669
public static Stack<string> MyStack { get; set; } = new Stack<string>();
public static DateTime StartTime { get; set; } = DateTime.Now;
delegate int DoPushTrack(string start);
private static DoPushTrack myDoPush = new DoPushTrack(IgnorePush);
public static int Push(string start) => DoPush(start);
private static int DoPush(string start)
{
MyStack.Push(Start);
Start = start;
return MyStack.Count;
}
private static int IgnorePush(string start)
{
Start = start;
return 1;
}
delegate int DoPopTrack(int depth);
private static DoPopTrack myDoPop = new DoPopTrack(IgnorePop);
public static int Pop(int depth) => DoPop(depth);
private static int DoPop(int depth)
{
if (MyStack.Count != depth)
_MyLog.WarnFormat("Profile Stack Issues\r\n {0}, Depth is {1}, Depth should be {2}", Start,MyStack.Count,depth);
Start = MyStack.Pop();
return MyStack.Count;
}
private static int IgnorePop(int depth) => 0;
public static int Depth => MyStack.Count;
public static Dictionary<string, long> TimerTable { get; set; } = new Dictionary<string, long>();
delegate void DoTrack(string module);
private static DoTrack myDoTrack = new DoTrack(IgnoreModule);
public static void TurnOnTracking(string filename)
{
DebugProfile.Open($"{VlnSettings.TemporaryFolder}\\{filename}");
myDoTrack = new DoTrack(TrackModule);
myDoPush = new DoPushTrack(DoPush);
myDoPop = new DoPopTrack(DoPop);
}
public static void TurnOffTracking()
{
myDoTrack = new DoTrack(IgnoreModule);
myDoPush = new DoPushTrack(IgnorePush);
myDoPop = new DoPopTrack(IgnorePop);
}
private static string _Start = "Start";
public static string Start
{
get { return _Start; }
set
{
myDoTrack(value);
}
}
private static void TrackModule(string module)
{
DateTime dtNext = DateTime.Now;
//Console.WriteLine("{0},'{1}'", TimeSpan.FromTicks(dtNext.Ticks - LastTime.Ticks).TotalSeconds, Description);
AddTimerInfo(_Start, dtNext.Ticks - LastTime.Ticks);
_Start = module;
LastTime = dtNext;
}
private static void IgnoreModule(string module) => _Start = module;
private static void AddTimerInfo(string description, long ticks)
{
if (TimerTable.ContainsKey(description))
TimerTable[description] += ticks;
else
TimerTable.Add(description, ticks);
}
public static void ShowTimerTable()
{
if (DebugProfile.IsOpen)
{
if (MyStack.Count > 0)
{
DebugProfile.WriteLine("{0} Items on Stack", MyStack.Count);
Dictionary<string, int> stackCount = new Dictionary<string, int>();
foreach (string loc in MyStack)
{
if (!stackCount.ContainsKey(loc))
stackCount.Add(loc, 1);
else
stackCount[loc]++;
}
foreach (string stkey in stackCount.Keys)
DebugProfile.WriteLine("\"{0}\"\t{1}", stkey, stackCount[stkey]);
DebugProfile.WriteLine("-----------------------------------");
}
long total = 0;
foreach (long ticks in TimerTable.Values)
total += ticks;
SortedDictionary<long, List<string>> sList = new SortedDictionary<long, List<string>>();
foreach (string str in TimerTable.Keys)
{
long key = -TimerTable[str];
if (!sList.ContainsKey(key))
sList.Add(key, new List<string>());
sList[key].Add(str);
}
long limit = (95 * total) / 100;
foreach (long lkey in sList.Keys)
{
foreach (string str1 in sList[lkey])
{
TimeSpan secs = TimeSpan.FromTicks(-lkey);
if (limit > 0)
DebugProfile.WriteLine("\"{0}\"\t{1}\t{2}", str1, secs.TotalSeconds, (100 * secs.Ticks) / total);
limit += lkey;
}
}
DebugProfile.WriteLine("\"Total\"\t{0}", TimeSpan.FromTicks(total).TotalSeconds);
DebugProfile.Show();
}
}
public static void Reset()
{
TimerTable = new Dictionary<string, long>();
_Start = "Start";
LastTime = DateTime.Now;
MyStack = new Stack<string>();
}
public static DateTime LastTime { get; set; } = DateTime.Now;
}
}