perf(worker): wnwrap poll skips the parse and diff when the alarm XML is unchanged (MXA-008, #256)

This commit is contained in:
Joseph Doherty
2026-12-03 09:53:39 -05:00
parent a17f24c817
commit 04c772268a
2 changed files with 87 additions and 7 deletions
@@ -724,6 +724,31 @@ public sealed class WnWrapAlarmConsumerXmlTests
// out of one call, produced under one lock acquisition.
// -------------------------------------------------------------------------
/// <summary>
/// MXA-008: an alarm document identical to the previous poll's is not parsed or diffed again and
/// emits nothing; a changed document is parsed and diffed as before.
/// </summary>
[Fact]
public void ProcessFetchedXml_WhenTheDocumentIsUnchanged_SkipsTheParseAndEmitsNothing()
{
using WnWrapAlarmConsumer consumer = new WnWrapAlarmConsumer(1024);
string xml = BuildAlarmXml(3);
IReadOnlyList<MxAlarmTransitionEvent> first = consumer.ProcessFetchedXml(xml);
IReadOnlyList<MxAlarmTransitionEvent> second = consumer.ProcessFetchedXml(new string(xml.ToCharArray()));
Assert.NotEmpty(first);
Assert.Empty(second);
Assert.Equal(1, consumer.XmlParseCount);
Assert.Equal(3, consumer.SnapshotActiveAlarms(out bool truncated).Count);
Assert.False(truncated);
IReadOnlyList<MxAlarmTransitionEvent> changed = consumer.ProcessFetchedXml(BuildAlarmXml(4));
Assert.Equal(2, consumer.XmlParseCount);
Assert.NotEmpty(changed);
Assert.Equal(4, consumer.SnapshotActiveAlarms(out _).Count);
}
/// <summary>
/// A capped fetch sets the truncation verdict handed out with the
/// snapshot. Without this the signal never leaves the consumer and the
@@ -104,6 +104,9 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer
private long lastTruncationWarningMilliseconds = -TruncationWarningIntervalMilliseconds;
private long truncatedFetchCount;
private bool lastSnapshotTruncated;
private string? lastFetchedXml;
private int lastFetchedRecordCount;
private int lastParsedRecordCount;
private wwAlarmConsumerClass? client;
private bool subscribed;
private bool disposed;
@@ -351,6 +354,9 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer
/// </summary>
internal Action<string>? TruncationWarningSink { get; set; }
/// <summary>Gets how many fetched documents were actually parsed (diagnostic, for tests).</summary>
internal int XmlParseCount { get; private set; }
/// <inheritdoc />
public void PollOnce()
{
@@ -367,7 +373,55 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer
string xml = xmlObj?.ToString() ?? string.Empty;
if (xml.Length == 0) return;
Dictionary<Guid, MxAlarmSnapshotRecord> next = ParseSnapshotXml(xml, out int fetchedRecordCount);
IReadOnlyList<MxAlarmTransitionEvent> transitions = ProcessFetchedXml(xml);
if (transitions.Count == 0) return;
EventHandler<MxAlarmTransitionEvent>? handler = AlarmTransitionEmitted;
if (handler is null) return;
foreach (MxAlarmTransitionEvent transition in transitions)
{
handler.Invoke(this, transition);
}
}
/// <summary>
/// Folds one fetched alarm XML document into the retained snapshot and returns the
/// transitions it implies. Split out of <see cref="PollOnce"/> so the fetch-to-fold step is
/// testable without the wnwrap COM object.
/// <para>
/// A document identical to the previous one cannot change the snapshot, the transitions
/// or the truncation verdict, so the parse (about 19 strings and a record per alarm) and
/// the diff are skipped; only the rate-limited truncation warning is still counted, so a
/// galaxy parked above the cap keeps being reported. The BSTR copy itself cannot be
/// avoided.
/// </para>
/// </summary>
/// <param name="xml">The non-empty <c>GetXmlCurrentAlarms2</c> reply.</param>
/// <returns>The transitions the fetch implies; empty when the document is unchanged.</returns>
internal IReadOnlyList<MxAlarmTransitionEvent> ProcessFetchedXml(string xml)
{
int fetchedRecordCount;
int parsedCount;
bool truncated;
lock (syncRoot)
{
if (lastFetchedXml is not null && string.Equals(lastFetchedXml, xml, StringComparison.Ordinal))
{
fetchedRecordCount = lastFetchedRecordCount;
parsedCount = lastParsedRecordCount;
truncated = lastSnapshotTruncated;
int retained = latestSnapshot.Count;
if (truncated)
{
WarnTruncatedFetch(fetchedRecordCount, parsedCount, retained);
}
return Array.Empty<MxAlarmTransitionEvent>();
}
}
Dictionary<Guid, MxAlarmSnapshotRecord> next = ParseSnapshotXml(xml, out fetchedRecordCount);
XmlParseCount++;
// TRUNCATION CLIFF. GetXmlCurrentAlarms2 caps its reply at maxAlmCnt
// and gives no *verified* "there is more" flag, so a reply holding
@@ -381,7 +435,7 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer
// matching the records in the reply every time. It reports the reply,
// not the total, so it cannot tell a capped fetch from a complete one.
// See docs/AlarmProbeFindings.md.)
bool truncated = IsTruncatedFetch(fetchedRecordCount, maxAlarmsPerFetch);
truncated = IsTruncatedFetch(fetchedRecordCount, maxAlarmsPerFetch);
IReadOnlyList<MxAlarmTransitionEvent> transitions =
FoldFetch(next, truncated, out int retainedCount);
@@ -391,13 +445,14 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer
WarnTruncatedFetch(fetchedRecordCount, next.Count, retainedCount);
}
if (transitions.Count == 0) return;
EventHandler<MxAlarmTransitionEvent>? handler = AlarmTransitionEmitted;
if (handler is null) return;
foreach (MxAlarmTransitionEvent transition in transitions)
lock (syncRoot)
{
handler.Invoke(this, transition);
lastFetchedXml = xml;
lastFetchedRecordCount = fetchedRecordCount;
lastParsedRecordCount = next.Count;
}
return transitions;
}
/// <summary>