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; /// /// #994 perf harness (opt-in: OTOPCUA_GRPCDATA_PERF=1, Category=Perf): 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. /// [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"); } }