Files
lmxopcua/docs/plans/evidence/grpc-data-ext/harness/Program.cs
T

41 lines
3.2 KiB
C#

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<string,int> kinds) {
kinds[n.NodeClass.ToString()] = kinds.GetValueOrDefault(n.NodeClass.ToString()) + 1;
var children = new List<BaseInstanceState>(); 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<NodeState, AlarmConditionState> 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<double>.Implementation<VariantBuilder>(alarm);
alarm.Create(ctx, new NodeId("cond", 2), new QualifiedName("cond", 2), new LocalizedText("cond"), true);
var kinds = new Dictionary<string,int>();
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<object>(); 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<double>.Implementation<VariantBuilder>(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<OffNormalAlarmState>().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"); }
}
}