perf(ui): shared KPI cache, live-cache-backed alarm summary, coalesced debug renders
This commit is contained in:
+144
@@ -0,0 +1,144 @@
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using System.Security.Claims;
|
||||
using Bunit;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using ZB.MOM.WW.ScadaBridge.CentralUI.Auth;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming;
|
||||
using ZB.MOM.WW.ScadaBridge.Communication;
|
||||
using ZB.MOM.WW.ScadaBridge.Communication.Grpc;
|
||||
using DebugViewPage = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Deployment.DebugView;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Deployment;
|
||||
|
||||
/// <summary>
|
||||
/// Regression tests for the Debug View render coalescing (arch-review WP2.4). Every streamed
|
||||
/// event used to marshal onto the circuit dispatcher and call <c>StateHasChanged</c> on its
|
||||
/// own, so a chatty instance drove one full render — and two full composition-tree rebuilds —
|
||||
/// per value change. Events are now buffered and applied once per ~250 ms window: a burst
|
||||
/// costs a handful of renders instead of one per event, and no event is dropped.
|
||||
/// </summary>
|
||||
public class DebugViewRenderCoalescingTests : BunitContext
|
||||
{
|
||||
private IRenderedComponent<DebugViewPage> RenderPage()
|
||||
{
|
||||
JSInterop.Mode = JSRuntimeMode.Loose;
|
||||
|
||||
var repo = Substitute.For<ITemplateEngineRepository>();
|
||||
var siteRepo = Substitute.For<ISiteRepository>();
|
||||
siteRepo.GetAllSitesAsync().Returns(new List<Site>());
|
||||
Services.AddSingleton(repo);
|
||||
Services.AddSingleton(siteRepo);
|
||||
|
||||
var comms = new CommunicationService(
|
||||
Options.Create(new CommunicationOptions()),
|
||||
NullLogger<CommunicationService>.Instance);
|
||||
Services.AddSingleton(comms);
|
||||
|
||||
var grpcFactory = new SiteStreamGrpcClientFactory(NullLoggerFactory.Instance);
|
||||
var debugStream = new DebugStreamService(
|
||||
comms, new ServiceCollection().BuildServiceProvider(), grpcFactory,
|
||||
NullLogger<DebugStreamService>.Instance);
|
||||
Services.AddSingleton(debugStream);
|
||||
|
||||
var identity = new ClaimsIdentity(
|
||||
new[] { new Claim(ClaimTypes.Name, "deployer") }, "TestCookie");
|
||||
var stubAuth = new StubAuthStateProvider(
|
||||
new AuthenticationState(new ClaimsPrincipal(identity)));
|
||||
Services.AddSingleton<AuthenticationStateProvider>(stubAuth);
|
||||
Services.AddScoped(_ => new SiteScopeService(stubAuth));
|
||||
|
||||
return Render<DebugViewPage>();
|
||||
}
|
||||
|
||||
private sealed class StubAuthStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
private readonly AuthenticationState _state;
|
||||
public StubAuthStateProvider(AuthenticationState state) => _state = state;
|
||||
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
=> Task.FromResult(_state);
|
||||
}
|
||||
|
||||
private static MethodInfo HandleStreamEvent => typeof(DebugViewPage).GetMethod(
|
||||
"HandleStreamEvent", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
|
||||
private static IDictionary AttributeValues(DebugViewPage c) => (IDictionary)
|
||||
typeof(DebugViewPage).GetField("_attributeValues",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(c)!;
|
||||
|
||||
private static void Send(DebugViewPage page, string name, object value) =>
|
||||
HandleStreamEvent.Invoke(page, new object[]
|
||||
{
|
||||
new AttributeValueChanged("Inst-1", name, name, value, "Good", DateTimeOffset.UtcNow),
|
||||
});
|
||||
|
||||
[Fact]
|
||||
public void BurstOfEvents_RendersOnce_ButAppliesEveryEvent()
|
||||
{
|
||||
var cut = RenderPage();
|
||||
var dict = AttributeValues(cut.Instance);
|
||||
var rendersBefore = cut.RenderCount;
|
||||
|
||||
const int burst = 300;
|
||||
for (var i = 0; i < burst; i++)
|
||||
{
|
||||
Send(cut.Instance, $"Tag.{i}", i);
|
||||
}
|
||||
|
||||
cut.WaitForState(() => dict.Count == burst, TimeSpan.FromSeconds(5));
|
||||
|
||||
// Pre-fix this was one render per event. The window may close mid-burst, so allow a
|
||||
// few flushes — the point is that it is a small constant, not O(events).
|
||||
var renders = cut.RenderCount - rendersBefore;
|
||||
Assert.InRange(renders, 1, 5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RepeatedUpdatesToOneTag_CollapseToTheLatestValue()
|
||||
{
|
||||
var cut = RenderPage();
|
||||
var dict = AttributeValues(cut.Instance);
|
||||
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
Send(cut.Instance, "Pump.Speed", i);
|
||||
}
|
||||
|
||||
cut.WaitForState(() => dict.Count == 1, TimeSpan.FromSeconds(5));
|
||||
var applied = (AttributeValueChanged)dict["Pump.Speed"]!;
|
||||
Assert.Equal(49, applied.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventArrivingAfterAFlush_StillArmsANewWindow()
|
||||
{
|
||||
var cut = RenderPage();
|
||||
var dict = AttributeValues(cut.Instance);
|
||||
|
||||
Send(cut.Instance, "First", 1);
|
||||
cut.WaitForState(() => dict.Count == 1, TimeSpan.FromSeconds(5));
|
||||
|
||||
// The window disarms on drain; a later event must arm a fresh one rather than
|
||||
// sitting in the pending map until some unrelated event happens to arrive.
|
||||
Send(cut.Instance, "Second", 2);
|
||||
cut.WaitForState(() => dict.Count == 2, TimeSpan.FromSeconds(5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventsAfterDispose_AreDroppedWithoutThrowing()
|
||||
{
|
||||
var cut = RenderPage();
|
||||
cut.Instance.Dispose();
|
||||
|
||||
var ex = Record.Exception(() => Send(cut.Instance, "Late", 1));
|
||||
|
||||
Assert.Null(ex);
|
||||
Assert.Empty(AttributeValues(cut.Instance));
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
using System.Security.Claims;
|
||||
using Bunit;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
using ZB.MOM.WW.ScadaBridge.CentralUI.Auth;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||
using ZB.MOM.WW.ScadaBridge.DeploymentManager;
|
||||
using DeploymentsPage = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Deployment.Deployments;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Deployment;
|
||||
|
||||
/// <summary>
|
||||
/// Regression tests for the Deployment Status push coalescing (arch-review WP2.4). One
|
||||
/// notifier callback reloads EVERY deployment record plus EVERY instance, and the notifier
|
||||
/// fires per status write — so a multi-instance deploy produced a stampede of full reloads
|
||||
/// per circuit. The reload is now leading-edge debounced: the first push after an idle gap
|
||||
/// is still immediate, and a burst behind it collapses into one trailing reload.
|
||||
/// </summary>
|
||||
public class DeploymentsReloadDebounceTests : BunitContext
|
||||
{
|
||||
private IDeploymentManagerRepository _deployRepo = null!;
|
||||
private ITemplateEngineRepository _templateRepo = null!;
|
||||
private DeploymentStatusNotifier _notifier = null!;
|
||||
|
||||
private void RegisterServices()
|
||||
{
|
||||
_deployRepo = Substitute.For<IDeploymentManagerRepository>();
|
||||
_templateRepo = Substitute.For<ITemplateEngineRepository>();
|
||||
_notifier = new DeploymentStatusNotifier(NullLogger<DeploymentStatusNotifier>.Instance);
|
||||
|
||||
_templateRepo.GetAllInstancesAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(new List<Instance> { new("Inst-1") { Id = 1, SiteId = 1 } });
|
||||
_deployRepo.GetAllDeploymentRecordsAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(new List<DeploymentRecord>());
|
||||
|
||||
Services.AddSingleton(_deployRepo);
|
||||
Services.AddSingleton(_templateRepo);
|
||||
Services.AddSingleton<IDeploymentStatusNotifier>(_notifier);
|
||||
|
||||
var identity = new ClaimsIdentity(
|
||||
new[] { new Claim(ClaimTypes.Name, "deployer") }, "TestCookie");
|
||||
var stubAuth = new StubAuthStateProvider(
|
||||
new AuthenticationState(new ClaimsPrincipal(identity)));
|
||||
Services.AddSingleton<AuthenticationStateProvider>(stubAuth);
|
||||
Services.AddScoped(_ => new SiteScopeService(stubAuth));
|
||||
}
|
||||
|
||||
private sealed class StubAuthStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
private readonly AuthenticationState _state;
|
||||
public StubAuthStateProvider(AuthenticationState state) => _state = state;
|
||||
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
=> Task.FromResult(_state);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BurstOfStatusWrites_CollapsesIntoFarFewerReloads()
|
||||
{
|
||||
RegisterServices();
|
||||
var cut = Render<DeploymentsPage>();
|
||||
cut.WaitForAssertion(() =>
|
||||
_deployRepo.Received().GetAllDeploymentRecordsAsync(Arg.Any<CancellationToken>()));
|
||||
_deployRepo.ClearReceivedCalls();
|
||||
|
||||
// A 40-instance site deploy: every status write raises the notifier.
|
||||
for (var i = 0; i < 40; i++)
|
||||
{
|
||||
_notifier.NotifyStatusChanged(
|
||||
new DeploymentStatusChange($"dep-{i}", 1, DeploymentStatus.InProgress));
|
||||
}
|
||||
|
||||
// Leading edge fires at once; the rest ride one trailing reload.
|
||||
cut.WaitForAssertion(() =>
|
||||
_deployRepo.Received().GetAllDeploymentRecordsAsync(Arg.Any<CancellationToken>()));
|
||||
|
||||
// Let the trailing window close before counting.
|
||||
Thread.Sleep(900);
|
||||
|
||||
var reloads = _deployRepo.ReceivedCalls()
|
||||
.Count(c => c.GetMethodInfo().Name == nameof(IDeploymentManagerRepository.GetAllDeploymentRecordsAsync));
|
||||
Assert.InRange(reloads, 1, 4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FirstPushAfterIdle_ReloadsImmediately()
|
||||
{
|
||||
RegisterServices();
|
||||
var cut = Render<DeploymentsPage>();
|
||||
cut.WaitForAssertion(() =>
|
||||
_deployRepo.Received().GetAllDeploymentRecordsAsync(Arg.Any<CancellationToken>()));
|
||||
_deployRepo.ClearReceivedCalls();
|
||||
|
||||
// Idle since the initial load — the leading edge must not wait out the window.
|
||||
Thread.Sleep(600);
|
||||
_notifier.NotifyStatusChanged(
|
||||
new DeploymentStatusChange("dep-1", 1, DeploymentStatus.Success));
|
||||
|
||||
cut.WaitForAssertion(
|
||||
() => _deployRepo.Received().GetAllDeploymentRecordsAsync(Arg.Any<CancellationToken>()),
|
||||
TimeSpan.FromMilliseconds(400));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisposeDuringACoalesceWindow_DoesNotReload()
|
||||
{
|
||||
RegisterServices();
|
||||
var cut = Render<DeploymentsPage>();
|
||||
cut.WaitForAssertion(() =>
|
||||
_deployRepo.Received().GetAllDeploymentRecordsAsync(Arg.Any<CancellationToken>()));
|
||||
|
||||
// Two pushes: the first takes the leading edge, the second arms the trailing timer.
|
||||
_notifier.NotifyStatusChanged(new DeploymentStatusChange("dep-1", 1, DeploymentStatus.InProgress));
|
||||
_notifier.NotifyStatusChanged(new DeploymentStatusChange("dep-2", 1, DeploymentStatus.InProgress));
|
||||
|
||||
cut.Instance.Dispose();
|
||||
_deployRepo.ClearReceivedCalls();
|
||||
|
||||
// The armed timer must be disposed with the component, not fire against it.
|
||||
Thread.Sleep(900);
|
||||
_deployRepo.DidNotReceive().GetAllDeploymentRecordsAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user