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.
This commit is contained in:
+45
@@ -116,6 +116,51 @@ public class InstanceActorChildRoutingTests : TestKit, IDisposable
|
||||
Assert.Single(actor.AllChangeChildren);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SRT-7 (WP6 6.2): an Expression script/alarm whose reads are literal keys is routed only to
|
||||
/// those attributes, so a change to any other attribute no longer reaches it.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void LiteralKeyExpression_RoutesOnlyToTheAttributesItReads()
|
||||
{
|
||||
var config = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "ExprKeyPump",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute { CanonicalName = "A", Value = "1", DataType = "Double" },
|
||||
new ResolvedAttribute { CanonicalName = "B", Value = "2", DataType = "Double" },
|
||||
new ResolvedAttribute { CanonicalName = "C", Value = "3", DataType = "Double" }
|
||||
],
|
||||
Scripts =
|
||||
[
|
||||
new ResolvedScript
|
||||
{
|
||||
CanonicalName = "S_expr", Code = "return 3;",
|
||||
TriggerType = "Expression",
|
||||
TriggerConfiguration = "{\"expression\":\"(double)Attributes[\\\"A\\\"] > (double)Attributes[\\\"B\\\"]\"}"
|
||||
}
|
||||
],
|
||||
Alarms =
|
||||
[
|
||||
new ResolvedAlarm
|
||||
{
|
||||
CanonicalName = "ExprAlarm",
|
||||
TriggerType = "Expression",
|
||||
TriggerConfiguration = "{\"expression\":\"(double)Attributes[\\\"B\\\"] > 1\"}",
|
||||
PriorityLevel = 1
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var actor = BuildInstance(config);
|
||||
|
||||
Assert.Empty(actor.AllChangeChildren);
|
||||
Assert.Single(actor.ChildrenByMonitoredAttribute["A"]); // the script
|
||||
Assert.Equal(2, actor.ChildrenByMonitoredAttribute["B"].Count); // script + alarm
|
||||
Assert.False(actor.ChildrenByMonitoredAttribute.ContainsKey("C"));
|
||||
}
|
||||
|
||||
/// <summary>Verifies that a monitored-attribute alarm routes only to its attribute while an expression alarm routes to all changes.</summary>
|
||||
[Fact]
|
||||
public void MonitoredAttributeAlarm_RoutesToItsAttribute_ExpressionAlarm_RoutesToAllChanges()
|
||||
|
||||
@@ -309,6 +309,31 @@ public class NativeAlarmActorTests : TestKit, IDisposable
|
||||
TimeSpan.FromSeconds(5));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SRT-14 (WP6 6.2): cap eviction picks the oldest conditions with a bounded heap; the
|
||||
/// selection (and its oldest-first order) matches the full sort it replaced.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(3)]
|
||||
[InlineData(10)]
|
||||
public void SelectOldest_MatchesAFullSort(int count)
|
||||
{
|
||||
var rng = new Random(42);
|
||||
var t0 = DateTimeOffset.UtcNow;
|
||||
var alarms = Enumerable.Range(0, 1001)
|
||||
.Select(i => Transition($"ref-{i}", AlarmTransitionKind.Raise,
|
||||
new AlarmConditionState(true, false, null, AlarmShelveState.Unshelved, false, 800),
|
||||
t0.AddMilliseconds(rng.Next(0, 1_000_000))))
|
||||
.ToList();
|
||||
|
||||
var expected = alarms.OrderBy(a => a.TransitionTime).Take(count).Select(a => a.TransitionTime).ToList();
|
||||
var actual = NativeAlarmActor.SelectOldest(alarms, count).Select(a => a.TransitionTime).ToList();
|
||||
|
||||
Assert.Equal(expected, actual);
|
||||
Assert.Empty(NativeAlarmActor.SelectOldest(alarms, 0));
|
||||
}
|
||||
|
||||
/// <summary>A snapshot swap that drops several conditions deletes their rows via the batched flush, and a delete cancels a pending upsert (review 2026-10-21 01/SR-P2).</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user