|
|
@ -7,29 +7,29 @@ namespace StabilityMatrix.Core.Helper; |
|
|
|
public class CodeTimer : IDisposable |
|
|
|
public class CodeTimer : IDisposable |
|
|
|
{ |
|
|
|
{ |
|
|
|
private static readonly Stack<CodeTimer> RunningTimers = new(); |
|
|
|
private static readonly Stack<CodeTimer> RunningTimers = new(); |
|
|
|
|
|
|
|
|
|
|
|
private readonly string name; |
|
|
|
private readonly string name; |
|
|
|
private readonly Stopwatch stopwatch; |
|
|
|
private readonly Stopwatch stopwatch; |
|
|
|
|
|
|
|
|
|
|
|
private CodeTimer? ParentTimer { get; } |
|
|
|
private CodeTimer? ParentTimer { get; } |
|
|
|
private List<CodeTimer> SubTimers { get; } = new(); |
|
|
|
private List<CodeTimer> SubTimers { get; } = new(); |
|
|
|
|
|
|
|
|
|
|
|
public CodeTimer([CallerMemberName] string? name = null) |
|
|
|
public CodeTimer(string postFix = "", [CallerMemberName] string callerName = "") |
|
|
|
{ |
|
|
|
{ |
|
|
|
this.name = name ?? ""; |
|
|
|
name = $"{callerName}" + (string.IsNullOrEmpty(postFix) ? "" : $" ({postFix})"); |
|
|
|
stopwatch = Stopwatch.StartNew(); |
|
|
|
stopwatch = Stopwatch.StartNew(); |
|
|
|
|
|
|
|
|
|
|
|
// Set parent as the top of the stack |
|
|
|
// Set parent as the top of the stack |
|
|
|
if (RunningTimers.TryPeek(out var timer)) |
|
|
|
if (RunningTimers.TryPeek(out var timer)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
ParentTimer = timer; |
|
|
|
ParentTimer = timer; |
|
|
|
timer.SubTimers.Add(this); |
|
|
|
timer.SubTimers.Add(this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Add ourselves to the stack |
|
|
|
// Add ourselves to the stack |
|
|
|
RunningTimers.Push(this); |
|
|
|
RunningTimers.Push(this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// Formats a TimeSpan into a string. Chooses the most appropriate unit of time. |
|
|
|
/// Formats a TimeSpan into a string. Chooses the most appropriate unit of time. |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
@ -64,22 +64,22 @@ public class CodeTimer : IDisposable |
|
|
|
private string GetResult() |
|
|
|
private string GetResult() |
|
|
|
{ |
|
|
|
{ |
|
|
|
var builder = new StringBuilder(); |
|
|
|
var builder = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
builder.AppendLine($"{name}: took {FormatTime(stopwatch.Elapsed)}"); |
|
|
|
builder.AppendLine($"{name}: took {FormatTime(stopwatch.Elapsed)}"); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var timer in SubTimers) |
|
|
|
foreach (var timer in SubTimers) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// For each sub timer layer, add a `|-` prefix |
|
|
|
// For each sub timer layer, add a `|-` prefix |
|
|
|
builder.AppendLine($"|- {timer.GetResult()}"); |
|
|
|
builder.AppendLine($"|- {timer.GetResult()}"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return builder.ToString(); |
|
|
|
return builder.ToString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
{ |
|
|
|
stopwatch.Stop(); |
|
|
|
stopwatch.Stop(); |
|
|
|
|
|
|
|
|
|
|
|
// Remove ourselves from the stack |
|
|
|
// Remove ourselves from the stack |
|
|
|
if (RunningTimers.TryPop(out var timer)) |
|
|
|
if (RunningTimers.TryPop(out var timer)) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -92,14 +92,14 @@ public class CodeTimer : IDisposable |
|
|
|
{ |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("Timer stack is empty"); |
|
|
|
throw new InvalidOperationException("Timer stack is empty"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// If we're a root timer, output all results |
|
|
|
// If we're a root timer, output all results |
|
|
|
if (ParentTimer is null) |
|
|
|
if (ParentTimer is null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
OutputDebug(GetResult()); |
|
|
|
OutputDebug(GetResult()); |
|
|
|
SubTimers.Clear(); |
|
|
|
SubTimers.Clear(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
GC.SuppressFinalize(this); |
|
|
|
GC.SuppressFinalize(this); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|