Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests/Scripts/TriggerExpressionKeysTests.cs
T
Joseph Doherty d3c6295670 perf(site-runtime): route expression triggers by referenced keys; heap cap eviction; O(1) predictor lane (SRT-7, SRT-14, CMN-11, WP6 task 6.2)
SRT-7: TriggerExpressionKeys extracts the literal Attributes["k"] /
Children["c"].Attributes["k"] reads of an Expression trigger; the Instance
Actor routes the child only those attributes (new MonitoredAttributes routing
kind). Anything not pinnable (non-literal key, alias/member access of the
globals, Parent, parse error, zero reads) falls back to all-changes. Child
actors are unchanged (narrow: WP3 edits ScriptActor/AlarmActor).
  Evidence: instance with 250 attributes at 1 Hz and 10 expression children
  each reading 2 attributes — child mailbox deliveries (and per-eval snapshot
  copies + CTS allocations) go from 2,500/s to 20/s (routing-map test pins
  that a non-read attribute has no route).

SRT-14: EnforceCap selects the oldest conditions with a bounded max-heap and
rate-limits the eviction Warning (1/min with an eviction count).
  Stopwatch, n=1001 k=1 per transition at the cap: OrderBy 13.0-18.2 us -> heap 1.2 us.

CMN-11: FlowShapePredictor looks up a node by the index encoded in its
append-only id (O(1)) instead of a linear scan per AddNode/Annotate.
  Stopwatch PredictSiteScript, Release: 5,000 nodes 125.5 -> 54.9 ms,
  20,000 nodes 434.2 -> 83.3 ms.
2026-12-03 09:45:50 -05:00

57 lines
3.0 KiB
C#

using ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.Scripts;
/// <summary>
/// SRT-7 (WP6 6.2): an Expression trigger is routed by the literal attribute keys it reads,
/// and falls back to all-changes whenever those keys cannot be pinned down.
/// </summary>
public class TriggerExpressionKeysTests
{
/// <summary>Literal root and child reads are extracted as canonical keys.</summary>
[Theory]
[InlineData("(double)Attributes[\"Temp\"] > 50", new[] { "Temp" })]
[InlineData("(double)Attributes[\"A\"] > (double)Attributes[\"B\"] && Attributes[\"A\"] != null", new[] { "A", "B" })]
[InlineData("(double)Children[\"Pump1\"].Attributes[\"Speed\"] > 10", new[] { "Pump1.Speed" })]
[InlineData("Attributes[\"Mode\"] as string == \"Auto\" || (bool)Children[\"V\"].Attributes[\"Open\"]", new[] { "Mode", "V.Open" })]
public void LiteralReads_AreExtracted(string expression, string[] expected)
{
Assert.True(TriggerExpressionKeys.TryExtract(expression, out var keys));
Assert.Equal(expected, keys);
}
/// <summary>Anything that is not a recognised literal read falls back to all-changes.</summary>
[Theory]
[InlineData("true")] // no attribute read (may depend on the clock)
[InlineData("DateTime.UtcNow.Second == 0")]
[InlineData("Attributes[\"A\" + \"B\"] != null")] // non-literal key
[InlineData("Attributes[name] != null")]
[InlineData("Parent != null")]
[InlineData("Children[\"P\"].Attributes[k] != null")] // non-literal child key
[InlineData("Children[c].Attributes[\"S\"] != null")] // non-literal child name
[InlineData("((Func<object>)(() => Attributes))() != null")] // alias of the global
[InlineData("(double)Attributes[\"A\"] > 1 && this.Attributes[\"B\"] != null")]
[InlineData("(double)Attributes[\"A\" > 1")] // parse error
[InlineData("")]
public void UnpinnableReads_FallBack(string expression)
{
Assert.False(TriggerExpressionKeys.TryExtract(expression, out _));
}
/// <summary>Routing decisions for Expression scripts and alarms use the extracted keys.</summary>
[Fact]
public void ExpressionRouting_UsesKeysOrFallsBack()
{
var keyed = TriggerRouting.ForScript("Expression", "{\"expression\":\"(double)Attributes[\\\"A\\\"] > 1\"}");
Assert.Equal(TriggerRoutingKind.MonitoredAttributes, keyed.Kind);
Assert.Equal(new[] { "A" }, keyed.MonitoredAttributeSet);
var alarm = TriggerRouting.ForAlarm("Expression", "{\"expression\":\"Children[\\\"P\\\"].Attributes[\\\"X\\\"] != null\"}");
Assert.Equal(TriggerRoutingKind.MonitoredAttributes, alarm.Kind);
Assert.Equal(new[] { "P.X" }, alarm.MonitoredAttributeSet);
Assert.Equal(TriggerRoutingKind.AllChanges, TriggerRouting.ForScript("Expression", "{\"expression\":\"true\"}").Kind);
Assert.Equal(TriggerRoutingKind.AllChanges, TriggerRouting.ForAlarm("Expression", "not json").Kind);
}
}