diff --git a/Dalamud/Game/Framework.cs b/Dalamud/Game/Framework.cs index c801f85d4..2ba584ae1 100644 --- a/Dalamud/Game/Framework.cs +++ b/Dalamud/Game/Framework.cs @@ -25,6 +25,7 @@ namespace Dalamud.Game public sealed class Framework : IDisposable { private static Stopwatch statsStopwatch = new(); + private Stopwatch updateStopwatch = new(); private Hook updateHook; private Hook destroyHook; @@ -92,6 +93,21 @@ namespace Dalamud.Game /// public FrameworkAddressResolver Address { get; } + /// + /// Gets the last time that the Framework Update event was triggered. + /// + public DateTime LastUpdate { get; private set; } = DateTime.MinValue; + + /// + /// Gets the last time in UTC that the Framework Update event was triggered. + /// + public DateTime LastUpdateUTC { get; private set; } = DateTime.MinValue; + + /// + /// Gets the delta between the last Framework Update and the currently executing one. + /// + public TimeSpan UpdateDelta { get; private set; } = TimeSpan.Zero; + /// /// Gets or sets a value indicating whether to dispatch update events. /// @@ -127,6 +143,9 @@ namespace Dalamud.Game this.updateHook?.Dispose(); this.destroyHook?.Dispose(); this.realDestroyHook?.Dispose(); + + this.updateStopwatch.Reset(); + statsStopwatch.Reset(); } private void HookVTable() @@ -173,6 +192,13 @@ namespace Dalamud.Game if (this.DispatchUpdateEvents) { + this.updateStopwatch.Stop(); + this.UpdateDelta = TimeSpan.FromMilliseconds(this.updateStopwatch.ElapsedMilliseconds); + this.updateStopwatch.Restart(); + + this.LastUpdate = DateTime.Now; + this.LastUpdateUTC = DateTime.UtcNow; + try { if (StatsEnabled && this.Update != null) @@ -180,16 +206,23 @@ namespace Dalamud.Game // Stat Tracking for Framework Updates var invokeList = this.Update.GetInvocationList(); var notUpdated = StatsHistory.Keys.ToList(); + // Individually invoke OnUpdate handlers and time them. foreach (var d in invokeList) { statsStopwatch.Restart(); d.Method.Invoke(d.Target, new object[] { this }); statsStopwatch.Stop(); + var key = $"{d.Target}::{d.Method.Name}"; - if (notUpdated.Contains(key)) notUpdated.Remove(key); - if (!StatsHistory.ContainsKey(key)) StatsHistory.Add(key, new List()); + if (notUpdated.Contains(key)) + notUpdated.Remove(key); + + if (!StatsHistory.ContainsKey(key)) + StatsHistory.Add(key, new List()); + StatsHistory[key].Add(statsStopwatch.Elapsed.TotalMilliseconds); + if (StatsHistory[key].Count > 1000) { StatsHistory[key].RemoveRange(0, StatsHistory[key].Count - 1000); diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs index 40894ea25..9de7ae45b 100644 --- a/Dalamud/Plugin/DalamudPluginInterface.cs +++ b/Dalamud/Plugin/DalamudPluginInterface.cs @@ -99,7 +99,7 @@ namespace Dalamud.Plugin /// /// Gets the timespan delta from when this plugin was loaded. /// - public TimeSpan DeltaLoadTime => DateTime.Now - this.LoadTime; + public TimeSpan LoadTimeDelta => DateTime.Now - this.LoadTime; /// /// Gets the directory Dalamud assets are stored in.