acdream/tests/AcDream.App.Tests/Diagnostics/ConnectedR6SoakContractTests.cs
Erik 7b456b49d6 perf(diag): per-frame history export + checkpoint LOH/cache counters + soak capped mode (2026-07-24 audit review)
An adversarial performance review found our own instruments cannot
measure the project's own performance gates:

- FrameProfiler aggregated CPU/GPU/alloc/stage samples into ~5-second
  windows and reset the ring buffers after each report, so route-wide
  p50/p95/p99 distributions across a whole soak could not be
  reconstructed after the fact. ACDREAM_FRAME_HISTORY=<path> now opts
  into a separate per-frame history (one record per frame, ~72
  bytes/record, accumulated in memory with zero frame-thread I/O) that
  a shutdown-only Dispose() writes as CSV. The aggregated [frame-prof]
  report format and its existing metrics are unchanged.

- The canonical checkpoint JSON tracked cache residency (entry/byte
  counts) but never LOH size/fragmentation, process-wide allocated
  bytes, or cache hit/miss/eviction traffic — a committed audit JSON
  showed 65% LOH fragmentation that no tracked instrument recorded,
  and "does a revisit portal hit or miss the caches" was unanswerable
  from an artifact alone. WorldLifecycleResourceSnapshot now carries
  loh_size_bytes/loh_fragmentation_bytes (GCMemoryInfo.GenerationInfo
  index 3), process_total_allocated_bytes (GC.GetTotalAllocatedBytes),
  and Interlocked hit/miss/eviction counters for the CPU mesh cache,
  decoded-texture cache, and the four bounded DAT-object caches
  (portal/cell/highRes/language, aggregated).

