actually do profiler samples

This commit is contained in:
JohnCorby 2025-01-29 14:01:14 -08:00
parent d551944207
commit 40621c62b0

View File

@ -1,7 +1,10 @@
using HarmonyLib; // #define ENABLE_PROFILER
using HarmonyLib;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
using UnityEngine.Profiling;
namespace NewHorizons.Patches; namespace NewHorizons.Patches;
@ -31,8 +34,10 @@ public static class ProfilerPatch
} }
[HarmonyPrefix] [HarmonyPrefix]
public static void Prefix(out Stopwatch __state) public static void Prefix(MethodBase __originalMethod, out Stopwatch __state)
{ {
Profiler.BeginSample($"{__originalMethod.DeclaringType.Name}.{__originalMethod.Name}");
__state = new Stopwatch(); __state = new Stopwatch();
__state.Start(); __state.Start();
} }
@ -40,6 +45,8 @@ public static class ProfilerPatch
[HarmonyPostfix] [HarmonyPostfix]
public static void Postfix(MethodBase __originalMethod, Stopwatch __state) public static void Postfix(MethodBase __originalMethod, Stopwatch __state)
{ {
Profiler.EndSample();
__state.Stop(); __state.Stop();
Main.Instance.ModHelper.Console.WriteLine($"[profiler] method {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds} ms"); Main.Instance.ModHelper.Console.WriteLine($"[profiler] method {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds} ms");
} }