Files
lmxopcua/tests/ZB.MOM.WW.LmxOpcUa.Tests/Metrics/PerformanceMetricsTests.cs
Joseph Doherty a7576ffb38 Implement LmxOpcUa server — all 6 phases complete
Full OPC UA server on .NET Framework 4.8 (x86) exposing AVEVA System
Platform Galaxy tags via MXAccess. Mirrors Galaxy object hierarchy as
OPC UA address space, translating contained-name browse paths to
tag-name runtime references.

Components implemented:
- Configuration: AppConfiguration with 4 sections, validator
- Domain: ConnectionState, Quality, Vtq, MxDataTypeMapper, error codes
- MxAccess: StaComThread, MxAccessClient (partial classes), MxProxyAdapter
  using strongly-typed ArchestrA.MxAccess COM interop
- Galaxy Repository: SQL queries (hierarchy, attributes, change detection),
  ChangeDetectionService with auto-rebuild on deploy
- OPC UA Server: LmxNodeManager (CustomNodeManager2), LmxOpcUaServer,
  OpcUaServerHost with programmatic config, SecurityPolicy None
- Status Dashboard: HTTP server with HTML/JSON/health endpoints
- Integration: Full 14-step startup, graceful shutdown, component wiring

175 tests (174 unit + 1 integration), all passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 05:55:27 -04:00

123 lines
4.0 KiB
C#

using System;
using Shouldly;
using Xunit;
using ZB.MOM.WW.LmxOpcUa.Host.Metrics;
namespace ZB.MOM.WW.LmxOpcUa.Tests.Metrics
{
public class PerformanceMetricsTests
{
[Fact]
public void EmptyState_ReturnsZeroStatistics()
{
using var metrics = new PerformanceMetrics();
var stats = metrics.GetStatistics();
stats.ShouldBeEmpty();
}
[Fact]
public void RecordOperation_TracksCounts()
{
using var metrics = new PerformanceMetrics();
metrics.RecordOperation("Read", TimeSpan.FromMilliseconds(10), true);
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);
}
[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);
}
[Fact]
public void P95_CalculatedCorrectly()
{
using var metrics = new PerformanceMetrics();
for (int i = 1; i <= 100; i++)
metrics.RecordOperation("Op", TimeSpan.FromMilliseconds(i));
var stats = metrics.GetStatistics()["Op"];
stats.Percentile95Milliseconds.ShouldBe(95);
}
[Fact]
public void RollingBuffer_EvictsOldEntries()
{
var opMetrics = new OperationMetrics();
for (int 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);
}
[Fact]
public void BeginOperation_TimingScopeRecordsOnDispose()
{
using var metrics = new PerformanceMetrics();
using (var scope = metrics.BeginOperation("Test"))
{
// Simulate some work
System.Threading.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);
}
[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);
}
[Fact]
public void GetMetrics_UnknownOperation_ReturnsNull()
{
using var metrics = new PerformanceMetrics();
metrics.GetMetrics("NonExistent").ShouldBeNull();
}
[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);
}
}
}