Removed Avg, Max and added raw stats i.e LastMoveNextTime

This commit is contained in:
Zaafar 2021-03-21 12:54:21 -04:00
parent 0e5b571590
commit 889ed157fe
3 changed files with 10 additions and 24 deletions

View File

@ -36,15 +36,11 @@ namespace Coroutine {
/// </summary> /// </summary>
public int MoveNextCount { get; private set; } public int MoveNextCount { get; private set; }
/// <summary> /// <summary>
/// The maximum amount of time that <see cref="MoveNext"/> took. /// The amount of time that the last <see cref="MoveNext"/> took.
/// This is the maximum amount of time that each "step", or each yield statement, of this coroutine took to execute. /// This is the amount of time that this active coroutine took for the last "step", or yield statement.
/// </summary> /// </summary>
public TimeSpan MaxMoveNextTime { get; private set; } public TimeSpan LastMoveNextTime { get; private set; }
/// <summary>
/// The average amount of time that <see cref="MoveNext"/> took.
/// This is the average amount of time that each "step", or each yield statement, of this coroutine took to execute.
/// </summary>
public TimeSpan AverageMoveNextTime => new TimeSpan(this.TotalMoveNextTime.Ticks / this.MoveNextCount);
/// <summary> /// <summary>
/// An event that gets fired when this active coroutine finishes or gets cancelled. /// An event that gets fired when this active coroutine finishes or gets cancelled.
/// When this event is called, <see cref="IsFinished"/> is always true. /// When this event is called, <see cref="IsFinished"/> is always true.
@ -97,9 +93,8 @@ namespace Coroutine {
this.stopwatch.Restart(); this.stopwatch.Restart();
var result = this.enumerator.MoveNext(); var result = this.enumerator.MoveNext();
this.stopwatch.Stop(); this.stopwatch.Stop();
this.LastMoveNextTime = this.stopwatch.Elapsed;
this.TotalMoveNextTime += this.stopwatch.Elapsed; this.TotalMoveNextTime += this.stopwatch.Elapsed;
if (this.stopwatch.Elapsed > this.MaxMoveNextTime)
this.MaxMoveNextTime = this.stopwatch.Elapsed;
this.MoveNextCount++; this.MoveNextCount++;
if (!result) { if (!result) {

View File

@ -56,8 +56,7 @@ namespace Example {
Console.WriteLine("By the way, the first coroutine has finished!"); Console.WriteLine("By the way, the first coroutine has finished!");
Console.WriteLine($"{first.Name} data: {first.MoveNextCount} moves, " + Console.WriteLine($"{first.Name} data: {first.MoveNextCount} moves, " +
$"{first.TotalMoveNextTime.TotalMilliseconds} total time, " + $"{first.TotalMoveNextTime.TotalMilliseconds} total time, " +
$"{first.AverageMoveNextTime.TotalMilliseconds} average, " + $"{first.LastMoveNextTime.TotalMilliseconds} last time");
$"{first.MaxMoveNextTime.TotalMilliseconds} maximum");
Environment.Exit(0); Environment.Exit(0);
} }
} }

View File

@ -313,7 +313,7 @@ namespace Tests {
} }
[Test] [Test]
public void CoroutineStatsAre95PercentAccurate() { public void CoroutineStatsAreUpdated() {
static IEnumerator<Wait> CoroutineTakesMax500Ms() { static IEnumerator<Wait> CoroutineTakesMax500Ms() {
Thread.Sleep(200); Thread.Sleep(200);
yield return new Wait(10); yield return new Wait(10);
@ -324,17 +324,9 @@ namespace Tests {
for (var i = 0; i < 5; i++) for (var i = 0; i < 5; i++)
CoroutineHandler.Tick(50); CoroutineHandler.Tick(50);
const int expected1 = 350; Assert.IsTrue(cr.TotalMoveNextTime.TotalMilliseconds >= 700);
const float errorbar1 = 5 / 100f * expected1; Assert.IsTrue(cr.LastMoveNextTime.TotalMilliseconds >= 500);
var gTa = cr.AverageMoveNextTime.Milliseconds > expected1 - errorbar1; // 95% accuracy. Assert.IsTrue(cr.MoveNextCount == 2);
var lTb = cr.AverageMoveNextTime.Milliseconds < expected1 + errorbar1; // 95% accuracy.
Assert.IsTrue(gTa && lTb, $"Average Move Next Time {cr.AverageMoveNextTime.Milliseconds} is invalid.");
const int expected2 = 500;
const float errorbar2 = 5 / 100f * expected2;
var gTc = cr.MaxMoveNextTime.Milliseconds > expected2 - errorbar2; // 95% accuracy.
var lTd = cr.MaxMoveNextTime.Milliseconds < expected2 + errorbar2; // 95% accuracy.
Assert.IsTrue(gTc && lTd, $"Maximum Move Next Time {cr.MaxMoveNextTime.Milliseconds} is invalid.");
} }
[Test] [Test]