Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataWave3PerfTests.cs
T
Joseph Doherty b03e01dae1 perf(grpc-data): #1105 a queued cohort frame rides the channel as a struct, not a FrameEvent record
Release, 10k cohort: ~1.69 KB -> 1.65-1.67 KB per delivered value process-wide; queue section 40 B -> 0 B per value.

- ProjectedDataAccess's cohort channel carries a CohortMessage struct (control Event, or frame + charge + received
  timestamp); control events convert implicitly, so every writer is unchanged. Slot release, charge and dispose obligations
  on the read, drain and finally paths are unchanged.
- GrpcDataWave3PerfTests asserts queue 0 B/value, build < 192 B/value and (Release) < 1.8 KB/value process-wide.

Declined (documented in docs/DataConnections.md): ProjectedValueUpdate+DataValueSnapshot (public publish API, retained by the
publisher), the one-element publish list (reuse needs an ownership promise IProjectedValuePublisher does not make), origin
frame records (public Core.Abstractions contract; the frame is owned by the reader), CachedSourceRead (public, escapes,
identity-based once-only guards).
2026-12-04 20:25:00 -05:00

103 lines
6.7 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);
// #1105 regression bounds (Release, 10k cohort: ~1.65 KB/value process-wide after #1105, ~1.69 KB before). A frame rides
// the cohort channel as a struct, so queueing it allocates nothing; a value's build is the two publish-API records only.
fixture.Allocations.Section(ProjectedPublishSection.Queue).PerValue.ShouldBe(0, "a queued frame must not be wrapped in an object");
fixture.Allocations.Section(ProjectedPublishSection.Build).PerValue.ShouldBeLessThan(192,
"a value's build is ProjectedValueUpdate + DataValueSnapshot (184 B) only");
#if !DEBUG
// Debug builds keep unoptimized async state (~2.5 KB/value), so the process-wide bound is a Release figure.
(publishAllocated / (double)Math.Max(1, delivered)).ShouldBeLessThan(1800, "process-wide publish allocation per value regressed");
#endif
}
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");
}
}