Merge pull request #547 from goatcorp/frameworkTimer

This commit is contained in:
goaaats 2021-09-09 00:33:10 +02:00 committed by GitHub
commit 0438baacc5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View file

@ -25,6 +25,7 @@ namespace Dalamud.Game
public sealed class Framework : IDisposable public sealed class Framework : IDisposable
{ {
private static Stopwatch statsStopwatch = new(); private static Stopwatch statsStopwatch = new();
private Stopwatch updateStopwatch = new();
private Hook<OnUpdateDetour> updateHook; private Hook<OnUpdateDetour> updateHook;
private Hook<OnDestroyDetour> destroyHook; private Hook<OnDestroyDetour> destroyHook;
@ -92,6 +93,21 @@ namespace Dalamud.Game
/// </summary> /// </summary>
public FrameworkAddressResolver Address { get; } public FrameworkAddressResolver Address { get; }
/// <summary>
/// Gets the last time that the Framework Update event was triggered.
/// </summary>
public DateTime LastUpdate { get; private set; } = DateTime.MinValue;
/// <summary>
/// Gets the last time in UTC that the Framework Update event was triggered.
/// </summary>
public DateTime LastUpdateUTC { get; private set; } = DateTime.MinValue;
/// <summary>
/// Gets the delta between the last Framework Update and the currently executing one.
/// </summary>
public TimeSpan UpdateDelta { get; private set; } = TimeSpan.Zero;
/// <summary> /// <summary>
/// Gets or sets a value indicating whether to dispatch update events. /// Gets or sets a value indicating whether to dispatch update events.
/// </summary> /// </summary>
@ -127,6 +143,9 @@ namespace Dalamud.Game
this.updateHook?.Dispose(); this.updateHook?.Dispose();
this.destroyHook?.Dispose(); this.destroyHook?.Dispose();
this.realDestroyHook?.Dispose(); this.realDestroyHook?.Dispose();
this.updateStopwatch.Reset();
statsStopwatch.Reset();
} }
private void HookVTable() private void HookVTable()
@ -173,6 +192,13 @@ namespace Dalamud.Game
if (this.DispatchUpdateEvents) 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 try
{ {
if (StatsEnabled && this.Update != null) if (StatsEnabled && this.Update != null)
@ -180,16 +206,23 @@ namespace Dalamud.Game
// Stat Tracking for Framework Updates // Stat Tracking for Framework Updates
var invokeList = this.Update.GetInvocationList(); var invokeList = this.Update.GetInvocationList();
var notUpdated = StatsHistory.Keys.ToList(); var notUpdated = StatsHistory.Keys.ToList();
// Individually invoke OnUpdate handlers and time them. // Individually invoke OnUpdate handlers and time them.
foreach (var d in invokeList) foreach (var d in invokeList)
{ {
statsStopwatch.Restart(); statsStopwatch.Restart();
d.Method.Invoke(d.Target, new object[] { this }); d.Method.Invoke(d.Target, new object[] { this });
statsStopwatch.Stop(); statsStopwatch.Stop();
var key = $"{d.Target}::{d.Method.Name}"; var key = $"{d.Target}::{d.Method.Name}";
if (notUpdated.Contains(key)) notUpdated.Remove(key); if (notUpdated.Contains(key))
if (!StatsHistory.ContainsKey(key)) StatsHistory.Add(key, new List<double>()); notUpdated.Remove(key);
if (!StatsHistory.ContainsKey(key))
StatsHistory.Add(key, new List<double>());
StatsHistory[key].Add(statsStopwatch.Elapsed.TotalMilliseconds); StatsHistory[key].Add(statsStopwatch.Elapsed.TotalMilliseconds);
if (StatsHistory[key].Count > 1000) if (StatsHistory[key].Count > 1000)
{ {
StatsHistory[key].RemoveRange(0, StatsHistory[key].Count - 1000); StatsHistory[key].RemoveRange(0, StatsHistory[key].Count - 1000);

View file

@ -99,7 +99,7 @@ namespace Dalamud.Plugin
/// <summary> /// <summary>
/// Gets the timespan delta from when this plugin was loaded. /// Gets the timespan delta from when this plugin was loaded.
/// </summary> /// </summary>
public TimeSpan DeltaLoadTime => DateTime.Now - this.LoadTime; public TimeSpan LoadTimeDelta => DateTime.Now - this.LoadTime;
/// <summary> /// <summary>
/// Gets the directory Dalamud assets are stored in. /// Gets the directory Dalamud assets are stored in.