Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.FOCAS.IntegrationTests/FocasAlarmSignalLatencyTests.cs
T

80 lines
3.8 KiB
C#

using System.Diagnostics;
using System.Text.Json;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Wire;
namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.IntegrationTests;
/// <summary>
/// grpc-data A1 exit gate L4 — FOCAS leg: against the live focas-mock, the latency from InitializeAsync returning
/// (the host's quality-Good edge) to the first <see cref="IAlarmInitialStateSignal.InitialAlarmStateDelivered"/>
/// (first successful <c>cnc_rdalmmsg2</c> tick, default 1 s alarm poll cadence). N samples, each a fresh driver.
/// Read-only: the mock's alarm list is read, never scheduled. Results: <c>OTOPCUA_L4_RESULTS</c> when set.
/// </summary>
[Collection(FocasSimCollection.Name)]
[Trait("Category", "LiveIntegration")]
[Trait("Feature", "GrpcData")]
[Trait("Phase", "L4")]
public sealed class FocasAlarmSignalLatencyTests(FocasSimFixture fx, ITestOutputHelper output)
{
private const int Samples = 20;
[Fact]
public async Task First_alarm_read_signal_latency_from_connected()
{
if (fx.SkipReason is not null) Assert.Skip(fx.SkipReason);
var ct = TestContext.Current.CancellationToken;
const string rawPath = "Plant/Cnc/l4/alarms";
var latencies = new List<double>();
var withConnect = new List<double>();
for (var i = 0; i < Samples; i++)
{
var driver = new FocasDriver(new FocasDriverOptions
{
Devices = [new FocasDeviceOptions(fx.DeviceUri, DeviceName: "l4")],
RawTags =
[
new RawTagEntry(rawPath, JsonSerializer.Serialize(new Dictionary<string, object>
{
["address"] = "ALARM:*",
["dataType"] = "Int32",
["alarm"] = new Dictionary<string, object> { ["alarmType"] = "AlarmCondition" },
}), WriteIdempotent: false, DeviceName: "l4"),
],
Probe = new FocasProbeOptions { Enabled = false },
}, driverInstanceId: $"focas-l4-{i}", clientFactory: new WireFocasClientFactory());
await using (driver)
{
var signalled = new TaskCompletionSource<AlarmInitialStateEventArgs>(TaskCreationOptions.RunContinuationsAsynchronously);
driver.InitialAlarmStateDelivered += (_, e) => signalled.TrySetResult(e);
driver.OnAlarmEvent += (_, _) => { };
var total = Stopwatch.StartNew();
await driver.InitializeAsync("{}", ct);
var sw = Stopwatch.StartNew();
await driver.SubscribeAlarmsAsync([rawPath], ct);
var args = await signalled.Task.WaitAsync(TimeSpan.FromSeconds(30), ct);
sw.Stop();
args.ConditionIds.ShouldBe([rawPath]);
latencies.Add(sw.Elapsed.TotalMilliseconds); withConnect.Add(total.Elapsed.TotalMilliseconds);
await driver.ShutdownAsync(ct);
}
}
var sorted = latencies.OrderBy(x => x).ToArray();
var sc = withConnect.OrderBy(x => x).ToArray();
double Pc(double q) => sc[Math.Clamp((int)Math.Ceiling(q * sc.Length) - 1, 0, sc.Length - 1)];
double P(double q) => sorted[Math.Clamp((int)Math.Ceiling(q * sorted.Length) - 1, 0, sorted.Length - 1)];
var line = JsonSerializer.Serialize(new
{
driverType = "FOCAS", fixture = fx.DeviceUri, n = sorted.Length, p50 = P(0.50), p99 = P(0.99), max = sorted[^1],
samplesMs = latencies.Select(x => Math.Round(x, 1)),
withConnectP50 = Pc(0.50), withConnectP99 = Pc(0.99),
});
output.WriteLine(line);
if (Environment.GetEnvironmentVariable("OTOPCUA_L4_RESULTS") is { Length: > 0 } path)
File.AppendAllText(path, line + Environment.NewLine);
}
}