Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataWave3PerfTests.cs
T
Joseph Doherty 5cccfd75ce fix(grpc-data): #994 projected cohorts batch, pace and scale past one item; busy origins no longer reset quiet streams
Wave 3 of the 2026-12-02 review (docs/reviews/2026-12-02/01-runtime.md R-2/3/7/8/9/10/12/13/21/22/23/24,
02-opcuaserver.md U-10/11/12/13).

Consumer (ProjectedDataAccess cohort):
- R-2: a cohort's OpenSubscription declared a snapshot budget of 1 item, which the origin uses to size the stream's
  handle table, so the 2nd item was refused LimitExceeded and the cohort re-opened in a loop (two projected items of
  one interval broke each other). Opens/adds/removes are now batched (IDataSubscriptionCommandLimits, <=256 items,
  bounded by estimated snapshot bytes) and paced (32 burst, 32/s, <=4 commands or snapshots outstanding).
- R-7: control events no longer share the bounded frame queue; a failed Remove can no longer Stop() the cohort.
- R-10: a local capacity refusal (TryPublishOrBackpressure -> CapacityExhausted) is retried for up to 2 s instead of
  invalidating healthy evidence and retiring the route.
- R-13: RemoteSubscription mutates its handle/version tables in place; the cohort caches the source per binding and
  drops the per-sample LINQ in EvidenceFor.
- R-8: only an unselectable endpoint arms the discovery backoff; a caller cancel is OperationCanceledException.
- R-24: the condition stream cancels and observes a pending frame read before disposing the enumerator.

Origin (DataSubscriptionSession / SamplePartition / SourceInterestLease):
- R-2 (found by the perf harness): the delivery index was sized MaxPendingFrames x 16 = 1024, so the 1025th item of a
  stream stopped the session after its ack; it now covers MaxMonitoredItems.
- R-3: interest reads count only own-ticket matches (scan budget separate), re-queue a lagging tick, and catch up from
  per-source last-change versions when the bounded history rolled off - no more "Source change history expired".
- R-9: a partition quiet bump resets a lease only when one of ITS sources was invalidated (per-source quiet version)
  or its binding retired; partition-wide invalidation still resets all.
- R-12: the per-item publish verdict is memoised per (catalog instance, authority generation) for <=1 s; Commit
  under the session lock re-checks identity only.
- R-21/R-22/R-23: registry prune is interval/capacity driven; a cleared history session refuses chunks; the snapshot
  barrier is decided and armed under the same gate its release is decided under.

Node manager: U-10 projected live items indexed by node; U-11 anchor removal / root-leaf swap tombstone the anchor's
own monitored node; U-12 per-parent child count; U-13 per-condition verdict index.

Also: Issue934_r1_N1 test clears its metadata failure before ending the stream (test-only sequencing race,
reproduced with a 200 ms delay; production behaviour correct).

Perf (Category=Perf, OTOPCUA_GRPCDATA_PERF=1): 10k-item cohort opens in 3.3 s / 80 commands / 1 stream (before: never
opens); 100 source versions at 1k/s -> 11,036 item values in 569 ms.
2026-12-03 11:59:26 -05:00

82 lines
4.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);
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.
var before = Volatile.Read(ref fixture.Publishes);
allocated = GC.GetTotalAllocatedBytes(precise: true);
watch.Restart();
const int versions = 100;
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(() => fixture.Latest.Values.All(v => v == 1 + versions), 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");
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");
}
}