Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.MTConnect.IntegrationTests/MTConnectAlarmSignalLatencyTests.cs
T

76 lines
3.8 KiB
C#

using System.Diagnostics;
using System.Text.Json;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.MTConnect.IntegrationTests;
/// <summary>
/// grpc-data A1 exit gate L4 — MTConnect leg: against the live fixture Agent, the latency from InitializeAsync
/// returning (the host's quality-Good edge; the <c>/current</c> baseline is read during it) to the resync emission's
/// <see cref="IAlarmInitialStateSignal.InitialAlarmStateDelivered"/>, over every CONDITION item the Agent declares.
/// N samples, each a fresh driver. Read-only. Results: <c>OTOPCUA_L4_RESULTS</c> when set.
/// </summary>
[Collection(MTConnectAgentCollection.Name)]
[Trait("Category", "LiveIntegration")]
[Trait("Feature", "GrpcData")]
[Trait("Phase", "L4")]
public sealed class MTConnectAlarmSignalLatencyTests(MTConnectAgentFixture agent, ITestOutputHelper output)
{
private const int Samples = 20;
[Fact]
public async Task Resync_signal_latency_from_connected()
{
if (agent.SkipReason is not null) Assert.Skip(agent.SkipReason);
var ct = TestContext.Current.CancellationToken;
var conditions = agent.DeclaredDataItems
.Where(d => string.Equals(d.Category, "CONDITION", StringComparison.OrdinalIgnoreCase) && !d.InComposition)
.Select(d => d.Id)
.ToList();
if (conditions.Count == 0) Assert.Skip($"The Agent at {agent.AgentUri} declares no CONDITION data items.");
var rawTags = conditions.Select(id => new RawTagEntry($"Plant/MT/l4/{id}",
JsonSerializer.Serialize(new { address = id, alarm = new { alarmType = "AlarmCondition" } }), false, "l4")).ToArray();
var latencies = new List<double>();
var withConnect = new List<double>();
for (var i = 0; i < Samples; i++)
{
var driver = new MTConnectDriver(new MTConnectDriverOptions
{
AgentUri = agent.AgentUri,
RequestTimeoutMs = 15_000,
RawTags = rawTags,
Probe = new MTConnectProbeOptions { Enabled = false },
}, $"mtconnect-l4-{i}");
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([.. rawTags.Select(t => t.RawPath)], ct);
var args = await signalled.Task.WaitAsync(TimeSpan.FromSeconds(30), ct);
sw.Stop();
args.ConditionIds.ShouldNotBeEmpty();
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 = "MTConnect", fixture = agent.AgentUri, n = sorted.Length, conditions = conditions.Count,
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);
}
}