perf(grpc-data): A3 L2 in-process memory harness (1,000 imported conditions, one mapping) + MaxImportedConditionBytes from the measurement (205,665 B × 2,500)
This commit is contained in:
+95
@@ -0,0 +1,95 @@
|
||||
using System.Diagnostics;
|
||||
using System.Security.Cryptography;
|
||||
using Opc.Ua;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests;
|
||||
|
||||
/// <summary>
|
||||
/// §9.3 L2 (A3 exit, m-T7) in-process measurement harness: consumer memory with 1,000 imported conditions in ONE mapping,
|
||||
/// materialised through the real <see cref="IProjectedConditionSink"/> of a running SDK server — condition nodes and their
|
||||
/// instance-scoped children, ids, the registry and its folder/notifier indexes, the verdict cache and the EventId registry
|
||||
/// (every condition reports one event, filling its N6 slot) — plus the updates the runtime keeps per key (records + infos).
|
||||
/// Opt-in: <c>OTOPCUA_A3_L2=1</c> (it forces full GCs and is not a unit gate). Pass criterion: working-set growth ≤ 200 MiB.
|
||||
/// </summary>
|
||||
[Trait("Feature", "GrpcData"), Trait("Phase", "A3"), Trait("Category", "Perf")]
|
||||
public sealed class ImportedConditionMemoryMeasurementTests
|
||||
{
|
||||
private const string Connection = "conn-l2";
|
||||
private const string Mapping = "map-l2";
|
||||
private const int Conditions = 1_000;
|
||||
|
||||
[Fact]
|
||||
public async Task L2_one_thousand_imported_conditions_in_one_mapping_grow_the_working_set_by_at_most_200_MiB()
|
||||
{
|
||||
if (Environment.GetEnvironmentVariable("OTOPCUA_A3_L2") != "1") Assert.Skip("Set OTOPCUA_A3_L2=1 to run the A3 L2 memory measurement.");
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
await using var rig = await ServerEventRig.StartAsync("a3-l2", encrypted: false, ct);
|
||||
var nm = rig.Nm;
|
||||
nm.EnsureFolder("Plant", null, "Plant", AddressSpaceRealm.Raw);
|
||||
var parent = nm.TryGetFolder("Plant", AddressSpaceRealm.Raw)!.NodeId;
|
||||
var root = new DataAddress("origin", "https://zb.com/otopcua/raw", "string", "Plant");
|
||||
nm.TryEnsureProjectedAnchor(new ProjectedAnchorDefinition(DataRealm.Raw, ProjectedDataIds.Identifier(Mapping, root), parent,
|
||||
"remote", "Plant/remote", Mapping, 1, () => true), out _).ShouldBeTrue();
|
||||
var access = new ImportedConditionEventWireTests.FakeConditionAccess();
|
||||
access.Allowed.Add(ServerEventRig.KeyPrincipal);
|
||||
nm.ProjectedConditionAccess = access;
|
||||
|
||||
var (heap0, ws0) = Measure();
|
||||
var keys = Enumerable.Range(0, Conditions).Select(i => KeyOf("l2-" + i)).ToArray();
|
||||
var first = keys.Select((k, i) => Update(k, i, 3, active: true, acked: false)).ToList();
|
||||
var sw = Stopwatch.StartNew();
|
||||
nm.ApplyConditions(first);
|
||||
var materialise = sw.Elapsed;
|
||||
first.Clear();
|
||||
var (heapMid, _) = Measure();
|
||||
// One more transition each: every condition reports an event (consumer-minted EventId in its N6 registry).
|
||||
var second = keys.Select((k, i) => Update(k, i, 4, active: true, acked: true)).ToList();
|
||||
sw.Restart();
|
||||
nm.ApplyConditions(second);
|
||||
var transition = sw.Elapsed;
|
||||
nm.ImportedConditionCount.ShouldBe(Conditions);
|
||||
// Exercise the verdict cache once per condition (one identity).
|
||||
var identity = new Security.RoleCarryingUserIdentity(new UserNameIdentityToken { UserName = "apikey" }, [], ServerEventRig.KeyPrincipal,
|
||||
"k", null, null);
|
||||
foreach (var k in keys)
|
||||
nm.AuthorizeImportedCondition(identity, Core.Abstractions.OpcUaOperation.Read, nm.ImportedConditionNodeId(Connection, k)).ShouldBeTrue();
|
||||
|
||||
var (heap1, ws1) = Measure();
|
||||
GC.KeepAlive(second);
|
||||
var heapGrowth = heap1 - heap0;
|
||||
var wsGrowth = ws1 - ws0;
|
||||
var line = $"L2: {Conditions} conditions, one mapping: managed heap +{heapGrowth / 1048576.0:F1} MiB ({heapGrowth / Conditions:N0} B/condition; after materialise {(heapMid - heap0) / Conditions:N0}), " +
|
||||
$"working set +{wsGrowth / 1048576.0:F1} MiB ({wsGrowth / Conditions:N0} B/condition); materialise {materialise.TotalMilliseconds:F0} ms, " +
|
||||
$"transition {transition.TotalMilliseconds:F0} ms; events reported {nm.ImportedEventsReported}";
|
||||
TestContext.Current.TestOutputHelper?.WriteLine(line);
|
||||
if (Environment.GetEnvironmentVariable("OTOPCUA_A3_MEASURE_OUT") is { Length: > 0 } outPath) File.AppendAllText(outPath, line + Environment.NewLine);
|
||||
wsGrowth.ShouldBeLessThanOrEqualTo(200L * 1024 * 1024);
|
||||
}
|
||||
|
||||
private static (long Heap, long WorkingSet) Measure()
|
||||
{
|
||||
for (var i = 0; i < 3; i++) { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true, compacting: true); GC.WaitForPendingFinalizers(); }
|
||||
using var process = Process.GetCurrentProcess();
|
||||
process.Refresh();
|
||||
return (GC.GetTotalMemory(forceFullCollection: true), process.WorkingSet64);
|
||||
}
|
||||
|
||||
private static string KeyOf(string seed) =>
|
||||
Convert.ToBase64String(SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(seed))).TrimEnd('=').Replace('+', '-').Replace('/', '_');
|
||||
|
||||
private static ImportedConditionUpdate Update(string key, int index, long version, bool active, bool acked)
|
||||
{
|
||||
var path = $"Plant/dev{index / 10}/HR{index}";
|
||||
var view = new DataView(new DataAddress("origin", "https://zb.com/otopcua/raw", "string", path), DataRealm.Raw, null, 0, path);
|
||||
var placements = new[] { new ImportedConditionPlacement(Mapping, DataRealm.Raw, view, "Plant/remote", path["Plant/".Length..]) };
|
||||
var info = new DataConditionInfo("http://opcfoundation.org/UA/:AlarmConditionType", $"HR{index}", DataConditionSourceKind.Native,
|
||||
[view], (uint)DataConditionMethodSets.Native);
|
||||
var state = new DataConditionState(key, version, false, true, info, active, acked, true, true, 0, 500, $"HR{index} high",
|
||||
StatusCodes.Good.Code, null, 0, DateTimeOffset.UtcNow, true, new DataActivationMarker("inc", 1));
|
||||
return new ImportedConditionUpdate(Connection, state, info, placements, "Plant/remote/" + placements[0].RelativePath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user