From e01e41c44997002bdb360c900c0c00a0345bfba9 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 8 Oct 2018 14:54:16 -0400 Subject: [PATCH] feat: Indenting stopwatch results into a tree format --- ICD.Common.Utils/Timers/IcdStopwatch.cs | 86 ++++++++++++++++++------- 1 file changed, 64 insertions(+), 22 deletions(-) diff --git a/ICD.Common.Utils/Timers/IcdStopwatch.cs b/ICD.Common.Utils/Timers/IcdStopwatch.cs index 8e0e6d0..006e666 100644 --- a/ICD.Common.Utils/Timers/IcdStopwatch.cs +++ b/ICD.Common.Utils/Timers/IcdStopwatch.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using ICD.Common.Properties; using ICD.Common.Utils.Extensions; #if SIMPLSHARP @@ -18,6 +19,8 @@ namespace ICD.Common.Utils.Timers private readonly Stopwatch m_Stopwatch; + private static int s_ProfileIndentCount; + #region Properties public long ElapsedTicks { get { return m_Stopwatch.ElapsedTicks; } } @@ -104,9 +107,18 @@ namespace ICD.Common.Utils.Timers if (action == null) throw new ArgumentNullException("action"); - IcdStopwatch stopwatch = StartNew(); - action(); - PrintProfile(stopwatch.ElapsedTicks, name); + s_ProfileIndentCount++; + + try + { + IcdStopwatch stopwatch = StartNew(); + action(); + PrintProfile(stopwatch.ElapsedTicks, name); + } + finally + { + s_ProfileIndentCount--; + } } /// @@ -122,11 +134,20 @@ namespace ICD.Common.Utils.Timers if (func == null) throw new ArgumentNullException("func"); - IcdStopwatch stopwatch = StartNew(); - T output = func(); - PrintProfile(stopwatch.ElapsedTicks, name); + s_ProfileIndentCount++; - return output; + try + { + IcdStopwatch stopwatch = StartNew(); + T output = func(); + PrintProfile(stopwatch.ElapsedTicks, name); + + return output; + } + finally + { + s_ProfileIndentCount--; + } } /// @@ -192,12 +213,16 @@ namespace ICD.Common.Utils.Timers // TODO - Print a fancy table with a total duration for the sequence + List output = new List(); + using (IEnumerator enumerator = enumerable.GetEnumerator()) { // ReSharper disable once AccessToDisposedClosure while (Profile(() => enumerator.MoveNext(), name)) - yield return enumerator.Current; + output.Add(enumerator.Current); } + + return output; } /// @@ -208,20 +233,27 @@ namespace ICD.Common.Utils.Timers /// public static void Profile(EventHandler eventHandler, object sender, string name) { - if (eventHandler == null) + s_ProfileIndentCount++; + + try { - PrintProfile(0, string.Format("{0} - No invocations", name)); - return; + if (eventHandler == null) + { + PrintProfile(0, string.Format("{0} - No invocations", name)); + return; + } + } + finally + { + s_ProfileIndentCount--; } // TODO - Print a fancy table with a total duration for the event -// ReSharper disable PossibleInvalidCastExceptionInForeachLoop - foreach (EventHandler subscriber in eventHandler.GetInvocationList()) -// ReSharper restore PossibleInvalidCastExceptionInForeachLoop + foreach (EventHandler subscriber in eventHandler.GetInvocationList().Cast()) { string subscriberName = string.Format("{0} - {1}.{2}", name, subscriber.Target.GetType().Name, - subscriber.GetMethodInfo().GetSignature(true)); + subscriber.GetMethodInfo().GetSignature(true)); EventHandler subscriber1 = subscriber; Profile(() => subscriber1(sender, EventArgs.Empty), subscriberName); @@ -239,20 +271,27 @@ namespace ICD.Common.Utils.Timers public static void Profile(EventHandler eventHandler, object sender, T eventArgs, string name) where T : EventArgs { - if (eventHandler == null) + s_ProfileIndentCount++; + + try { - PrintProfile(0, string.Format("{0} - No invocations", name)); - return; + if (eventHandler == null) + { + PrintProfile(0, string.Format("{0} - No invocations", name)); + return; + } + } + finally + { + s_ProfileIndentCount--; } // TODO - Print a fancy table with a total duration for the event -// ReSharper disable PossibleInvalidCastExceptionInForeachLoop - foreach (EventHandler subscriber in eventHandler.GetInvocationList()) -// ReSharper restore PossibleInvalidCastExceptionInForeachLoop + foreach (EventHandler subscriber in eventHandler.GetInvocationList().Cast>()) { string subscriberName = string.Format("{0} - {1}.{2}", name, subscriber.Target.GetType().Name, - subscriber.GetMethodInfo().GetSignature(true)); + subscriber.GetMethodInfo().GetSignature(true)); EventHandler subscriber1 = subscriber; Profile(() => subscriber1(sender, eventArgs), subscriberName); @@ -269,6 +308,9 @@ namespace ICD.Common.Utils.Timers if (elapsed >= RED_MILLISECONDS) color = eConsoleColor.Red; + if (s_ProfileIndentCount > 0) + IcdConsole.Print(StringUtils.Repeat(" ", s_ProfileIndentCount - 1)); + IcdConsole.Print(color, "{0:n}ms", elapsed); IcdConsole.PrintLine(" to execute {0}", name); }