perf(dashboard): coalesce session-details event renders to 250 ms and key the rows (DSH-004, #256)
This commit is contained in:
+33
-10
@@ -142,9 +142,10 @@ else
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (MxEvent evt in _recentEvents)
|
||||
@foreach (EventRow row in _recentEvents)
|
||||
{
|
||||
<tr>
|
||||
MxEvent evt = row.Event;
|
||||
<tr @key="row.Id">
|
||||
<td class="num mono">@evt.WorkerSequence</td>
|
||||
<td>@evt.Family</td>
|
||||
<td class="num mono">@evt.ServerHandle</td>
|
||||
@@ -162,6 +163,20 @@ else
|
||||
@code {
|
||||
private const int MaxRecentEvents = 50;
|
||||
|
||||
/// <summary>
|
||||
/// Minimum spacing between event-driven renders. A busy session emits hundreds of events a
|
||||
/// second; the page shows only the newest <see cref="MaxRecentEvents"/>, so rendering more
|
||||
/// often than a few times a second only costs circuit bandwidth and gateway CPU.
|
||||
/// </summary>
|
||||
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<MxEvent> _recentEvents = new();
|
||||
private readonly LinkedList<EventRow> _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<MxEvent> 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<MxEvent> 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)
|
||||
|
||||
@@ -134,6 +134,56 @@ public sealed class SessionDetailsPageEventAclTests
|
||||
Assert.Empty(subscriber.UndisposedAfterReplacement);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DSH-004: a session emitting hundreds of events a second renders a bounded number of times,
|
||||
/// and the retained rows are the newest ones.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[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<ILoggerFactory>());
|
||||
|
||||
SessionDetailsPage page = await renderer.MountAsync<SessionDetailsPage>();
|
||||
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<IdleSubscription> UndisposedAfterReplacement =>
|
||||
[.. _handedOut.SkipLast(1).Where(subscription => !subscription.IsDisposed)];
|
||||
|
||||
/// <summary>Gets the most recently handed-out subscription.</summary>
|
||||
internal IdleSubscription Latest => _handedOut[^1];
|
||||
|
||||
/// <inheritdoc />
|
||||
public IDashboardEventSubscription Subscribe(string sessionId)
|
||||
{
|
||||
@@ -214,6 +267,10 @@ public sealed class SessionDetailsPageEventAclTests
|
||||
/// <inheritdoc />
|
||||
public ChannelReader<MxEvent> Reader => _channel.Reader;
|
||||
|
||||
/// <summary>Queues an event for the page's pump to read.</summary>
|
||||
/// <param name="mxEvent">The event to queue.</param>
|
||||
public void Write(MxEvent mxEvent) => _channel.Writer.TryWrite(mxEvent);
|
||||
|
||||
/// <summary>Marks the subscription as released and completes its channel.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
@@ -271,6 +328,11 @@ public sealed class SessionDetailsPageEventAclTests
|
||||
/// <summary>Gets exceptions the renderer surfaced, so a test never passes over a swallowed fault.</summary>
|
||||
public List<Exception> Exceptions { get; } = [];
|
||||
|
||||
/// <summary>Gets the number of render batches the renderer produced.</summary>
|
||||
public int RenderBatches => Volatile.Read(ref _renderBatches);
|
||||
|
||||
private int _renderBatches;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Dispatcher Dispatcher { get; } = Dispatcher.CreateDefault();
|
||||
|
||||
@@ -306,7 +368,11 @@ public sealed class SessionDetailsPageEventAclTests
|
||||
protected override void HandleException(Exception exception) => Exceptions.Add(exception);
|
||||
|
||||
/// <inheritdoc />
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user