using Opc.Ua; sealed class Ids : INodeIdFactory { int n; public NodeId New(ISystemContext c, NodeState node) => new NodeId("n" + (++n), 2); } static class P { static int Count(ISystemContext ctx, NodeState n, Dictionary kinds) { kinds[n.NodeClass.ToString()] = kinds.GetValueOrDefault(n.NodeClass.ToString()) + 1; var children = new List(); n.GetChildren(ctx, children); int c = 1; foreach (var ch in children) c += Count(ctx, ch, kinds); return c; } static void Run(string name, Func make, ISystemContext ctx) { var parent = new FolderState(null); var alarm = make(parent); alarm.ConfirmedState = new TwoStateVariableState(alarm); alarm.Confirm = new AddCommentMethodState(alarm); alarm.ShelvingState = new ShelvedStateMachineState(alarm); alarm.MaxTimeShelved = new PropertyState.Implementation(alarm); alarm.Create(ctx, new NodeId("cond", 2), new QualifiedName("cond", 2), new LocalizedText("cond"), true); var kinds = new Dictionary(); int total = Count(ctx, alarm, kinds); Console.WriteLine($"{name}: total={total} ({string.Join(", ", kinds.Select(k => k.Key + "=" + k.Value))}); Quality={(alarm.Quality!=null)} BranchId={(alarm.BranchId!=null)} Comment={(alarm.Comment!=null)}"); } static void Main() { var ctx = new SystemContext((ITelemetryContext)null) { NodeIdFactory = new Ids(), NamespaceUris = new NamespaceTable() }; ctx.NamespaceUris.Append("urn:a"); ctx.NamespaceUris.Append("urn:b"); Run("AlarmConditionState", p => new AlarmConditionState(p), ctx); Run("OffNormalAlarmState", p => new OffNormalAlarmState(p), ctx); Run("DiscreteAlarmState", p => new DiscreteAlarmState(p), ctx); var keep = new List(); long before = GC.GetTotalMemory(true); for (int i = 0; i < 2000; i++) { var parent = new FolderState(null); var a = new OffNormalAlarmState(parent); a.ConfirmedState = new TwoStateVariableState(a); a.Confirm = new AddCommentMethodState(a); a.ShelvingState = new ShelvedStateMachineState(a); a.MaxTimeShelved = new PropertyState.Implementation(a); a.Create(ctx, new NodeId("c"+i, 2), new QualifiedName("c"+i, 2), new LocalizedText("c"+i), true); keep.Add(a); } long after = GC.GetTotalMemory(true); Console.WriteLine($"retained bytes/condition (2000, OffNormal, no address-space indexes): {(after-before)/2000}"); GC.KeepAlive(keep); var conds = keep.Cast().ToArray(); var recs = new object[conds.Length]; for (int w = 0; w < 3; w++) { var sw = System.Diagnostics.Stopwatch.StartNew(); for (int i = 0; i < conds.Length; i++) { var c = conds[i]; recs[i] = (c.NodeId, c.ActiveState?.Id?.Value, c.AckedState?.Id?.Value, c.ConfirmedState?.Id?.Value, c.EnabledState?.Id?.Value, c.ShelvingState?.CurrentState?.Value, c.Severity?.Value, c.Message?.Value, c.Quality?.Value, c.Retain?.Value, c.Time?.Value, c.Comment?.Value, c.ClientUserId?.Value, c.ConditionClassId?.Value, c.ActiveState?.TransitionTime?.Value); } sw.Stop(); Console.WriteLine($"snapshot copy of {conds.Length} conditions: {sw.Elapsed.TotalMilliseconds:F2} ms"); } } }