public void When_checking_in_multiple_times_for_a_projector_it_should_remember_the_last_only() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- DateTime nowUtc = 16.June(2017).At(15, 00).AsUtc(); var stats = new ProjectionStats(() => nowUtc); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- stats.TrackProgress("myProjector", 1000); nowUtc = nowUtc.Add(1.Hours()); stats.TrackProgress("myProjector", 2000); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- var projectorStats = stats.Should().ContainSingle(s => s.ProjectorId == "myProjector").Subject; projectorStats.LastCheckpoint.Checkpoint.Should().Be(2000); projectorStats.LastCheckpoint.TimestampUtc.Should().Be(nowUtc); }
public void When_the_projector_runs_for_more_than_10_minutes_it_should_only_evaluate_the_last_10_minutes() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- DateTime nowUtc = 16.June(2017).At(15, 00).AsUtc(); var stats = new ProjectionStats(() => nowUtc); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- long checkpoint = 0; // The first ten minutes should be ignored for (int seconds = 0; seconds < (10 * 60); ++seconds) { checkpoint += 1000; stats.TrackProgress("myProjector", checkpoint); nowUtc = nowUtc.Add(1.Seconds()); } // Then nine minutes of 2000/s. for (int seconds = 0; seconds < (9 * 60); ++seconds) { checkpoint += 2000; stats.TrackProgress("myProjector", checkpoint); nowUtc = nowUtc.Add(1.Seconds()); } // The last minute should run on 3000/s for (int seconds = 0; seconds < (60); ++seconds) { checkpoint += 3000; stats.TrackProgress("myProjector", checkpoint); nowUtc = nowUtc.Add(1.Seconds()); } //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- TimeSpan?eta = stats.GetTimeToReach("myProjector", checkpoint + 100000); float precalculatedWeightedAveragePerSecond = 2222.5022F; long secondsToComplete = (long)(100000 / precalculatedWeightedAveragePerSecond); eta.Should().Be(TimeSpan.FromSeconds(secondsToComplete)); stats.GetSpeed("myProjector").Should().BeApproximately(precalculatedWeightedAveragePerSecond, 1); }
public void When_the_projector_runs_at_a_very_low_speed_it_should_still_calculate_the_eta() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- DateTime nowUtc = 16.June(2017).At(15, 00).AsUtc(); var stats = new ProjectionStats(() => nowUtc); long transactionsPer5Seconds = 1; //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- long checkpoint = 0; for (int seconds = 0; seconds < 60; seconds += 5) { checkpoint += transactionsPer5Seconds; stats.TrackProgress("myProjector", checkpoint); nowUtc = nowUtc.Add(5.Seconds()); } //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- TimeSpan?eta = stats.GetTimeToReach("myProjector", 100000); long secondsToComplete = ((100000 - checkpoint) / transactionsPer5Seconds * 5); eta.Should().BeCloseTo(TimeSpan.FromSeconds(secondsToComplete), 1000); }
public When_a_specific_projector_is_requested() { Given(() => { var nowUtc = 10.July(2017).At(10, 39).AsUtc(); var stats = new ProjectionStats(() => nowUtc); stats.TrackProgress("id1", 1000); stats.StoreProperty("id1", "property1", "value1"); var appBuilder = new AppBuilder(); appBuilder.UseLiquidProjections(stats); var httpClient = new HttpClient(new OwinHttpMessageHandler(appBuilder.Build())); httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); WithSubject(ct => httpClient); }); When(async() => { return(await Subject.GetAsync("http://localhost/projectionStats/id1")); }); }
public void When_the_projector_is_ahead_of_the_requested_checkpoint_the_eta_should_be_zero() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- DateTime utcNow = new DateTime(2017, 7, 4, 11, 50, 0, DateTimeKind.Utc); var stats = new ProjectionStats(() => utcNow); stats.TrackProgress("myProjector", 1000); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- utcNow = new DateTime(2017, 7, 4, 11, 52, 0, DateTimeKind.Utc); stats.TrackProgress("myProjector", 10000); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- stats.GetSpeed("myProjector").Should().Be((10000 - 1000) / 120); stats.GetTimeToReach("myProjector", 5000).Should().Be(TimeSpan.Zero); }
public void When_the_projectors_speed_increases_it_should_favor_the_higher_speed_in_the_eta() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- DateTime nowUtc = 16.June(2017).At(15, 00).AsUtc(); var stats = new ProjectionStats(() => nowUtc); long transactionsPerSecond = 1000; //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- long checkpoint = 0; for (int seconds = 0; seconds < 60; ++seconds) { checkpoint += transactionsPerSecond; stats.TrackProgress("myProjector", checkpoint); nowUtc = nowUtc.Add(1.Seconds()); transactionsPerSecond += 100; } //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- TimeSpan?eta = stats.GetTimeToReach("myProjector", checkpoint + 100000); long weightedAveragePerSecond = 4550; stats.GetSpeed("myProjector").Should().Be(weightedAveragePerSecond); long secondsToComplete = 100000 / weightedAveragePerSecond; eta.Should().Be(TimeSpan.FromSeconds(secondsToComplete)); }
public void When_the_projector_runs_at_a_constant_speed_it_should_use_that_to_calculate_the_eta() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- DateTime nowUtc = 16.June(2017).At(15, 00).AsUtc(); var stats = new ProjectionStats(() => nowUtc); long transactionsPerSecond = 1000; //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- long checkpoint = 0; for (int seconds = 0; seconds < 60; ++seconds) { checkpoint += transactionsPerSecond; stats.TrackProgress("myProjector", checkpoint); nowUtc = nowUtc.Add(1.Seconds()); } //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- TimeSpan?eta = stats.GetTimeToReach("myProjector", 100000); stats.GetSpeed("myProjector").Should().Be(transactionsPerSecond); long secondsToComplete = (100000 - checkpoint) / transactionsPerSecond; eta.Should().Be(TimeSpan.FromSeconds(secondsToComplete)); }
public async Task Handle(IReadOnlyList <Transaction> transactions) { await InnerProjector.Handle(transactions); stats.TrackProgress(Id, transactions.Last().Checkpoint); }