From 6236e087d25c1103a42554ecf709b8d8bede606f Mon Sep 17 00:00:00 2001 From: CptMoore <39010654+CptMoore@users.noreply.github.com> Date: Sun, 8 Dec 2024 00:58:09 +0100 Subject: [PATCH] Added JSON / hydration profilers. --- ...ity_RehydrateObjectFromDictionary_Patch.cs | 54 +++++++++++++++++++ .../Profiler/Patches/JSON_ToObject_Patch.cs | 36 +++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 ModTek/Features/Profiler/Patches/JSONSerializationUtility_RehydrateObjectFromDictionary_Patch.cs create mode 100644 ModTek/Features/Profiler/Patches/JSON_ToObject_Patch.cs diff --git a/ModTek/Features/Profiler/Patches/JSONSerializationUtility_RehydrateObjectFromDictionary_Patch.cs b/ModTek/Features/Profiler/Patches/JSONSerializationUtility_RehydrateObjectFromDictionary_Patch.cs new file mode 100644 index 0000000..ebb0ede --- /dev/null +++ b/ModTek/Features/Profiler/Patches/JSONSerializationUtility_RehydrateObjectFromDictionary_Patch.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using HBS; +using HBS.Util; +using ModTek.Features.Logging; + +namespace ModTek.Features.Profiler.Patches; + +[HarmonyPatch( + typeof(JSONSerializationUtility), + nameof(JSONSerializationUtility.RehydrateObjectFromDictionary), + typeof(object), + typeof(Dictionary), + typeof(string), + typeof(Stopwatch), + typeof(Stopwatch), + typeof(JSONSerializationUtility.RehydrationFilteringMode), + typeof(Func[]) +)] +internal static class JSONSerializationUtility_RehydrateObjectFromDictionary_Patch +{ + public static bool Prepare() + { + return ModTek.Enabled && ModTek.Config.ProfilerEnabled; + } + + private static readonly MTStopwatch s_stopwatch = new() + { + Callback = stats => + { + var id = "JSONSerializationUtility.RehydrateObjectFromDictionary"; + Log.Main.Trace?.Log($"{id} was called {stats.Count} times, taking a total of {stats.TotalTime} with an average of {stats.AverageNanoseconds}ns."); + }, + CallbackForEveryNumberOfMeasurements = 1000 + }; + + [HarmonyPriority(Priority.First)] + public static void Prefix(string classStructure, ref MTStopwatch.Tracker __state) + { + if (string.IsNullOrEmpty(classStructure)) + { + __state.Begin(); + } + } + + [HarmonyPriority(Priority.Last)] + public static void Postfix(string classStructure, ref MTStopwatch.Tracker __state) + { + if (string.IsNullOrEmpty(classStructure)) + { + s_stopwatch.AddMeasurement(__state.End()); + } + } +} \ No newline at end of file diff --git a/ModTek/Features/Profiler/Patches/JSON_ToObject_Patch.cs b/ModTek/Features/Profiler/Patches/JSON_ToObject_Patch.cs new file mode 100644 index 0000000..59049ab --- /dev/null +++ b/ModTek/Features/Profiler/Patches/JSON_ToObject_Patch.cs @@ -0,0 +1,36 @@ +using fastJSON; +using ModTek.Features.Logging; + +namespace ModTek.Features.Profiler.Patches; + +[HarmonyPatch(typeof(JSON), nameof(JSON.ToObject), typeof(string), typeof(bool))] +internal static class JSON_ToObject_Patch +{ + public static bool Prepare() + { + return ModTek.Enabled && ModTek.Config.ProfilerEnabled; + } + + private static readonly MTStopwatch s_stopwatch = new() + { + Callback = stats => + { + Log.Main.Trace?.Log( + $"JSON.ToObject called {stats.Count} times, taking a total of {stats.TotalTime} with an average of {stats.AverageNanoseconds}ns." + ); + }, + CallbackForEveryNumberOfMeasurements = 1000 + }; + + [HarmonyPriority(Priority.First)] + public static void Prefix(ref MTStopwatch.Tracker __state) + { + __state.Begin(); + } + + [HarmonyPriority(Priority.Last)] + public static void Postfix(ref MTStopwatch.Tracker __state) + { + s_stopwatch.AddMeasurement(__state.End()); + } +} \ No newline at end of file