mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-05 23:11:36 +00:00
Improved the pauses between indexing steps and between files of the Batch Processing Assistant (#1240)
This commit is contained in:
1 parent
a117074972
commit
899139c88b
32 files changed
+787
-425
No files matched your search
@@ -0,0 +1,143 @@
|
||||
using AIStudio.Tools.Pauses;
|
||||
|
||||
namespace AIStudio.Tests.Tools.Pauses;
|
||||
|
||||
/// <summary>
|
||||
/// Checks the pause between two indexing steps, which spares an embedding server that many people share.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The pause is a gap after every step which worked on documents. It is drawn only once a step is
|
||||
/// about to start, and the time which passed by then counts toward it.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class PauseBetweenStepsTests
|
||||
{
|
||||
private static readonly DateTimeOffset STEP_ENDED = new(2026, 10, 27, 14, 0, 0, TimeSpan.Zero);
|
||||
|
||||
private static readonly PauseRange THIRTY_TO_SIXTY_SECONDS = new(30, 60);
|
||||
|
||||
[Test]
|
||||
public void BeforeTheFirstStepNothingWaits()
|
||||
{
|
||||
var random = new LargestRandom();
|
||||
var pause = new PauseBetweenSteps().GetPauseBeforeNextStep(STEP_ENDED, THIRTY_TO_SIXTY_SECONDS, random);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(pause, Is.EqualTo(PauseBeforeStep.NONE));
|
||||
Assert.That(random.Draws, Is.Zero);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheNextStepWaitsForTheWholePauseRightAfterAStep()
|
||||
{
|
||||
var pauses = new PauseBetweenSteps();
|
||||
pauses.RecordStepEnded(STEP_ENDED);
|
||||
|
||||
var pause = pauses.GetPauseBeforeNextStep(STEP_ENDED, THIRTY_TO_SIXTY_SECONDS, new LargestRandom());
|
||||
|
||||
Assert.That(pause, Is.EqualTo(new PauseBeforeStep(TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(60))));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheUpperBoundCanBeDrawn()
|
||||
{
|
||||
var pauses = new PauseBetweenSteps();
|
||||
pauses.RecordStepEnded(STEP_ENDED);
|
||||
|
||||
var pause = pauses.GetPauseBeforeNextStep(STEP_ENDED, new PauseRange(45, 45), new LargestRandom());
|
||||
|
||||
Assert.That(pause.Pause, Is.EqualTo(TimeSpan.FromSeconds(45)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TimeWhichPassedSinceTheStepCountsTowardThePause()
|
||||
{
|
||||
var pauses = new PauseBetweenSteps();
|
||||
pauses.RecordStepEnded(STEP_ENDED);
|
||||
|
||||
var pause = pauses.GetPauseBeforeNextStep(STEP_ENDED + TimeSpan.FromSeconds(25), THIRTY_TO_SIXTY_SECONDS, new LargestRandom());
|
||||
|
||||
Assert.That(pause, Is.EqualTo(new PauseBeforeStep(TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(35))));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AStepWhichStartsLaterAnywayDoesNotWait()
|
||||
{
|
||||
var pauses = new PauseBetweenSteps();
|
||||
pauses.RecordStepEnded(STEP_ENDED);
|
||||
|
||||
var pause = pauses.GetPauseBeforeNextStep(STEP_ENDED + TimeSpan.FromSeconds(60), THIRTY_TO_SIXTY_SECONDS, new LargestRandom());
|
||||
|
||||
Assert.That(pause, Is.EqualTo(PauseBeforeStep.NONE));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NothingIsDrawnWhileThereIsNoPause()
|
||||
{
|
||||
var pauses = new PauseBetweenSteps();
|
||||
pauses.RecordStepEnded(STEP_ENDED);
|
||||
var random = new LargestRandom();
|
||||
|
||||
var withoutPause = pauses.GetPauseBeforeNextStep(STEP_ENDED, PauseRange.NONE, random);
|
||||
var withPause = pauses.GetPauseBeforeNextStep(STEP_ENDED + TimeSpan.FromSeconds(10), THIRTY_TO_SIXTY_SECONDS, random);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(withoutPause, Is.EqualTo(PauseBeforeStep.NONE));
|
||||
Assert.That(withPause, Is.EqualTo(new PauseBeforeStep(TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(50))));
|
||||
Assert.That(random.Draws, Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AStepWhichStoppedWhileWaitingLeavesTheRestOfThePauseToTheNextOne()
|
||||
{
|
||||
var pauses = new PauseBetweenSteps();
|
||||
pauses.RecordStepEnded(STEP_ENDED);
|
||||
var random = new LargestRandom();
|
||||
|
||||
pauses.GetPauseBeforeNextStep(STEP_ENDED, THIRTY_TO_SIXTY_SECONDS, random);
|
||||
var nextPause = pauses.GetPauseBeforeNextStep(STEP_ENDED + TimeSpan.FromSeconds(20), new PauseRange(1, 1), random);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(nextPause, Is.EqualTo(new PauseBeforeStep(TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(40))));
|
||||
Assert.That(random.Draws, Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ANewStepOpensANewGap()
|
||||
{
|
||||
var pauses = new PauseBetweenSteps();
|
||||
pauses.RecordStepEnded(STEP_ENDED);
|
||||
var random = new LargestRandom();
|
||||
pauses.GetPauseBeforeNextStep(STEP_ENDED, THIRTY_TO_SIXTY_SECONDS, random);
|
||||
|
||||
var nextStepEnded = STEP_ENDED + TimeSpan.FromMinutes(6);
|
||||
pauses.RecordStepEnded(nextStepEnded);
|
||||
var pause = pauses.GetPauseBeforeNextStep(nextStepEnded + TimeSpan.FromSeconds(5), new PauseRange(10, 10), random);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(pause, Is.EqualTo(new PauseBeforeStep(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(5))));
|
||||
Assert.That(random.Draws, Is.EqualTo(2));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Always draws the largest value it may, and counts how often it was asked.
|
||||
/// </summary>
|
||||
private sealed class LargestRandom : Random
|
||||
{
|
||||
public int Draws { get; private set; }
|
||||
|
||||
public override int Next(int minValue, int maxValue)
|
||||
{
|
||||
this.Draws++;
|
||||
return maxValue - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
using AIStudio.Settings.DataModel;
|
||||
using AIStudio.Tools.Pauses;
|
||||
|
||||
namespace AIStudio.Tests.Tools.Pauses;
|
||||
|
||||
/// <summary>
|
||||
/// Checks which bounds of a random pause apply, as the user set them, as configuration plugins lock them, and within the limits of each feature.
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public sealed class PauseRangeTests
|
||||
{
|
||||
private static readonly Func<bool> LOCKED = () => true;
|
||||
|
||||
private static readonly Func<bool> UNLOCKED = () => false;
|
||||
|
||||
private static readonly PauseLimits INDEXING = DataDataSourceIndexing.PAUSE_LIMITS;
|
||||
|
||||
private static readonly PauseLimits BATCH_PROCESSING = DataBatchProcessing.PAUSE_LIMITS;
|
||||
|
||||
[Test]
|
||||
public void BoundsInOrderStayAsTheyAre()
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(INDEXING, 10, UNLOCKED, 20, UNLOCKED), Is.EqualTo(new PauseRange(10, 20)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnUpperBoundBelowTheLowerOneIsRaisedToIt()
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(INDEXING, 60, UNLOCKED, 20, UNLOCKED), Is.EqualTo(new PauseRange(60, 60)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ALockedLowerBoundRaisesTheUpperOne()
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(INDEXING, 60, LOCKED, 20, UNLOCKED), Is.EqualTo(new PauseRange(60, 60)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ALockedUpperBoundLowersTheLowerOne()
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(INDEXING, 60, UNLOCKED, 20, LOCKED), Is.EqualTo(new PauseRange(20, 20)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BothBoundsLockedTheWrongWayRoundLetTheLongerPauseWin()
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(INDEXING, 60, LOCKED, 20, LOCKED), Is.EqualTo(new PauseRange(60, 60)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BoundsOutsideTheLimitsAreClamped()
|
||||
{
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(INDEXING, -5, UNLOCKED, 1_000, UNLOCKED), Is.EqualTo(new PauseRange(0, 300)));
|
||||
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, -5, UNLOCKED, 1_000, UNLOCKED), Is.EqualTo(new PauseRange(6, 300)));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BoundsBelowTheShortestPauseOfAFeatureAreRaisedToIt()
|
||||
{
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, 0, UNLOCKED, 3, UNLOCKED), Is.EqualTo(new PauseRange(6, 6)));
|
||||
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, 2, UNLOCKED, 20, UNLOCKED), Is.EqualTo(new PauseRange(6, 20)));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ALockedUpperBoundLowersTheLowerOneOnlyToTheShortestPauseOfAFeature()
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, 60, UNLOCKED, 2, LOCKED), Is.EqualTo(new PauseRange(6, 6)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheLocksAreAskedOnlyWhileTheBoundsAreTheWrongWayRound()
|
||||
{
|
||||
var asked = 0;
|
||||
var countingLock = () =>
|
||||
{
|
||||
asked++;
|
||||
return false;
|
||||
};
|
||||
|
||||
PauseRange.GetEffective(INDEXING, 10, countingLock, 10, countingLock);
|
||||
PauseRange.GetEffective(INDEXING, 10, countingLock, 20, countingLock);
|
||||
PauseRange.GetEffective(BATCH_PROCESSING, 0, countingLock, 3, countingLock);
|
||||
Assert.That(asked, Is.Zero);
|
||||
|
||||
PauseRange.GetEffective(INDEXING, 20, countingLock, 10, countingLock);
|
||||
Assert.That(asked, Is.Positive);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheIndexingTakesItsBoundsFromItsSettings()
|
||||
{
|
||||
var settings = new DataDataSourceIndexing
|
||||
{
|
||||
MinimumPauseBetweenStepsSeconds = 10,
|
||||
MaximumPauseBetweenStepsSeconds = 20
|
||||
};
|
||||
|
||||
Assert.That(settings.EffectivePauseRange, Is.EqualTo(new PauseRange(10, 20)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheBatchProcessingPausesSixToTenSecondsByDefault()
|
||||
{
|
||||
var settings = new DataBatchProcessing();
|
||||
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, settings.MinimumDelaySeconds, UNLOCKED, settings.MaximumDelaySeconds, UNLOCKED), Is.EqualTo(new PauseRange(6, 10)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OnlyZeroForBothBoundsMeansNoPause()
|
||||
{
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(INDEXING, 0, UNLOCKED, 0, UNLOCKED).IsNone, Is.True);
|
||||
Assert.That(PauseRange.GetEffective(INDEXING, 0, UNLOCKED, 5, UNLOCKED).IsNone, Is.False);
|
||||
Assert.That(PauseRange.GetEffective(INDEXING, 30, UNLOCKED, 0, UNLOCKED), Is.EqualTo(new PauseRange(30, 30)));
|
||||
Assert.That(new DataDataSourceIndexing().EffectivePauseRange.IsNone, Is.True);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheBatchProcessingAlwaysPauses()
|
||||
{
|
||||
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, 0, UNLOCKED, 0, UNLOCKED).IsNone, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheLimitsAllowTheirShortestAndLongestPause()
|
||||
{
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(INDEXING.Allows(0), Is.True);
|
||||
Assert.That(INDEXING.Allows(-1), Is.False);
|
||||
Assert.That(INDEXING.Allows(300), Is.True);
|
||||
Assert.That(INDEXING.Allows(301), Is.False);
|
||||
Assert.That(BATCH_PROCESSING.Allows(5), Is.False);
|
||||
Assert.That(BATCH_PROCESSING.Allows(6), Is.True);
|
||||
Assert.That(BATCH_PROCESSING.Allows(300), Is.True);
|
||||
Assert.That(BATCH_PROCESSING.Allows(301), Is.False);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user