From 17a42ab2923c23d39ce2a00249a1d8b63526c813 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 09:39:50 -0500 Subject: [PATCH] perf(dashboard): coalesce session-details event renders to 250 ms and key the rows (DSH-004, #256) --- .../Components/Pages/SessionDetailsPage.razor | 43 +++++++++--- .../SessionDetailsPageEventAclTests.cs | 68 ++++++++++++++++++- 2 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Pages/SessionDetailsPage.razor b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Pages/SessionDetailsPage.razor index 480e6331..523b3736 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Pages/SessionDetailsPage.razor +++ b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Pages/SessionDetailsPage.razor @@ -142,9 +142,10 @@ else - @foreach (MxEvent evt in _recentEvents) + @foreach (EventRow row in _recentEvents) { - + MxEvent evt = row.Event; + @evt.WorkerSequence @evt.Family @evt.ServerHandle @@ -162,6 +163,20 @@ else @code { private const int MaxRecentEvents = 50; + /// + /// Minimum spacing between event-driven renders. A busy session emits hundreds of events a + /// second; the page shows only the newest , so rendering more + /// often than a few times a second only costs circuit bandwidth and gateway CPU. + /// + internal static readonly TimeSpan EventRenderInterval = TimeSpan.FromMilliseconds(250); + + // A row's key is page-local rather than the worker sequence: a duplicate key among siblings + // throws in the renderer, and a circuit-killing exception is the wrong failure mode for a + // display-only table. + private sealed record EventRow(long Id, MxEvent Event); + + private long _nextEventRowId; + [Parameter] public string SessionId { get; set; } = string.Empty; @@ -196,7 +211,7 @@ else // subscription (never disposed, its viewer registration never released, its pump never // cancelled) behind the fields it overwrites. private int _attachGeneration; - private readonly LinkedList _recentEvents = new(); + private readonly LinkedList _recentEvents = new(); private bool CanManage { get; set; } @@ -367,13 +382,17 @@ else { while (await subscription.Reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) { - // Drain what is queued and render once: a burst costs one render pass, - // not one per event. Reading past the display cap would be wasted work, - // and anything left queued is picked up on the next pass. - List batch = new(); - while (batch.Count < MaxRecentEvents && subscription.Reader.TryRead(out MxEvent? mxEvent)) + // Drain everything queued and render once: a burst costs one render pass, + // not one per event. Only the newest MaxRecentEvents can be shown, so older + // ones are discarded as the drain goes rather than held. + Queue batch = new(); + while (subscription.Reader.TryRead(out MxEvent? mxEvent)) { - batch.Add(mxEvent); + batch.Enqueue(mxEvent); + if (batch.Count > MaxRecentEvents) + { + batch.Dequeue(); + } } if (batch.Count == 0) @@ -396,7 +415,7 @@ else foreach (MxEvent mxEvent in batch) { - _recentEvents.AddFirst(mxEvent); + _recentEvents.AddFirst(new EventRow(++_nextEventRowId, mxEvent)); } while (_recentEvents.Count > MaxRecentEvents) @@ -406,6 +425,10 @@ else StateHasChanged(); }).ConfigureAwait(false); + + // Time-based coalescing: events arriving during this wait queue up and are + // drained into the next single render. + await Task.Delay(EventRenderInterval, cancellationToken).ConfigureAwait(false); } } catch (OperationCanceledException) diff --git a/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Dashboard/SessionDetailsPageEventAclTests.cs b/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Dashboard/SessionDetailsPageEventAclTests.cs index 1bb16b5c..c8bee4c7 100644 --- a/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Dashboard/SessionDetailsPageEventAclTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Dashboard/SessionDetailsPageEventAclTests.cs @@ -134,6 +134,56 @@ public sealed class SessionDetailsPageEventAclTests Assert.Empty(subscriber.UndisposedAfterReplacement); } + /// + /// DSH-004: a session emitting hundreds of events a second renders a bounded number of times, + /// and the retained rows are the newest ones. + /// + /// A task that represents the asynchronous operation. + [Fact] + public async Task Page_WhenEventsArriveInABurst_CoalescesRendersAndKeepsNewestRows() + { + RecordingEventSubscriber subscriber = new(); + ServiceCollection services = BuildServices(subscriber, allow: true); + await using ServiceProvider provider = services.BuildServiceProvider(); + await using ParameterDrivingRenderer renderer = new(provider, provider.GetRequiredService()); + + SessionDetailsPage page = await renderer.MountAsync(); + await renderer.SetParametersAsync(page, SessionId).WaitAsync(TestTimeout); + int baseline = renderer.RenderBatches; + + // ~1.2 s of traffic at roughly 800 events/s. + const int total = 1000; + for (int sequence = 1; sequence <= total; sequence++) + { + subscriber.Latest.Write(new MxEvent { WorkerSequence = (ulong)sequence }); + if (sequence % 4 == 0) + { + await Task.Delay(1); + } + } + + // Let the pump drain what the last interval left queued. + await Task.Delay(SessionDetailsPage.EventRenderInterval * 3); + + int renders = renderer.RenderBatches - baseline; + Assert.Empty(renderer.Exceptions); + Assert.InRange(renders, 1, 20); + + System.Reflection.FieldInfo rows = typeof(SessionDetailsPage).GetField( + "_recentEvents", + System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!; + System.Collections.IEnumerable recent = (System.Collections.IEnumerable)rows.GetValue(page)!; + int count = 0; + foreach (object row in recent) + { + MxEvent evt = (MxEvent)row.GetType().GetProperty("Event")!.GetValue(row)!; + Assert.Equal((ulong)(total - count), evt.WorkerSequence); + count++; + } + + Assert.Equal(50, count); + } + private static readonly TimeSpan TestTimeout = TimeSpan.FromSeconds(30); private static ServiceCollection BuildServices( @@ -192,6 +242,9 @@ public sealed class SessionDetailsPageEventAclTests public IReadOnlyList UndisposedAfterReplacement => [.. _handedOut.SkipLast(1).Where(subscription => !subscription.IsDisposed)]; + /// Gets the most recently handed-out subscription. + internal IdleSubscription Latest => _handedOut[^1]; + /// public IDashboardEventSubscription Subscribe(string sessionId) { @@ -214,6 +267,10 @@ public sealed class SessionDetailsPageEventAclTests /// public ChannelReader Reader => _channel.Reader; + /// Queues an event for the page's pump to read. + /// The event to queue. + public void Write(MxEvent mxEvent) => _channel.Writer.TryWrite(mxEvent); + /// Marks the subscription as released and completes its channel. public void Dispose() { @@ -271,6 +328,11 @@ public sealed class SessionDetailsPageEventAclTests /// Gets exceptions the renderer surfaced, so a test never passes over a swallowed fault. public List Exceptions { get; } = []; + /// Gets the number of render batches the renderer produced. + public int RenderBatches => Volatile.Read(ref _renderBatches); + + private int _renderBatches; + /// public override Dispatcher Dispatcher { get; } = Dispatcher.CreateDefault(); @@ -306,7 +368,11 @@ public sealed class SessionDetailsPageEventAclTests protected override void HandleException(Exception exception) => Exceptions.Add(exception); /// - protected override Task UpdateDisplayAsync(in RenderBatch renderBatch) => Task.CompletedTask; + protected override Task UpdateDisplayAsync(in RenderBatch renderBatch) + { + Interlocked.Increment(ref _renderBatches); + return Task.CompletedTask; + } } #pragma warning restore BL0006