1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-20 00:43:36 +03:00

fix: do not subtract nested events from unprofiled

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-12-24 12:48:23 +05:00
parent 0dc23400b9
commit 3883ae08ae
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF

View file

@ -27,17 +27,17 @@ public sealed class Profiler
/// <param name="id">The ID of the event.</param> /// <param name="id">The ID of the event.</param>
public void Push(string id) public void Push(string id)
{ {
_runningStopwatches++;
_events.Add(new ProfilerEvent _events.Add(new ProfilerEvent
{ {
Id = id, Id = id,
Stopwatch = Stopwatch.StartNew(), Stopwatch = Stopwatch.StartNew(),
NestingLevel = _runningStopwatches - 1 NestingLevel = _runningStopwatches - 1
}); });
_runningStopwatches++;
} }
/// <summary> /// <summary>
/// Pops the last pushed event from the profiler. /// Pops the last running event from the profiler.
/// </summary> /// </summary>
/// <exception cref="InvalidOperationException">Thrown if the profiler contains no events.</exception> /// <exception cref="InvalidOperationException">Thrown if the profiler contains no events.</exception>
public void Pop() public void Pop()
@ -47,10 +47,15 @@ public sealed class Profiler
throw new InvalidOperationException("Nothing to pop"); throw new InvalidOperationException("Nothing to pop");
} }
_runningStopwatches--;
_events.FindLast(item => item.Stopwatch.IsRunning).Stopwatch.Stop(); _events.FindLast(item => item.Stopwatch.IsRunning).Stopwatch.Stop();
_runningStopwatches--;
} }
/// <summary>
/// <see cref="Pop" /> on the profiler and return a <see cref="Result{TEntity}" />.
/// </summary>
/// <param name="result">The result to be returned.</param>
/// <returns>The original result.</returns>
public Result PopWithResult(Result result) public Result PopWithResult(Result result)
{ {
Pop(); Pop();
@ -60,7 +65,6 @@ public sealed class Profiler
/// <summary> /// <summary>
/// If the profiler took too long to execute, this will log a warning with per-event time usage /// If the profiler took too long to execute, this will log a warning with per-event time usage
/// </summary> /// </summary>
/// <exception cref="InvalidOperationException">Thrown if there are stopwatches still running.</exception>
private void Report() private void Report()
{ {
var main = _events[0]; var main = _events[0];
@ -76,7 +80,10 @@ public sealed class Profiler
var profilerEvent = _events[i]; var profilerEvent = _events[i];
builder.Append(' ', profilerEvent.NestingLevel * 4) builder.Append(' ', profilerEvent.NestingLevel * 4)
.AppendLine($"{profilerEvent.Id}: {profilerEvent.Stopwatch.ElapsedMilliseconds}ms"); .AppendLine($"{profilerEvent.Id}: {profilerEvent.Stopwatch.ElapsedMilliseconds}ms");
unprofiled -= profilerEvent.Stopwatch.ElapsedMilliseconds; if (profilerEvent.NestingLevel is 0)
{
unprofiled -= profilerEvent.Stopwatch.ElapsedMilliseconds;
}
} }
if (unprofiled > 0) if (unprofiled > 0)
@ -89,7 +96,7 @@ public sealed class Profiler
} }
/// <summary> /// <summary>
/// <see cref="Pop"/> the profiler and <see cref="Report"/> on it afterwards. /// <see cref="Pop"/> all running events in the profiler and <see cref="Report"/> on it afterwards.
/// </summary> /// </summary>
private void PopAndReport() private void PopAndReport()
{ {
@ -104,8 +111,8 @@ public sealed class Profiler
/// <summary> /// <summary>
/// <see cref="PopAndReport"/> on the profiler and return a <see cref="Result{TEntity}"/>. /// <see cref="PopAndReport"/> on the profiler and return a <see cref="Result{TEntity}"/>.
/// </summary> /// </summary>
/// <param name="result"></param> /// <param name="result">The result to be returned.</param>
/// <returns></returns> /// <returns>The original result.</returns>
public Result ReportWithResult(Result result) public Result ReportWithResult(Result result)
{ {
PopAndReport(); PopAndReport();