Skip to content

Commit

Permalink
Added JSON / hydration profilers.
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Dec 7, 2024
1 parent 8dadee4 commit 6236e08
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<string, object>),
typeof(string),
typeof(Stopwatch),
typeof(Stopwatch),
typeof(JSONSerializationUtility.RehydrationFilteringMode),
typeof(Func<string, bool>[])
)]
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());
}
}
}
36 changes: 36 additions & 0 deletions ModTek/Features/Profiler/Patches/JSON_ToObject_Patch.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 6236e08

Please sign in to comment.