fix(scripted-alarms): #995 K-3 a deploy reuses unchanged alarm predicates and compiles new ones outside the engine gate
Every LoadAsync cleared the compile cache and recompiled every predicate serially while holding _evalGate (and, uncontended, on the ScriptedAlarmHostActor thread). Now the compile phase runs first on the thread pool (bounded parallelism) without the gate, reusing source-hash-cached compiles; only the swap holds the gate, and CompiledScriptCache.RetainOnly disposes just the retired predicates' ALCs. Out of process the host's validation compile is remembered per source too. Measured, 500 alarms (distinct predicates, in process, Release): reload of unchanged defs 1303 ms -> 15 ms; one edited 1040 -> 16 ms; all-new reload 1002 -> 748 ms; ack issued during that reload waited 800 -> 1 ms.
This commit is contained in:
+86
@@ -0,0 +1,86 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using Serilog;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// #995 K-3 measurement: a deploy with <see cref="AlarmCount"/> scripted alarms. Opt-in and never part of a default
|
||||
/// run — skips unless <c>OTOPCUA_SCRIPT_PERF=1</c>. Reports the first load, a reload of the SAME definitions (what
|
||||
/// every deploy that did not touch an alarm does), a reload with one predicate edited, and how long an operator
|
||||
/// acknowledge issued just after a reload started waits for the engine gate.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Run: <c>OTOPCUA_SCRIPT_PERF=1 dotnet test tests/Core/ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms.Tests -c Release
|
||||
/// --filter Category=Perf --logger "console;verbosity=detailed"</c>.
|
||||
/// </remarks>
|
||||
[Trait("Category", "Perf")]
|
||||
public sealed class ScriptedAlarmEngineReloadPerfTests
|
||||
{
|
||||
private const int AlarmCount = 500;
|
||||
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
||||
|
||||
private static ScriptedAlarmDefinition Alarm(int i, int threshold) => new(
|
||||
AlarmId: $"A{i}",
|
||||
EquipmentPath: "Plant/Line1/Reactor",
|
||||
AlarmName: $"A{i}",
|
||||
Kind: AlarmKind.AlarmCondition,
|
||||
Severity: AlarmSeverity.High,
|
||||
MessageTemplate: "over",
|
||||
PredicateScriptSource: $"return (int)ctx.GetTag(\"T{i % 50}\").Value > {threshold};");
|
||||
|
||||
/// <summary>Measures load / reload / ack-during-reload for 500 alarms with distinct predicates.</summary>
|
||||
[Fact]
|
||||
public async Task Reload_of_500_alarms()
|
||||
{
|
||||
if (Environment.GetEnvironmentVariable("OTOPCUA_SCRIPT_PERF") != "1")
|
||||
Assert.Skip("perf harness: set OTOPCUA_SCRIPT_PERF=1 to run");
|
||||
|
||||
var up = new FakeUpstream();
|
||||
for (var t = 0; t < 50; t++) up.Set($"T{t}", 0);
|
||||
var logger = new LoggerConfiguration().CreateLogger();
|
||||
using var eng = new ScriptedAlarmEngine(up, new InMemoryAlarmStateStore(), new ScriptLoggerFactory(logger), logger);
|
||||
|
||||
var defs = Enumerable.Range(0, AlarmCount).Select(i => Alarm(i, 1000 + i)).ToArray();
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
await eng.LoadAsync(defs, Ct);
|
||||
var first = sw.Elapsed;
|
||||
|
||||
sw.Restart();
|
||||
await eng.LoadAsync(defs, Ct);
|
||||
var same = sw.Elapsed;
|
||||
|
||||
var edited = defs.ToArray();
|
||||
edited[7] = Alarm(7, 4242);
|
||||
sw.Restart();
|
||||
await eng.LoadAsync(edited, Ct);
|
||||
var oneEdited = sw.Elapsed;
|
||||
|
||||
// Ack latency while a reload that must compile everything is in progress (a fresh set of sources).
|
||||
var fresh = Enumerable.Range(0, AlarmCount).Select(i => Alarm(i, 9000 + i)).ToArray();
|
||||
sw.Restart();
|
||||
var reload = Task.Run(() => eng.LoadAsync(fresh, Ct), Ct);
|
||||
await Task.Delay(200, Ct);
|
||||
var ackStart = sw.Elapsed;
|
||||
await eng.AcknowledgeAsync("A1", "op", null, Ct);
|
||||
var ackWait = sw.Elapsed - ackStart;
|
||||
await reload;
|
||||
var freshReload = sw.Elapsed;
|
||||
|
||||
var report = new StringBuilder()
|
||||
.AppendLine($"K-3 scripted-alarm reload, {AlarmCount} alarms (distinct predicates):")
|
||||
.AppendLine($" first load {first.TotalMilliseconds,10:F0} ms")
|
||||
.AppendLine($" reload, same defs {same.TotalMilliseconds,10:F0} ms")
|
||||
.AppendLine($" reload, one edited {oneEdited.TotalMilliseconds,10:F0} ms")
|
||||
.AppendLine($" reload, all new {freshReload.TotalMilliseconds,10:F0} ms")
|
||||
.AppendLine($" ack wait during that {ackWait.TotalMilliseconds,10:F0} ms")
|
||||
.ToString();
|
||||
TestContext.Current.TestOutputHelper?.WriteLine(report);
|
||||
if (Environment.GetEnvironmentVariable("OTOPCUA_SCRIPT_PERF_OUT") is { Length: > 0 } path)
|
||||
await File.AppendAllTextAsync(path, report, Ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user