Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataWave3PerfTests.cs
T
Joseph Doherty b9bd7960de test(grpc-data): #1098 attribute the consumer side of the projected publish path per thread
ProjectedDataAccess gets a test-only IProjectedPublishProbe (null in production) over six synchronous
sections: the stream pump, the frame queue, the cohort loop's dispatch, a value's build, its publication
and the frame's release. The perf harness prints them and the remainder against the process-wide figure.
Release, 10k cohort, 5 runs: the ~600-650 B/value that #1094 left unattributed is Pump 84-209 (the LINQ
Sum's boxed enumerator plus SemaphoreSlim.WaitAsync under a full frame window), Queue 40 (FrameEvent),
Build 184 (DataValueSnapshot + ProjectedValueUpdate), Publish 208 (the latest-sample cache re-wrapping
the update in two identical records, plus the one-element list), Release 55-88 (enumerators boxed by
the frame disposes); 15-51 B/value stays unattributed (other threads).
2026-12-04 17:36:54 -05:00

94 lines
5.9 KiB
C#

using System.Diagnostics;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
/// <summary>
/// #994 perf harness (opt-in: <c>OTOPCUA_GRPCDATA_PERF=1</c>, <c>Category=Perf</c>): a 10k-item projected cohort opened against
/// a real origin session, a sustained value publish through it, and the write-registry claim cost. Numbers go to the test
/// output; the assertions are loose sanity bounds only.
/// </summary>
[Trait("Category", "Perf"), Trait("Issue", "994")]
public sealed class GrpcDataWave3PerfTests(ITestOutputHelper output)
{
private static bool Enabled => Environment.GetEnvironmentVariable("OTOPCUA_GRPCDATA_PERF") == "1";
[Fact(Timeout = 300000)]
public async Task Cohort_of_10k_items_opens_and_carries_a_1k_values_per_second_publish()
{
Assert.SkipUnless(Enabled, "set OTOPCUA_GRPCDATA_PERF=1");
const int items = 10_000;
using var fixture = new CohortScaleFixture(items, maxBatchItems: 1000) { MeasureAllocations = true };
using var access = fixture.Create(items + 16);
var demands = fixture.AcquireAll(access);
try
{
var allocated = GC.GetTotalAllocatedBytes(precise: true);
var watch = Stopwatch.StartNew();
await using (fixture.PumpClock())
{
foreach (var demand in demands) access.StartSubscription(demand, TimeSpan.FromMilliseconds(100)).ShouldBeTrue();
await fixture.WaitForAsync(() => fixture.Baselines.Count == items, TimeSpan.FromSeconds(60), "10k baselines");
}
var open = watch.Elapsed;
var openAllocated = GC.GetTotalAllocatedBytes(precise: true) - allocated;
var commands = fixture.Commands.Count;
output.WriteLine($"open: {items} items in {open.TotalMilliseconds:F0} ms, {commands} commands " +
$"({fixture.Commands.Count(c => c is ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.ChallengeSubscriptionFreshness)} freshness challenges), " +
$"{fixture.Opens} stream(s), {openAllocated / (1024 * 1024)} MiB allocated");
// 1k values/s: 100 source versions 1 ms apart on the fixture clock (1,000 changes/s). The fixture's items share one
// origin source, so each change fans out to every item at its 100 ms publishing interval; count delivered item values.
// #1094: the wait below polls a counter the sink maintains (the old poll copied the 10k-entry Latest.Values every 10 ms), and
// the per-value figures come from per-thread counters on the publish path, so neither the poll nor the clock pump is in them.
var before = Volatile.Read(ref fixture.Publishes);
const int versions = 100;
Volatile.Write(ref fixture.TargetVersion, 1 + versions);
fixture.Allocations.Reset();
allocated = GC.GetTotalAllocatedBytes(precise: true);
watch.Restart();
for (var version = 2; version < 2 + versions; version++)
{
fixture.PublishVersion(version, version);
fixture.Origin.Clock.Advance(TimeSpan.FromMilliseconds(1));
await Task.Delay(1, TestContext.Current.CancellationToken);
}
await using (fixture.PumpClock())
await fixture.WaitForAsync(() => Volatile.Read(ref fixture.AtTargetVersion) >= items, TimeSpan.FromSeconds(60), "final version everywhere");
var publish = watch.Elapsed;
var delivered = Volatile.Read(ref fixture.Publishes) - before;
var publishAllocated = GC.GetTotalAllocatedBytes(precise: true) - allocated;
output.WriteLine($"publish: {versions} source versions -> {delivered} item values delivered in {publish.TotalMilliseconds:F0} ms " +
$"({delivered / Math.Max(0.001, publish.TotalSeconds):F0} values/s), {publishAllocated / Math.Max(1, delivered)} B allocated per value");
output.WriteLine("publish path (per-thread, #1094): " + fixture.Allocations);
var attributed = fixture.Allocations.AttributedPerValue();
output.WriteLine("consumer sections (per-thread, #1098): " + fixture.Allocations.Consumer());
output.WriteLine($"attributed {attributed:F0} B/value of {publishAllocated / Math.Max(1, delivered)} B/value process-wide; " +
$"unattributed {publishAllocated / (double)Math.Max(1, delivered) - attributed:F0} B/value");
var perValue = fixture.Allocations.PerValue();
perValue.Session.ShouldBeGreaterThan(0, "the tick seam must see the publish");
perValue.Consumer.ShouldBeGreaterThan(0, "the consumer seam must see frames");
fixture.Opens.ShouldBeLessThanOrEqualTo(3);
}
finally { foreach (var demand in demands) demand.Dispose(); }
}
[Fact(Timeout = 120000)]
public void Write_registry_claim_against_a_retention_full_table()
{
Assert.SkipUnless(Enabled, "set OTOPCUA_GRPCDATA_PERF=1");
var clock = new ManualTimeProvider();
var registry = new WriteOperationRegistry("inc", 20_000, 256L * 1024 * 1024, TimeSpan.FromSeconds(30), clock);
var hash = new byte[32];
for (var i = 0; i < 5000; i++) { BitConverter.GetBytes(i).CopyTo(hash, 0); registry.Claim("p", "op-" + i, "inc", hash); }
var allocated = GC.GetAllocatedBytesForCurrentThread();
var watch = Stopwatch.StartNew();
for (var i = 0; i < 2000; i++) { BitConverter.GetBytes(100_000 + i).CopyTo(hash, 0); registry.Claim("p", "more-" + i, "inc", hash); }
output.WriteLine($"registry: 2000 claims against 5000-7000 retained entries in {watch.Elapsed.TotalMilliseconds:F1} ms, " +
$"{(GC.GetAllocatedBytesForCurrentThread() - allocated) / 2000} B allocated per claim");
}
}