Dalamud/Dalamud/Utility/HitchDetector.cs
goat 448b0d16ea
Add "loading dialog" for service init, unify blocking logic (#1779)
* wip

* hacky fix for overlapping event text in profiler

* move IsResumeGameAfterPluginLoad logic to PluginManager

* fix some warnings

* handle exceptions properly

* remove ability to cancel, rename button to "hide" instead

* undo Dalamud.Service refactor for now

* warnings

* add explainer, show which plugins are still loading

* add some text if loading takes more than 3 minutes

* undo wrong CS merge
2024-04-21 17:28:37 +02:00

55 lines
1.6 KiB
C#

using System.Diagnostics;
using Serilog;
namespace Dalamud.Utility;
/// <summary>
/// Utility class to detect hitches.
/// </summary>
public class HitchDetector
{
private readonly TimeSpan cooldownTime = TimeSpan.FromSeconds(30);
private readonly string name;
private readonly double millisecondsMax;
private DateTime lastTriggeredTime;
private Stopwatch stopwatch = new();
/// <summary>
/// Initializes a new instance of the <see cref="HitchDetector"/> class.
/// </summary>
/// <param name="name">Name to log.</param>
/// <param name="millisecondsMax">Milliseconds to print a warning for.</param>
public HitchDetector(string name, double millisecondsMax = 20)
{
this.name = name;
this.millisecondsMax = millisecondsMax;
this.lastTriggeredTime = DateTime.Now - this.cooldownTime;
}
/// <summary>
/// Start the time tracking.
/// </summary>
public void Start()
{
this.stopwatch.Restart();
}
/// <summary>
/// Stop the time tracking, and print a warning, if applicable.
/// </summary>
public void Stop()
{
this.stopwatch.Stop();
if (this.stopwatch.Elapsed.TotalMilliseconds > this.millisecondsMax &&
DateTime.Now - this.lastTriggeredTime > this.cooldownTime)
{
Log.Warning("[HITCH] Long {Name} detected, {Total}ms > {Max}ms - check in the plugin stats window.", this.name, this.stopwatch.Elapsed.TotalMilliseconds, this.millisecondsMax);
this.lastTriggeredTime = DateTime.Now;
}
}
}