- run-connected-r6-soak.ps1 unconditionally forced
  ACDREAM_UNCAPPED_RENDER=1 with no capped mode, while its sibling
  lifecycle-gate script correctly gated it behind a switch. Added
  -Uncapped (default capped, matching the sibling script's pattern),
  fixed the stationary dwell (12s -> 26s, past the 25s
  LiveEntityLivenessController deadline the adjacent comment already
  cited), and now write an env-disclosure.json into the automation
  artifact directory before every launch listing every ACDREAM_* var
  the script sets plus -Uncapped, since the prior audit could only see
  ACDREAM_DUMP_MOVE_TRUTH and nothing else was ever recorded anywhere.

Cache counters are wired via the existing composition path
(ObjectMeshManager already owns the CPU mesh cache and the mesh
extractor directly; content.Dats is threaded into
WorldLifecycleResourceSnapshotSource the same way every other
composition consumer receives it). The DAT-object cache lives behind
IDatReaderWriter, a third-party interface from the DatReaderWriter
package that cannot be extended; RuntimeDatCollection (the one
production implementation) exposes the aggregate stats directly and a
pattern match reads them, degrading to zero for any test double —
no new static registry was introduced (GpuMemoryTracker remains the
one precedented process-wide static).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
(cherry picked from commit 1da2c33c875b41fa383dd79694ee2765f0e21896)
2026-07-24 12:00:30 +02:00

223 lines
7.8 KiB
C#

namespace AcDream.App.Tests.Diagnostics;
public sealed class ConnectedR6SoakContractTests
{
private static readonly string[] ExpectedCheckpointNames =
[
"caul-baseline",
"sawato-baseline",
"rynthid",
"aerlinthe",
"sawato-return",
"holtburg",
"caul-return",
"sawato-plateau",
"caul-plateau",
];
[Fact]
public void RouteContainsExactlyNineOrderedCheckpointBarriers()
{
string[] checkpoints = File.ReadAllLines(Path.Combine(
FindRepoRoot(),
"tools",
"connected-r6-soak.route.txt"))
.Select(static line => line.Trim())
.Where(static line => line.StartsWith(
"checkpoint ",
StringComparison.Ordinal))
.Select(static line => line["checkpoint ".Length..])
.ToArray();
Assert.Equal(ExpectedCheckpointNames, checkpoints);
string[] teleports = File.ReadAllLines(Path.Combine(
FindRepoRoot(),
"tools",
"connected-r6-soak.route.txt"))
.Select(static line => line.Trim())
.Where(static line => line.StartsWith(
"command /teleloc ",
StringComparison.Ordinal))
.ToArray();
Assert.Equal(9, teleports.Length);
Assert.All(teleports, static line => Assert.EndsWith(
" 1 0 0 0",
line,
StringComparison.Ordinal));
}
[Fact]
public void GateConsumesCanonicalTimelineWithoutWeakeningResidencyFormulas()
{
string source = File.ReadAllText(Path.Combine(
FindRepoRoot(),
"tools",
"run-connected-r6-soak.ps1"));
AssertAppearsInOrder(
source,
"$expectedCheckpointNames = @(",
"'caul-baseline'",
"'sawato-baseline'",
"'rynthid'",
"'aerlinthe'",
"'sawato-return'",
"'holtburg'",
"'caul-return'",
"'sawato-plateau'",
"'caul-plateau'");
AssertAppearsInOrder(
source,
"$canonicalCheckpoints = @(Read-CanonicalCheckpoints)",
"Add-CanonicalCheckpointFailures $process",
"Add-RelativeGateFailures");
Assert.Contains("CanonicalCheckpoints = @($canonicalCheckpoints)", source);
Assert.Contains("CheckpointTimeline = $checkpointTimeline", source);
Assert.Contains(
"Join-Path $artifactDir \"checkpoint-$expectedName.json\"",
source,
StringComparison.Ordinal);
Assert.Contains(
"missing render-world synchronization fields",
source,
StringComparison.Ordinal);
Assert.Contains(
"canonical cache '$field' grew",
source,
StringComparison.Ordinal);
Assert.Contains(
"if ($workloadChanged) { $warnings.Add($message) }",
source,
StringComparison.Ordinal);
string[] zeroFields =
[
"pendingLiveTeardowns",
"pendingLandblockRetirements",
"stagedMeshUploads",
"stagedMeshBytes",
"compositeWarmupPending",
];
foreach (string field in zeroFields)
Assert.Contains($"'{field}'", source, StringComparison.Ordinal);
Assert.Equal(1, CountOccurrences(
source,
"$privateLimit = [Math]::Max(192.0, $pair.First.PrivateMiB * 0.20)"));
Assert.Equal(1, CountOccurrences(
source,
"$workingLimit = [Math]::Max(192.0, $pair.First.WorkingSetMiB * 0.20)"));
Assert.Equal(1, CountOccurrences(
source,
"$updateLimit = $pair.First.UpdateP95Ms * 1.5 + 0.5"));
Assert.Equal(1, CountOccurrences(
source,
"$allocLimit = $pair.First.AllocP50Kb * 1.5 + 16.0"));
}
[Fact]
public void UncappedRenderDefaultsToCappedAndIsGatedByAParameter()
{
// 2026-07-24 measurement-tooling review: the soak unconditionally
// forced ACDREAM_UNCAPPED_RENDER=1 with no capped mode, while the
// sibling lifecycle gate script exposes both. Pin the fix so it
// cannot silently regress back to an unconditional '1'.
string source = File.ReadAllText(Path.Combine(
FindRepoRoot(),
"tools",
"run-connected-r6-soak.ps1"));
Assert.Contains("[switch]$Uncapped", source, StringComparison.Ordinal);
Assert.Contains(
"$env:ACDREAM_UNCAPPED_RENDER = if ($Uncapped) { '1' } else { $null }",
source,
StringComparison.Ordinal);
Assert.DoesNotContain(
"$env:ACDREAM_UNCAPPED_RENDER = '1'",
source,
StringComparison.Ordinal);
}
[Fact]
public void StationaryDwellSamplesAfterTheLivenessDeadline()
{
// LiveEntityLivenessController.DestructionTimeoutSeconds is 25s; the
// stationary sample must dwell past it or a prior destination's
// records can masquerade as retained memory in the return gate.
// The script previously slept only 12s despite the comment already
// citing the 25s deadline.
string source = File.ReadAllText(Path.Combine(
FindRepoRoot(),
"tools",
"run-connected-r6-soak.ps1"));
AssertAppearsInOrder(
source,
"LiveEntityLivenessController's authoritative absence deadline is 25",
"Start-Sleep -Seconds 26");
}
[Fact]
public void LaunchConfigurationIsDisclosedToTheArtifactDirectoryBeforeLaunch()
{
// 2026-07-24 measurement-tooling review: a prior audit could only
// observe ACDREAM_DUMP_MOVE_TRUTH because no other ACDREAM_* launch
// flag the script sets was ever recorded anywhere machine-readable.
string source = File.ReadAllText(Path.Combine(
FindRepoRoot(),
"tools",
"run-connected-r6-soak.ps1"));
AssertAppearsInOrder(
source,
"Get-ChildItem Env: | Where-Object { $_.Name -like 'ACDREAM_*' }",
"Join-Path $artifactDir 'env-disclosure.json'",
"try {");
Assert.Contains(
"EnvDisclosure = Join-Path $artifactDir 'env-disclosure.json'",
source,
StringComparison.Ordinal);
// ACDREAM_DUMP_MOVE_TRUTH stays SET (the exercise gate greps its
// output) — it is now disclosed, not removed.
Assert.Contains("$env:ACDREAM_DUMP_MOVE_TRUTH = '1'", source, StringComparison.Ordinal);
}
private static int CountOccurrences(string source, string value)
{
int count = 0;
int cursor = 0;
while ((cursor = source.IndexOf(value, cursor, StringComparison.Ordinal)) >= 0)
{
count++;
cursor += value.Length;
}
return count;
}
private static void AssertAppearsInOrder(string source, params string[] values)
{
int cursor = -1;
foreach (string value in values)
{
int next = source.IndexOf(value, cursor + 1, StringComparison.Ordinal);
Assert.True(next >= 0, $"Missing expected source fragment: {value}");
Assert.True(next > cursor, $"Out-of-order source fragment: {value}");
cursor = next;
}
}
private static string FindRepoRoot()
{
DirectoryInfo? directory = new(AppContext.BaseDirectory);
while (directory is not null)
{
if (File.Exists(Path.Combine(directory.FullName, "AcDream.slnx")))
return directory.FullName;
directory = directory.Parent;
}
throw new DirectoryNotFoundException("Could not find AcDream.slnx.");
}
}