Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.IntegrationTests/OpcUaClientAlarmSignalLatencyTests.cs
T

70 lines
3.3 KiB
C#

using System.Diagnostics;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.IntegrationTests;
/// <summary>
/// grpc-data A1 exit gate L4 — OpcUaClient leg: against the live <c>opc-plc --alm</c>, the latency from the driver
/// being connected (InitializeAsync returned = the host's quality-Good edge) to
/// <see cref="IAlarmInitialStateSignal.InitialAlarmStateDelivered"/> raised on the alarm subscription's RefreshEnd.
/// N samples, each a fresh driver (connect + alarm subscribe + ConditionRefresh). Read-only against the fixture.
/// Results: <c>OTOPCUA_L4_RESULTS</c> (JSON lines) when set, and the test output.
/// </summary>
[Collection(OpcPlcCollection.Name)]
[Trait("Category", "LiveIntegration")]
[Trait("Feature", "GrpcData")]
[Trait("Phase", "L4")]
public sealed class OpcUaClientAlarmSignalLatencyTests(OpcPlcFixture sim, ITestOutputHelper output)
{
private const int Samples = 20;
[Fact]
public async Task RefreshEnd_signal_latency_from_connected()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var ct = TestContext.Current.CancellationToken;
var latencies = new List<double>();
var withConnect = new List<double>();
for (var i = 0; i < Samples; i++)
{
await using var drv = new OpcUaClientDriver(OpcPlcProfile.BuildOptions(sim.EndpointUrl), $"opcua-l4-{i}");
var signalled = new TaskCompletionSource<AlarmInitialStateEventArgs>(TaskCreationOptions.RunContinuationsAsynchronously);
drv.InitialAlarmStateDelivered += (_, e) => signalled.TrySetResult(e);
drv.OnAlarmEvent += (_, _) => { };
var total = Stopwatch.StartNew();
await drv.InitializeAsync("{}", ct);
var sw = Stopwatch.StartNew();
await drv.SubscribeAlarmsAsync(["Plant/opc/plc/l4"], ct);
var args = await signalled.Task.WaitAsync(TimeSpan.FromSeconds(30), ct);
sw.Stop();
args.Unsignalable.ShouldBeFalse("opc-plc --alm supports ConditionRefresh, so the RefreshEnd signals");
latencies.Add(sw.Elapsed.TotalMilliseconds); withConnect.Add(total.Elapsed.TotalMilliseconds);
await drv.ShutdownAsync(ct);
}
L4Report.Write("OpcUaClient", sim.EndpointUrl, latencies, withConnect, output);
}
}
internal static class L4Report
{
public static void Write(string driverType, string fixture, List<double> ms, List<double> withConnect, ITestOutputHelper output)
{
var sorted = ms.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 = System.Text.Json.JsonSerializer.Serialize(new
{
driverType, fixture, n = sorted.Length, p50 = P(0.50), p99 = P(0.99), max = sorted[^1],
samplesMs = ms.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);
}
}