Measure reactive performance across runtimes
This commit is contained in:
parent
af4efb8784
commit
44abedfc9f
6 changed files with 390 additions and 5 deletions
141
scripts/measure-reactive-document-performance.mjs
Normal file
141
scripts/measure-reactive-document-performance.mjs
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
import { monitorEventLoopDelay, performance } from "node:perf_hooks";
|
||||
import process from "node:process";
|
||||
|
||||
import { CultMesh } from "../packages/cultmesh-ts/dist/index.js";
|
||||
|
||||
const quick = process.argv.includes("--quick");
|
||||
const idleMs = quick ? 250 : 10_000;
|
||||
const activeMs = quick ? 500 : 10_000;
|
||||
const results = [];
|
||||
|
||||
for (const documentCount of [1, 100, 1000]) {
|
||||
results.push(await measure(documentCount));
|
||||
}
|
||||
|
||||
console.log(JSON.stringify({
|
||||
runtime: "typescript",
|
||||
workload: {
|
||||
documentCounts: [1, 100, 1000],
|
||||
payloadBytes: 16 * 1024,
|
||||
idleSeconds: idleMs / 1000,
|
||||
activeSeconds: activeMs / 1000,
|
||||
updateRateHz: 60,
|
||||
changedFraction: 0.01,
|
||||
},
|
||||
results,
|
||||
}, undefined, 2));
|
||||
|
||||
if (results.some(result =>
|
||||
result.actualPublishes !== result.expectedPublishes ||
|
||||
result.p99PublishLatencyMilliseconds >= 250 ||
|
||||
result.activeHeapPeakGrowthBytes >= 128 * 1024 * 1024)) {
|
||||
process.exitCode = 1;
|
||||
}
|
||||
|
||||
async function measure(documentCount) {
|
||||
const payload = "x".repeat(16 * 1024);
|
||||
const changedDocumentCount = Math.max(1, Math.floor(documentCount / 100));
|
||||
const reactiveDocuments = [];
|
||||
const publishSignals = new Array(documentCount);
|
||||
const updateStartedAt = new Float64Array(documentCount);
|
||||
const latencies = [];
|
||||
let payloadBytesPublished = 0;
|
||||
let publishes = 0;
|
||||
|
||||
for (let index = 0; index < documentCount; index++) {
|
||||
let current = { id: `performance:${index}`, payload, revision: 1 };
|
||||
let watcher;
|
||||
const document = CultMesh.document(
|
||||
`performance:${index}`,
|
||||
{ schemaId: "gamecult.mesh.performance_probe.v1" },
|
||||
async () => current,
|
||||
{
|
||||
submitPrediction: async (_context, value) => {
|
||||
payloadBytesPublished += Buffer.byteLength(JSON.stringify(value));
|
||||
latencies.push(performance.now() - updateStartedAt[index]);
|
||||
publishes++;
|
||||
current = value;
|
||||
watcher?.(value);
|
||||
publishSignals[index]?.();
|
||||
},
|
||||
watchDocument: (_context, callback) => {
|
||||
watcher = callback;
|
||||
return () => { watcher = undefined; };
|
||||
},
|
||||
},
|
||||
);
|
||||
const reactive = document.predictionWriter().reactive();
|
||||
reactiveDocuments.push(reactive);
|
||||
}
|
||||
|
||||
await Promise.all(reactiveDocuments.map(document => document.ready));
|
||||
collect();
|
||||
const idleHeapStart = process.memoryUsage().heapUsed;
|
||||
const idleCpuStart = process.cpuUsage();
|
||||
await delay(idleMs);
|
||||
collect();
|
||||
const idleHeapGrowthBytes = process.memoryUsage().heapUsed - idleHeapStart;
|
||||
const idleCpu = process.cpuUsage(idleCpuStart);
|
||||
|
||||
collect();
|
||||
const activeHeapStart = process.memoryUsage().heapUsed;
|
||||
let activeHeapPeak = activeHeapStart;
|
||||
const activeCpuStart = process.cpuUsage();
|
||||
const eventLoop = monitorEventLoopDelay({ resolution: 10 });
|
||||
eventLoop.enable();
|
||||
const activeStartedAt = performance.now();
|
||||
let frames = 0;
|
||||
while (performance.now() - activeStartedAt < activeMs) {
|
||||
const completions = [];
|
||||
for (let index = 0; index < changedDocumentCount; index++) {
|
||||
completions.push(new Promise(resolve => { publishSignals[index] = resolve; }));
|
||||
updateStartedAt[index] = performance.now();
|
||||
reactiveDocuments[index].update(value => { value.revision++; });
|
||||
}
|
||||
await Promise.all(completions);
|
||||
frames++;
|
||||
activeHeapPeak = Math.max(activeHeapPeak, process.memoryUsage().heapUsed);
|
||||
const remaining = frames * (1000 / 60) - (performance.now() - activeStartedAt);
|
||||
if (remaining > 0) await delay(remaining);
|
||||
}
|
||||
eventLoop.disable();
|
||||
const activeElapsedSeconds = (performance.now() - activeStartedAt) / 1000;
|
||||
const activeCpu = process.cpuUsage(activeCpuStart);
|
||||
const ordered = latencies.toSorted((left, right) => left - right);
|
||||
|
||||
for (const document of reactiveDocuments) document.dispose();
|
||||
|
||||
return {
|
||||
documentCount,
|
||||
changedDocumentCount,
|
||||
frames,
|
||||
expectedPublishes: frames * changedDocumentCount,
|
||||
actualPublishes: publishes,
|
||||
payloadBytesPublished,
|
||||
idleHeapGrowthBytes,
|
||||
idleCpuMilliseconds: (idleCpu.user + idleCpu.system) / 1000,
|
||||
activeHeapPeakGrowthBytes: activeHeapPeak - activeHeapStart,
|
||||
activeCpuMilliseconds: (activeCpu.user + activeCpu.system) / 1000,
|
||||
publishedPayloadBytesPerSecond: payloadBytesPublished / activeElapsedSeconds,
|
||||
p50PublishLatencyMilliseconds: percentile(ordered, 0.50),
|
||||
p95PublishLatencyMilliseconds: percentile(ordered, 0.95),
|
||||
p99PublishLatencyMilliseconds: percentile(ordered, 0.99),
|
||||
p99EventLoopDelayMilliseconds: eventLoop.percentile(99) / 1_000_000,
|
||||
};
|
||||
}
|
||||
|
||||
function percentile(ordered, ratio) {
|
||||
if (ordered.length === 0) return 0;
|
||||
return ordered[Math.max(0, Math.ceil(ordered.length * ratio) - 1)];
|
||||
}
|
||||
|
||||
function collect() {
|
||||
if (typeof globalThis.gc !== "function") {
|
||||
throw new Error("Run this probe with node --expose-gc.");
|
||||
}
|
||||
globalThis.gc();
|
||||
}
|
||||
|
||||
function delay(milliseconds) {
|
||||
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
||||
}
|
||||
|
|
@ -1,9 +1,43 @@
|
|||
param(
|
||||
[switch] $Quick
|
||||
[switch] $Quick,
|
||||
[switch] $SkipRuntimeBuild,
|
||||
[string] $PythonPath = "",
|
||||
[string] $NodePath = ""
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$root = Split-Path -Parent $PSScriptRoot
|
||||
$nodeExecutable = if ([string]::IsNullOrWhiteSpace($NodePath)) {
|
||||
(Get-Command node -ErrorAction Stop).Source
|
||||
}
|
||||
else {
|
||||
[IO.Path]::GetFullPath($NodePath)
|
||||
}
|
||||
$pythonExecutable = if ([string]::IsNullOrWhiteSpace($PythonPath)) {
|
||||
(Get-Command python -ErrorAction Stop).Source
|
||||
}
|
||||
else {
|
||||
[IO.Path]::GetFullPath($PythonPath)
|
||||
}
|
||||
foreach ($executable in @($nodeExecutable, $pythonExecutable)) {
|
||||
if (-not (Test-Path -LiteralPath $executable -PathType Leaf)) {
|
||||
throw "Performance probe runtime not found: $executable"
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $SkipRuntimeBuild) {
|
||||
$typescript = Join-Path $root "node_modules\typescript\bin\tsc"
|
||||
foreach ($dependency in @("cultcache-ts", "cultnet-ts", "cultmesh-ts")) {
|
||||
$dependencyRoot = Join-Path $root "packages\$dependency"
|
||||
if ($dependency -eq "cultnet-ts") {
|
||||
& $nodeExecutable (Join-Path $dependencyRoot "tools\generate-swarm-contracts.mjs")
|
||||
if ($LASTEXITCODE -ne 0) { throw "CultNet TypeScript contract generation failed." }
|
||||
}
|
||||
& $nodeExecutable $typescript -p (Join-Path $dependencyRoot "tsconfig.json") --pretty false
|
||||
if ($LASTEXITCODE -ne 0) { throw "TypeScript performance dependency build failed: $dependency" }
|
||||
}
|
||||
}
|
||||
|
||||
$project = Join-Path $root "tools\GameCult.Mesh.PerformanceProbe\GameCult.Mesh.PerformanceProbe.csproj"
|
||||
$buildArguments = @(
|
||||
"build",
|
||||
|
|
@ -24,5 +58,31 @@ if ($Quick) {
|
|||
|
||||
& dotnet @arguments
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "CultMesh reactive document performance probe failed."
|
||||
throw "CultMesh C# reactive document performance probe failed."
|
||||
}
|
||||
|
||||
$runtimeArguments = @()
|
||||
if ($Quick) { $runtimeArguments += "--quick" }
|
||||
|
||||
& $nodeExecutable --expose-gc (Join-Path $root "scripts\measure-reactive-document-performance.mjs") @runtimeArguments
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "CultMesh TypeScript reactive document performance probe failed."
|
||||
}
|
||||
|
||||
$pythonSource = Join-Path $root "packages\cultcache-py\src"
|
||||
$previousPythonPath = $env:PYTHONPATH
|
||||
$env:PYTHONPATH = if ([string]::IsNullOrWhiteSpace($previousPythonPath)) {
|
||||
$pythonSource
|
||||
}
|
||||
else {
|
||||
$pythonSource + [IO.Path]::PathSeparator + $previousPythonPath
|
||||
}
|
||||
try {
|
||||
& $pythonExecutable (Join-Path $root "packages\cultcache-py\tools\reactive_performance_probe.py") @runtimeArguments
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "CultMesh Python reactive document performance probe failed."
|
||||
}
|
||||
}
|
||||
finally {
|
||||
$env:PYTHONPATH = $previousPythonPath
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue