From c9c69a7ed8dd47cce23bd9c8e82fafbaf940e297 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:47:41 +0200 Subject: [PATCH] Added Profiler for simple rough time measurement --- .../Additional-Popups/Animation/ChangeTile.cs | 12 +++--- .../ApplicationBuildInfo.cs | 0 .../ApplicationScope.cs | 6 +-- .../{Internals => Internal}/CommitInfo.cs | 0 PCK-Studio/Internal/Profiler.cs | 38 +++++++++++++++++++ .../SettingsManager.cs | 0 .../{Internals => Internal}/SkinANIM.cs | 0 PCK-Studio/{Internals => Internal}/SkinBOX.cs | 0 PCK-Studio/PckStudio.csproj | 13 ++++--- 9 files changed, 53 insertions(+), 16 deletions(-) rename PCK-Studio/{Internals => Internal}/ApplicationBuildInfo.cs (100%) rename PCK-Studio/{Internals => Internal}/ApplicationScope.cs (85%) rename PCK-Studio/{Internals => Internal}/CommitInfo.cs (100%) create mode 100644 PCK-Studio/Internal/Profiler.cs rename PCK-Studio/{Internals => Internal}/SettingsManager.cs (100%) rename PCK-Studio/{Internals => Internal}/SkinANIM.cs (100%) rename PCK-Studio/{Internals => Internal}/SkinBOX.cs (100%) diff --git a/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs b/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs index 2a9e8a2c..d0a6c36c 100644 --- a/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs +++ b/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Diagnostics; using System.Windows.Forms; using MetroFramework.Forms; -using Newtonsoft.Json.Linq; using PckStudio.Extensions; using PckStudio.Forms.Utilities; +using PckStudio.Internal; namespace PckStudio.Forms.Additional_Popups.Animation { @@ -30,11 +30,10 @@ namespace PckStudio.Forms.Additional_Popups.Animation private void InitializeTreeviews() { - Stopwatch stopwatch = Stopwatch.StartNew(); + Profiler.Start(); GetTileDataToView("blocks", treeViewBlocks.Nodes, treeViewBlockCache.Add); GetTileDataToView("items", treeViewItems.Nodes, treeViewItemCache.Add); - stopwatch.Stop(); - Debug.WriteLine($"{nameof(InitializeTreeviews)} took {stopwatch.ElapsedMilliseconds}ms"); + Profiler.Stop(); } private void treeViews_AfterSelect(object sender, TreeViewEventArgs e) @@ -57,7 +56,7 @@ namespace PckStudio.Forms.Additional_Popups.Animation "items" => AnimationResources.ItemTileInfos, _ => throw new InvalidOperationException(key) }; - Stopwatch stopwatch = Stopwatch.StartNew(); + Profiler.Start(); try { if (textureInfos is not null) @@ -83,8 +82,7 @@ namespace PckStudio.Forms.Additional_Popups.Animation MessageBox.Show(j_ex.Message, "Error"); return; } - stopwatch.Stop(); - Debug.WriteLine($"{nameof(GetTileDataToView)} took {stopwatch.ElapsedMilliseconds}ms", category: nameof(ChangeTile)); + Profiler.Stop(); } void filter_TextChanged(object sender, EventArgs e) diff --git a/PCK-Studio/Internals/ApplicationBuildInfo.cs b/PCK-Studio/Internal/ApplicationBuildInfo.cs similarity index 100% rename from PCK-Studio/Internals/ApplicationBuildInfo.cs rename to PCK-Studio/Internal/ApplicationBuildInfo.cs diff --git a/PCK-Studio/Internals/ApplicationScope.cs b/PCK-Studio/Internal/ApplicationScope.cs similarity index 85% rename from PCK-Studio/Internals/ApplicationScope.cs rename to PCK-Studio/Internal/ApplicationScope.cs index bdd24c51..70a14eb4 100644 --- a/PCK-Studio/Internals/ApplicationScope.cs +++ b/PCK-Studio/Internal/ApplicationScope.cs @@ -24,7 +24,8 @@ namespace PckStudio internal static void Initialize() { - Stopwatch stopwatch = Stopwatch.StartNew(); + Profiler.Configure(Debug.Listeners[0]); + Profiler.Start(); { _entityImages ??= Resources.entities_sheet.CreateImageList(32).ToArray(); DataCacher ??= new FileCacher(Program.AppDataCache); @@ -34,8 +35,7 @@ namespace PckStudio SettingsManager.Initialize(); CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; } - stopwatch.Stop(); - Debug.WriteLine($"{nameof(ApplicationScope.Initialize)} took {stopwatch.ElapsedMilliseconds}ms"); + Profiler.Stop(); } } } \ No newline at end of file diff --git a/PCK-Studio/Internals/CommitInfo.cs b/PCK-Studio/Internal/CommitInfo.cs similarity index 100% rename from PCK-Studio/Internals/CommitInfo.cs rename to PCK-Studio/Internal/CommitInfo.cs diff --git a/PCK-Studio/Internal/Profiler.cs b/PCK-Studio/Internal/Profiler.cs new file mode 100644 index 00000000..922f05af --- /dev/null +++ b/PCK-Studio/Internal/Profiler.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace PckStudio.Internal +{ + internal static class Profiler + { + private static Stopwatch _stopwatch = new Stopwatch(); + private static TraceListener _listener; + + [Conditional("DEBUG")] + internal static void Configure(TraceListener listener) + { + _listener = listener; + } + + [Conditional("DEBUG")] + internal static void Start([CallerMemberName] string caller = default!, [CallerFilePath] string source = default!, [CallerLineNumber] int line = default!) + { + _listener?.WriteLine($"Stopwatch starts", category: nameof(Profiler)); + _listener?.WriteLine($"{source}@{caller}:{line}", category: nameof(Profiler)); + _stopwatch.Restart(); + } + + [Conditional("DEBUG")] + internal static void Stop([CallerMemberName] string caller = default!, [CallerFilePath] string source = default!, [CallerLineNumber] int line = default!) + { + _stopwatch.Stop(); + _listener?.WriteLine($"{caller} took {_stopwatch.ElapsedMilliseconds}ms", category: nameof(Profiler)); + } + + } +} diff --git a/PCK-Studio/Internals/SettingsManager.cs b/PCK-Studio/Internal/SettingsManager.cs similarity index 100% rename from PCK-Studio/Internals/SettingsManager.cs rename to PCK-Studio/Internal/SettingsManager.cs diff --git a/PCK-Studio/Internals/SkinANIM.cs b/PCK-Studio/Internal/SkinANIM.cs similarity index 100% rename from PCK-Studio/Internals/SkinANIM.cs rename to PCK-Studio/Internal/SkinANIM.cs diff --git a/PCK-Studio/Internals/SkinBOX.cs b/PCK-Studio/Internal/SkinBOX.cs similarity index 100% rename from PCK-Studio/Internals/SkinBOX.cs rename to PCK-Studio/Internal/SkinBOX.cs diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index fb0d51b7..50456f78 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -195,7 +195,7 @@ AppSettingsForm.cs - + @@ -219,11 +219,12 @@ - - - + + + + - + @@ -261,7 +262,7 @@ WiiUPanel.cs - + Form