From 57109a042fd18bf742eb2f614fa9a221653cf007 Mon Sep 17 00:00:00 2001 From: "T. Thiery" Date: Wed, 23 Dec 2020 18:49:58 +0100 Subject: [PATCH] Finish RampUp - Waiting Time is adjusted to command execution time (IO) #57 non-breaking --- .../ExampleRampUp.cs | 4 ++-- .../Functions/IterativeChange.cs | 22 +++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs b/examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs index 70210a2..0be2091 100644 --- a/examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs +++ b/examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs @@ -23,7 +23,7 @@ public override async Task ExecuteAsync() await technicMediumHub.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red); stopWatch.Start(); - await rampUp.ExecuteAsync(motor, 20, 100, 40, 5_000); + await rampUp.ExecuteAsync(motor, 20, 100, 40, 10_000); var redPhase = stopWatch.ElapsedMilliseconds; await technicMediumHub.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Green); @@ -36,7 +36,7 @@ public override async Task ExecuteAsync() var rampDown = ServiceProvider.GetService(); var beforeOrangePhase = stopWatch.ElapsedMilliseconds; - await rampDown.ExecuteAsync(motor, 100, 0, 100, 10_000); + await rampDown.ExecuteAsync(motor, 100, 0, 100, 20_000); var orangePhase = stopWatch.ElapsedMilliseconds - beforeOrangePhase; stopWatch.Stop(); diff --git a/src/SharpBrick.PoweredUp/Functions/IterativeChange.cs b/src/SharpBrick.PoweredUp/Functions/IterativeChange.cs index fe7c61a..f1f3dee 100644 --- a/src/SharpBrick.PoweredUp/Functions/IterativeChange.cs +++ b/src/SharpBrick.PoweredUp/Functions/IterativeChange.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -7,6 +8,7 @@ namespace SharpBrick.PoweredUp.Functions public abstract class IterativeChange { private readonly ILogger> _logger; + private readonly Stopwatch _stopwatch; protected abstract T Function(int idx); protected abstract Task ChangeAsync(T value, CancellationTokenSource cts); @@ -14,15 +16,18 @@ public abstract class IterativeChange public IterativeChange(ILogger> logger) { _logger = logger; + _stopwatch = new Stopwatch(); } protected async Task IterativeExecuteAsync(int iterations, int delayInMilliseconds, CancellationTokenSource cts = default) { _logger.LogInformation($"Start iterative change over {iterations} steps"); + _stopwatch.Start(); for (int idx = 0; idx < iterations; idx++) { _logger.LogInformation($"+ Iteration: {idx}"); + var startTime = _stopwatch.ElapsedMilliseconds; if (cts?.IsCancellationRequested ?? false) { _logger.LogInformation("+ Cancelled (before Actor)"); @@ -42,12 +47,25 @@ protected async Task IterativeExecuteAsync(int iterations, int delayInMillisecon break; } + var endTime = _stopwatch.ElapsedMilliseconds; - _logger.LogInformation($"+ Waiting {delayInMilliseconds}ms"); + var currentDuration = endTime - startTime; + var waiting = delayInMilliseconds - currentDuration; - await Task.Delay(delayInMilliseconds); + _logger.LogInformation($"+ Waiting {delayInMilliseconds}ms ({currentDuration} already elapsed; {waiting} to go; walltime: {endTime})"); + + if (waiting > 0 && waiting <= int.MaxValue) + { + await Task.Delay((int)waiting); + } + else + { + _logger.LogError($"+ IO Time exceeded Waiting Time. Reducing Step Count or Increasing Delay Time might help."); + } } + _stopwatch.Stop(); + _logger.LogInformation("Finished iterative change"); } }