mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
feat: Indenting stopwatch results into a tree format
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
using ICD.Common.Utils.Extensions;
|
using ICD.Common.Utils.Extensions;
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
@@ -18,6 +19,8 @@ namespace ICD.Common.Utils.Timers
|
|||||||
|
|
||||||
private readonly Stopwatch m_Stopwatch;
|
private readonly Stopwatch m_Stopwatch;
|
||||||
|
|
||||||
|
private static int s_ProfileIndentCount;
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
public long ElapsedTicks { get { return m_Stopwatch.ElapsedTicks; } }
|
public long ElapsedTicks { get { return m_Stopwatch.ElapsedTicks; } }
|
||||||
@@ -104,10 +107,19 @@ namespace ICD.Common.Utils.Timers
|
|||||||
if (action == null)
|
if (action == null)
|
||||||
throw new ArgumentNullException("action");
|
throw new ArgumentNullException("action");
|
||||||
|
|
||||||
|
s_ProfileIndentCount++;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
IcdStopwatch stopwatch = StartNew();
|
IcdStopwatch stopwatch = StartNew();
|
||||||
action();
|
action();
|
||||||
PrintProfile(stopwatch.ElapsedTicks, name);
|
PrintProfile(stopwatch.ElapsedTicks, name);
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
s_ProfileIndentCount--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Executes the function and prints the duration to the console.
|
/// Executes the function and prints the duration to the console.
|
||||||
@@ -122,12 +134,21 @@ namespace ICD.Common.Utils.Timers
|
|||||||
if (func == null)
|
if (func == null)
|
||||||
throw new ArgumentNullException("func");
|
throw new ArgumentNullException("func");
|
||||||
|
|
||||||
|
s_ProfileIndentCount++;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
IcdStopwatch stopwatch = StartNew();
|
IcdStopwatch stopwatch = StartNew();
|
||||||
T output = func();
|
T output = func();
|
||||||
PrintProfile(stopwatch.ElapsedTicks, name);
|
PrintProfile(stopwatch.ElapsedTicks, name);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
s_ProfileIndentCount--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Executes the action the given number of iterations and prints timing information to the console.
|
/// Executes the action the given number of iterations and prints timing information to the console.
|
||||||
@@ -192,12 +213,16 @@ namespace ICD.Common.Utils.Timers
|
|||||||
|
|
||||||
// TODO - Print a fancy table with a total duration for the sequence
|
// TODO - Print a fancy table with a total duration for the sequence
|
||||||
|
|
||||||
|
List<T> output = new List<T>();
|
||||||
|
|
||||||
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
|
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
|
||||||
{
|
{
|
||||||
// ReSharper disable once AccessToDisposedClosure
|
// ReSharper disable once AccessToDisposedClosure
|
||||||
while (Profile(() => enumerator.MoveNext(), name))
|
while (Profile(() => enumerator.MoveNext(), name))
|
||||||
yield return enumerator.Current;
|
output.Add(enumerator.Current);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -207,18 +232,25 @@ namespace ICD.Common.Utils.Timers
|
|||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="name"></param>
|
/// <param name="name"></param>
|
||||||
public static void Profile(EventHandler eventHandler, object sender, string name)
|
public static void Profile(EventHandler eventHandler, object sender, string name)
|
||||||
|
{
|
||||||
|
s_ProfileIndentCount++;
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (eventHandler == null)
|
if (eventHandler == null)
|
||||||
{
|
{
|
||||||
PrintProfile(0, string.Format("{0} - No invocations", name));
|
PrintProfile(0, string.Format("{0} - No invocations", name));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
s_ProfileIndentCount--;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO - Print a fancy table with a total duration for the event
|
// TODO - Print a fancy table with a total duration for the event
|
||||||
|
|
||||||
// ReSharper disable PossibleInvalidCastExceptionInForeachLoop
|
foreach (EventHandler subscriber in eventHandler.GetInvocationList().Cast<EventHandler>())
|
||||||
foreach (EventHandler subscriber in eventHandler.GetInvocationList())
|
|
||||||
// ReSharper restore PossibleInvalidCastExceptionInForeachLoop
|
|
||||||
{
|
{
|
||||||
string subscriberName = string.Format("{0} - {1}.{2}", name, subscriber.Target.GetType().Name,
|
string subscriberName = string.Format("{0} - {1}.{2}", name, subscriber.Target.GetType().Name,
|
||||||
subscriber.GetMethodInfo().GetSignature(true));
|
subscriber.GetMethodInfo().GetSignature(true));
|
||||||
@@ -238,18 +270,25 @@ namespace ICD.Common.Utils.Timers
|
|||||||
/// <param name="name"></param>
|
/// <param name="name"></param>
|
||||||
public static void Profile<T>(EventHandler<T> eventHandler, object sender, T eventArgs, string name)
|
public static void Profile<T>(EventHandler<T> eventHandler, object sender, T eventArgs, string name)
|
||||||
where T : EventArgs
|
where T : EventArgs
|
||||||
|
{
|
||||||
|
s_ProfileIndentCount++;
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (eventHandler == null)
|
if (eventHandler == null)
|
||||||
{
|
{
|
||||||
PrintProfile(0, string.Format("{0} - No invocations", name));
|
PrintProfile(0, string.Format("{0} - No invocations", name));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
s_ProfileIndentCount--;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO - Print a fancy table with a total duration for the event
|
// TODO - Print a fancy table with a total duration for the event
|
||||||
|
|
||||||
// ReSharper disable PossibleInvalidCastExceptionInForeachLoop
|
foreach (EventHandler<T> subscriber in eventHandler.GetInvocationList().Cast<EventHandler<T>>())
|
||||||
foreach (EventHandler<T> subscriber in eventHandler.GetInvocationList())
|
|
||||||
// ReSharper restore PossibleInvalidCastExceptionInForeachLoop
|
|
||||||
{
|
{
|
||||||
string subscriberName = string.Format("{0} - {1}.{2}", name, subscriber.Target.GetType().Name,
|
string subscriberName = string.Format("{0} - {1}.{2}", name, subscriber.Target.GetType().Name,
|
||||||
subscriber.GetMethodInfo().GetSignature(true));
|
subscriber.GetMethodInfo().GetSignature(true));
|
||||||
@@ -269,6 +308,9 @@ namespace ICD.Common.Utils.Timers
|
|||||||
if (elapsed >= RED_MILLISECONDS)
|
if (elapsed >= RED_MILLISECONDS)
|
||||||
color = eConsoleColor.Red;
|
color = eConsoleColor.Red;
|
||||||
|
|
||||||
|
if (s_ProfileIndentCount > 0)
|
||||||
|
IcdConsole.Print(StringUtils.Repeat(" ", s_ProfileIndentCount - 1));
|
||||||
|
|
||||||
IcdConsole.Print(color, "{0:n}ms", elapsed);
|
IcdConsole.Print(color, "{0:n}ms", elapsed);
|
||||||
IcdConsole.PrintLine(" to execute {0}", name);
|
IcdConsole.PrintLine(" to execute {0}", name);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user