Files
lmxopcua/tests/ZB.MOM.WW.OtOpcUa.Tests/Metrics/PerformanceMetricsTests.cs
Joseph Doherty 3b2defd94f Phase 0 — mechanical rename ZB.MOM.WW.LmxOpcUa.* → ZB.MOM.WW.OtOpcUa.*
Renames all 11 projects (5 src + 6 tests), the .slnx solution file, all source-file namespaces, all axaml namespace references, and all v1 documentation references in CLAUDE.md and docs/*.md (excluding docs/v2/ which is already in OtOpcUa form). Also updates the TopShelf service registration name from "LmxOpcUa" to "OtOpcUa" per Phase 0 Task 0.6.

Preserves runtime identifiers per Phase 0 Out-of-Scope rules to avoid breaking v1/v2 client trust during coexistence: OPC UA `ApplicationUri` defaults (`urn:{GalaxyName}:LmxOpcUa`), server `EndpointPath` (`/LmxOpcUa`), `ServerName` default (feeds cert subject CN), `MxAccessConfiguration.ClientName` default (defensive — stays "LmxOpcUa" for MxAccess audit-trail consistency), client OPC UA identifiers (`ApplicationName = "LmxOpcUaClient"`, `ApplicationUri = "urn:localhost:LmxOpcUaClient"`, cert directory `%LocalAppData%\LmxOpcUaClient\pki\`), and the `LmxOpcUaServer` class name (class rename out of Phase 0 scope per Task 0.5 sed pattern; happens in Phase 1 alongside `LmxNodeManager → GenericDriverNodeManager` Core extraction). 23 LmxOpcUa references retained, all enumerated and justified in `docs/v2/implementation/exit-gate-phase-0.md`.

Build clean: 0 errors, 30 warnings (lower than baseline 167). Tests at strict improvement over baseline: 821 passing / 1 failing vs baseline 820 / 2 (one flaky pre-existing failure passed this run; the other still fails — both pre-existing and unrelated to the rename). `Client.UI.Tests`, `Historian.Aveva.Tests`, `Client.Shared.Tests`, `IntegrationTests` all match baseline exactly. Exit gate compliance results recorded in `docs/v2/implementation/exit-gate-phase-0.md` with all 7 checks PASS or DEFERRED-to-PR-review (#7 service install verification needs Windows service permissions on the reviewer's box).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 13:57:47 -04:00

153 lines
5.4 KiB
C#

using System;
using System.Threading;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Host.Metrics;
namespace ZB.MOM.WW.OtOpcUa.Tests.Metrics
{
/// <summary>
/// Verifies operation timing aggregation, rolling buffers, and success tracking used by the bridge metrics subsystem.
/// </summary>
public class PerformanceMetricsTests
{
/// <summary>
/// Confirms that a fresh metrics collector reports no statistics.
/// </summary>
[Fact]
public void EmptyState_ReturnsZeroStatistics()
{
using var metrics = new PerformanceMetrics();
var stats = metrics.GetStatistics();
stats.ShouldBeEmpty();
}
/// <summary>
/// Confirms that repeated operation recordings update total and successful execution counts.
/// </summary>
[Fact]
public void RecordOperation_TracksCounts()
{
using var metrics = new PerformanceMetrics();
metrics.RecordOperation("Read", TimeSpan.FromMilliseconds(10));
metrics.RecordOperation("Read", TimeSpan.FromMilliseconds(20), false);
var stats = metrics.GetStatistics();
stats.ShouldContainKey("Read");
stats["Read"].TotalCount.ShouldBe(2);
stats["Read"].SuccessCount.ShouldBe(1);
stats["Read"].SuccessRate.ShouldBe(0.5);
}
/// <summary>
/// Confirms that min, max, and average timing values are calculated from recorded operations.
/// </summary>
[Fact]
public void RecordOperation_TracksMinMaxAverage()
{
using var metrics = new PerformanceMetrics();
metrics.RecordOperation("Write", TimeSpan.FromMilliseconds(10));
metrics.RecordOperation("Write", TimeSpan.FromMilliseconds(30));
metrics.RecordOperation("Write", TimeSpan.FromMilliseconds(20));
var stats = metrics.GetStatistics()["Write"];
stats.MinMilliseconds.ShouldBe(10);
stats.MaxMilliseconds.ShouldBe(30);
stats.AverageMilliseconds.ShouldBe(20);
}
/// <summary>
/// Confirms that the 95th percentile is calculated from the recorded timing sample.
/// </summary>
[Fact]
public void P95_CalculatedCorrectly()
{
using var metrics = new PerformanceMetrics();
for (var i = 1; i <= 100; i++)
metrics.RecordOperation("Op", TimeSpan.FromMilliseconds(i));
var stats = metrics.GetStatistics()["Op"];
stats.Percentile95Milliseconds.ShouldBe(95);
}
/// <summary>
/// Confirms that the rolling buffer keeps the most recent operation durations for percentile calculations.
/// </summary>
[Fact]
public void RollingBuffer_EvictsOldEntries()
{
var opMetrics = new OperationMetrics();
for (var i = 0; i < 1100; i++)
opMetrics.Record(TimeSpan.FromMilliseconds(i), true);
var stats = opMetrics.GetStatistics();
stats.TotalCount.ShouldBe(1100);
// P95 should be from the last 1000 entries (100-1099)
stats.Percentile95Milliseconds.ShouldBeGreaterThan(1000);
}
/// <summary>
/// Confirms that a timing scope records an operation when disposed.
/// </summary>
[Fact]
public void BeginOperation_TimingScopeRecordsOnDispose()
{
using var metrics = new PerformanceMetrics();
using (var scope = metrics.BeginOperation("Test"))
{
// Simulate some work
Thread.Sleep(5);
}
var stats = metrics.GetStatistics();
stats.ShouldContainKey("Test");
stats["Test"].TotalCount.ShouldBe(1);
stats["Test"].SuccessCount.ShouldBe(1);
stats["Test"].AverageMilliseconds.ShouldBeGreaterThan(0);
}
/// <summary>
/// Confirms that a timing scope can mark an operation as failed before disposal.
/// </summary>
[Fact]
public void BeginOperation_SetSuccessFalse()
{
using var metrics = new PerformanceMetrics();
using (var scope = metrics.BeginOperation("Test"))
{
scope.SetSuccess(false);
}
var stats = metrics.GetStatistics()["Test"];
stats.TotalCount.ShouldBe(1);
stats.SuccessCount.ShouldBe(0);
}
/// <summary>
/// Confirms that looking up an unknown operation returns no metrics bucket.
/// </summary>
[Fact]
public void GetMetrics_UnknownOperation_ReturnsNull()
{
using var metrics = new PerformanceMetrics();
metrics.GetMetrics("NonExistent").ShouldBeNull();
}
/// <summary>
/// Confirms that operation names are tracked without case sensitivity.
/// </summary>
[Fact]
public void OperationNames_AreCaseInsensitive()
{
using var metrics = new PerformanceMetrics();
metrics.RecordOperation("Read", TimeSpan.FromMilliseconds(10));
metrics.RecordOperation("read", TimeSpan.FromMilliseconds(20));
var stats = metrics.GetStatistics();
stats.Count.ShouldBe(1);
stats["READ"].TotalCount.ShouldBe(2);
}
}
}