-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...Features/Profiler/Patches/JSONSerializationUtility_RehydrateObjectFromDictionary_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |