From 3883ae08aead9d2854eb749250fe4f3c342c2f46 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Sun, 24 Dec 2023 12:48:23 +0500 Subject: [PATCH] fix: do not subtract nested events from unprofiled Signed-off-by: Octol1ttle --- src/Services/Profiler/Profiler.cs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Services/Profiler/Profiler.cs b/src/Services/Profiler/Profiler.cs index 8e34540..2cd4dd8 100644 --- a/src/Services/Profiler/Profiler.cs +++ b/src/Services/Profiler/Profiler.cs @@ -27,17 +27,17 @@ public sealed class Profiler /// The ID of the event. public void Push(string id) { - _runningStopwatches++; _events.Add(new ProfilerEvent { Id = id, Stopwatch = Stopwatch.StartNew(), NestingLevel = _runningStopwatches - 1 }); + _runningStopwatches++; } /// - /// Pops the last pushed event from the profiler. + /// Pops the last running event from the profiler. /// /// Thrown if the profiler contains no events. public void Pop() @@ -47,10 +47,15 @@ public sealed class Profiler throw new InvalidOperationException("Nothing to pop"); } - _runningStopwatches--; _events.FindLast(item => item.Stopwatch.IsRunning).Stopwatch.Stop(); + _runningStopwatches--; } + /// + /// on the profiler and return a . + /// + /// The result to be returned. + /// The original result. public Result PopWithResult(Result result) { Pop(); @@ -60,7 +65,6 @@ public sealed class Profiler /// /// If the profiler took too long to execute, this will log a warning with per-event time usage /// - /// Thrown if there are stopwatches still running. private void Report() { var main = _events[0]; @@ -76,7 +80,10 @@ public sealed class Profiler var profilerEvent = _events[i]; builder.Append(' ', profilerEvent.NestingLevel * 4) .AppendLine($"{profilerEvent.Id}: {profilerEvent.Stopwatch.ElapsedMilliseconds}ms"); - unprofiled -= profilerEvent.Stopwatch.ElapsedMilliseconds; + if (profilerEvent.NestingLevel is 0) + { + unprofiled -= profilerEvent.Stopwatch.ElapsedMilliseconds; + } } if (unprofiled > 0) @@ -89,7 +96,7 @@ public sealed class Profiler } /// - /// the profiler and on it afterwards. + /// all running events in the profiler and on it afterwards. /// private void PopAndReport() { @@ -104,8 +111,8 @@ public sealed class Profiler /// /// on the profiler and return a . /// - /// - /// + /// The result to be returned. + /// The original result. public Result ReportWithResult(Result result) { PopAndReport();