From 06a82513edf04a0e7fecd5a34daea5bfb90da6b7 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 1 Feb 2018 15:23:14 -0500 Subject: [PATCH] Adding profile shims to IcdStopwatch --- ICD.Common.Utils/ConsoleColor.cs | 108 ++++++++++++++++++ .../ICD.Common.Utils_SimplSharp.csproj | 1 + ICD.Common.Utils/IcdConsole.cs | 36 ++++++ ICD.Common.Utils/Timers/IcdStopwatch.cs | 65 ++++++++++- 4 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 ICD.Common.Utils/ConsoleColor.cs diff --git a/ICD.Common.Utils/ConsoleColor.cs b/ICD.Common.Utils/ConsoleColor.cs new file mode 100644 index 0000000..7035e50 --- /dev/null +++ b/ICD.Common.Utils/ConsoleColor.cs @@ -0,0 +1,108 @@ +using System; + +namespace ICD.Common.Utils +{ + public enum eConsoleColor + { + Red, + Green, + Yellow, + Blue, + Magenta, + Cyan, + White, + YellowOnRed + } + + public static class ConsoleColorExtensions + { + public const string CONSOLE_RED = "\x1B[31;1m"; + public const string CONSOLE_GREEN = "\x1B[32;1m"; + public const string CONSOLE_YELLOW = "\x1B[33;1m"; + public const string CONSOLE_BLUE = "\x1B[34;1m"; + public const string CONSOLE_MAGENTA = "\x1B[35;1m"; + public const string CONSOLE_CYAN = "\x1B[36;1m"; + public const string CONSOLE_WHITE = "\x1B[37;1m"; + public const string CONSOLE_YELLOW_ON_RED_BACKGROUND = "\x1B[93;41m"; + public const string CONSOLE_RESET = "\x1B[0m"; + + public static string FormatAnsi(this eConsoleColor extends, string data) + { + return string.Format("{0}{1}{2}", extends.ToAnsiPrefix(), data, CONSOLE_RESET); + } + + public static string ToAnsiPrefix(this eConsoleColor extends) + { + switch (extends) + { + case eConsoleColor.Red: + return CONSOLE_RED; + case eConsoleColor.Green: + return CONSOLE_GREEN; + case eConsoleColor.Yellow: + return CONSOLE_YELLOW; + case eConsoleColor.Blue: + return CONSOLE_BLUE; + case eConsoleColor.Magenta: + return CONSOLE_MAGENTA; + case eConsoleColor.Cyan: + return CONSOLE_CYAN; + case eConsoleColor.White: + return CONSOLE_WHITE; + case eConsoleColor.YellowOnRed: + return CONSOLE_YELLOW_ON_RED_BACKGROUND; + default: + throw new ArgumentOutOfRangeException("extends"); + } + } + +#if STANDARD + + public static ConsoleColor ToForegroundConsoleColor(this eConsoleColor extends) + { + switch (extends) + { + case eConsoleColor.Red: + return ConsoleColor.Red; + case eConsoleColor.Green: + return ConsoleColor.Green; + case eConsoleColor.Yellow: + case eConsoleColor.YellowOnRed: + return ConsoleColor.Yellow; + case eConsoleColor.Blue: + return ConsoleColor.Blue; + case eConsoleColor.Magenta: + return ConsoleColor.Magenta; + case eConsoleColor.Cyan: + return ConsoleColor.Cyan; + case eConsoleColor.White: + return ConsoleColor.White; + default: + throw new ArgumentOutOfRangeException("extends"); + } + } + + public static ConsoleColor ToBackgroundConsoleColor(this eConsoleColor extends) + { + switch (extends) + { + case eConsoleColor.Red: + case eConsoleColor.Green: + case eConsoleColor.Yellow: + case eConsoleColor.Blue: + case eConsoleColor.Magenta: + case eConsoleColor.Cyan: + case eConsoleColor.White: + return ConsoleColor.Black; + + case eConsoleColor.YellowOnRed: + return ConsoleColor.Red; + + default: + throw new ArgumentOutOfRangeException("extends"); + } + } + +#endif + } +} diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 5e604f1..bea8f4e 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -75,6 +75,7 @@ + diff --git a/ICD.Common.Utils/IcdConsole.cs b/ICD.Common.Utils/IcdConsole.cs index 2745f32..d9ebbf7 100644 --- a/ICD.Common.Utils/IcdConsole.cs +++ b/ICD.Common.Utils/IcdConsole.cs @@ -67,6 +67,24 @@ namespace ICD.Common.Utils PrintLine(message); } + public static void PrintLine(eConsoleColor color, string message) + { +#if SIMPLSHARP + PrintLine(color.FormatAnsi(message)); +#else + System.Console.ForegroundColor = color.ToForegroundConsoleColor(); + System.Console.BackgroundColor = color.ToBackgroundConsoleColor(); + System.Console.Error.WriteLine(message); + System.Console.ResetColor(); +#endif + } + + public static void PrintLine(eConsoleColor color, string message, params object[] args) + { + message = string.Format(message, args); + PrintLine(color, message); + } + public static void Print(string message) { #if SIMPLSHARP @@ -82,6 +100,24 @@ namespace ICD.Common.Utils Print(message); } + public static void Print(eConsoleColor color, string message) + { +#if SIMPLSHARP + Print(color.FormatAnsi(message)); +#else + System.Console.ForegroundColor = color.ToForegroundConsoleColor(); + System.Console.BackgroundColor = color.ToBackgroundConsoleColor(); + System.Console.Error.Write(message); + System.Console.ResetColor(); +#endif + } + + public static void Print(eConsoleColor color, string message, params object[] args) + { + message = string.Format(message, args); + Print(color, message); + } + public static bool SendControlSystemCommand(string command, ref string result) { #if SIMPLSHARP diff --git a/ICD.Common.Utils/Timers/IcdStopwatch.cs b/ICD.Common.Utils/Timers/IcdStopwatch.cs index 00b3421..d6d1810 100644 --- a/ICD.Common.Utils/Timers/IcdStopwatch.cs +++ b/ICD.Common.Utils/Timers/IcdStopwatch.cs @@ -1,4 +1,6 @@ -#if SIMPLSHARP +using System; +using ICD.Common.Properties; +#if SIMPLSHARP using Crestron.SimplSharp; #else using System.Diagnostics; @@ -8,6 +10,10 @@ namespace ICD.Common.Utils.Timers { public sealed class IcdStopwatch { + // Used with profiling + private const long YELLOW_MILLISECONDS = 100; + private const long RED_MILLISECONDS = 400; + private readonly Stopwatch m_Stopwatch; #region Properties @@ -80,5 +86,62 @@ namespace ICD.Common.Utils.Timers } #endregion + + #region Profiling + + /// + /// Executes the action and prints the duration to the console. + /// + /// + /// + [PublicAPI] + public static void Profile(Action action, string name) + { + if (action == null) + throw new ArgumentNullException("action"); + + IcdStopwatch stopwatch = StartNew(); + action(); + PrintProfile(stopwatch, name); + } + + /// + /// Executes the function and prints the duration to the console. + /// + /// + /// + /// + /// + [PublicAPI] + public static T Profile(Func func, string name) + { + if (func == null) + throw new ArgumentNullException("func"); + + T output = default(T); + + Profile(() => + { + output = func(); + }, name); + + return output; + } + + private static void PrintProfile(IcdStopwatch stopwatch, string name) + { + long elapsed = stopwatch.ElapsedMilliseconds; + + eConsoleColor color = eConsoleColor.Green; + if (elapsed >= YELLOW_MILLISECONDS) + color = eConsoleColor.Yellow; + if (elapsed >= RED_MILLISECONDS) + color = eConsoleColor.Red; + + IcdConsole.Print(color, "{0}ms", elapsed); + IcdConsole.PrintLine(" to execute {0}", name); + } + + #endregion } }