Files
MikrotikManager/backend/src/services/server-poll-memory.owned.test.ts
T

118 lines
8.5 KiB
TypeScript

/** Compiled-only candidate sizing: one genuine public MCP poll. This is not a
* whole-installation 2GB acceptance and does not change any production budget. */
import assert from "node:assert/strict"
import { basename, dirname, isAbsolute, resolve } from "node:path"
import { getHeapStatistics } from "node:v8"
import { performance } from "node:perf_hooks"
import { existsSync } from "node:fs"
assert.equal(process.env.MMAPP_OWN_POLL_MEMORY, "true", "MMAPP_OWN_POLL_MEMORY is required before DB imports")
assert.equal(process.env.NODE_ENV, "test")
const endpoint = new URL(process.env.DATABASE_URL ?? "")
assert(["postgres:", "postgresql:"].includes(endpoint.protocol))
assert.equal(endpoint.hostname, "127.0.0.1"); assert.equal(endpoint.username, "mcp_test"); assert(endpoint.password)
assert.equal(endpoint.pathname, "/postgres"); assert.equal(endpoint.search, ""); assert.equal(endpoint.hash, "")
assert(Number.isInteger(Number(endpoint.port)) && Number(endpoint.port) > 0 && Number(endpoint.port) <= 65535)
assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR))
assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state")
assert.match(basename(dirname(resolve(process.env.MCP_STATE_DIR))), /^mm-mcp-test-[A-Za-z0-9-]+$/)
assert.equal(resolve(process.cwd()), dirname(resolve(process.env.MCP_STATE_DIR)), "Use the private owned working directory so dotenv cannot read backend/.env")
assert.equal(basename(dirname(dirname(new URL(import.meta.url).pathname))), "dist-mcp-memory", "Run the emitted compiled entry, never source via tsx")
assert(!process.execArgv.some(argument => /tsx|--import|--require|--loader|^-r$/.test(argument)), "No TS loader or preload may enter the measured child")
assert(!/tsx|--import|--require|--loader|(?:^|\s)-r(?:\s|$)/.test(process.env.NODE_OPTIONS ?? ""))
assert(process.execArgv.includes("--max-old-space-size=192"), "Explicit --max-old-space-size=192 is required before DB imports")
assert(process.execArgv.includes("--max-semi-space-size=16"), "Explicit --max-semi-space-size=16 is required before DB imports")
assert.equal(process.execArgv.filter(argument => /^--max[-_]old[-_]space[-_]size/.test(argument)).length, 1)
assert.equal(process.execArgv.filter(argument => /^--max[-_]semi[-_]space[-_]size/.test(argument)).length, 1)
assert(!/--max[-_](?:old|semi)[-_]space[-_]size/.test(process.env.NODE_OPTIONS ?? ""), "Sizing flags must be explicit and independently inspectable")
const heapLimitBytes = getHeapStatistics().heap_size_limit
const candidateBackendRssBudgetBytes = 512 * 1024 * 1024
assert(heapLimitBytes >= 192 * 1024 * 1024 && heapLimitBytes <= 256 * 1024 * 1024, "Observed V8 heap must match the explicit candidate cap")
assert.notEqual(process.env.MMAPP_OWN_POLL_EXPIRY_DIAGNOSTICS, "true", "Memory acceptance uses the unwrapped production service")
process.env.MMAPP_OWN_SERVER_POLL_STORAGE_FRESH = "true"
process.env.MMAPP_OWN_SQL_DB = "postgres"
const started = performance.now(), before = process.memoryUsage()
const costDiagnostic = process.env.MMAPP_OWN_POLL_COST_DIAGNOSTICS === "true"
const cpuDiagnostic = process.execArgv.some(argument => argument === "--cpu-prof" || argument.startsWith("--cpu-prof="))
const diagnosticOnly = costDiagnostic || cpuDiagnostic
assert([undefined,"false","true"].includes(process.env.MMAPP_OWN_POLL_BOOTSTRAP),"Explicit owned bootstrap selection required")
const bootstrap = process.env.MMAPP_OWN_POLL_BOOTSTRAP === "true"
let bootstrapElapsedMs: number | undefined
const cpuBefore = process.cpuUsage()
let costs: { snapshot(): unknown; restore(): void } | undefined
let costTimer: ReturnType<typeof setInterval> | undefined
const peak = { ...before }, maximumSamples = 10000, intervalMs = 250
let samples = 0
const sample = () => {
if (samples >= maximumSamples) return
samples++
const current = process.memoryUsage()
for (const key of Object.keys(peak) as (keyof NodeJS.MemoryUsage)[]) peak[key] = Math.max(peak[key], current[key])
}
sample()
const sampler = setInterval(sample, intervalMs); sampler.unref()
let closePool: (() => Promise<void>) | undefined, closeApp: (() => Promise<void>) | undefined
try {
if (costDiagnostic) {
const { installServerPollCostDiagnostics } = await import("./server-poll-cost-diagnostics.js")
costs = installServerPollCostDiagnostics()
console.log("Owned poll COST DIAGNOSTIC ONLY: driver observers installed; not unwrapped acceptance")
costTimer = setInterval(() => console.log("Owned poll COST DIAGNOSTIC ONLY progress:", JSON.stringify({ elapsedMs: performance.now() - started, cpu: process.cpuUsage(cpuBefore), sql: costs?.snapshot() })), 15000)
costTimer.unref()
}
// All configured production imports occur after owned endpoint/state/runtime
// guards. The normal app is assembled without scheduler/GeoIP/listener jobs.
const database = await import("../db/index.js"); closePool = database.closePool
const { applySqlMigrations } = await import("../db/migrate.js")
const { MIGRATION_MANIFEST } = await import("../db/migration-manifest.js")
const { buildApp } = await import("../index.js")
const { acceptServerPollPublicRuntimeSql } = await import("../modules/mcp/server-poll-http.sql-helper.js")
if(bootstrap){
const sqlite=resolve(process.env.DATABASE_PATH??"")
assert.equal(dirname(sqlite),process.cwd());assert.equal(existsSync(sqlite),false,"Bootstrap may not import a SQLite source")
const { initDatabase }=await import("../db/bootstrap.js")
const began=performance.now();await initDatabase();bootstrapElapsedMs=performance.now()-began
assert.equal(existsSync(sqlite),false)
}else await applySqlMigrations(database.pool)
assert.equal((await database.pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n, MIGRATION_MANIFEST.length)
assert.equal((await database.pool.query("SELECT count(*)::int AS n FROM server_poll_storage_admissions")).rows[0].n, 0)
const app = await buildApp({ logger: false, startScheduler: false }); closeApp = () => app.close()
await app.ready(); sample()
const appReady = process.memoryUsage()
const runtime = await acceptServerPollPublicRuntimeSql()
assert("actualDefaultPublicRegistry" in runtime,"Sizing requires the positive full poll result")
assert.equal(runtime.actualDefaultPublicRegistry, true); assert.equal(runtime.actualNativeGets, 4)
assert.equal(runtime.dispatchesAfterConsumedAdmission, 4)
assert.equal(runtime.persistedReadAndVerifyNoMutation, true); assert.equal(runtime.idempotentApplyNoNewNativeOrCharge, true)
sample(); const after = process.memoryUsage()
assert(samples < maximumSamples, "The bounded sampler must cover the entire measured workload")
console.log("Owned compiled poll memory:", JSON.stringify({ compiledProductionApp: true, tsxLoaded: false,
nodeVersion: process.version, platform: process.platform, arch: process.arch,
execArgv: process.execArgv, heapLimitBytes, heapLimitMiB: heapLimitBytes / 1024 / 1024,
catalogueInitialization:bootstrap?"actual-initDatabase":"migrations-only",bootstrapElapsedMs,
diagnosticOnly, cpuProfilerEnabled: cpuDiagnostic, sqlCosts: costs?.snapshot(), cpu: process.cpuUsage(cpuBefore),
before, appReady, after, sampledPeak: peak, samples, sampleIntervalMs: intervalMs, maximumSamples,
maxRssKiB: process.resourceUsage().maxRSS, candidateBackendRssBudgetBytes, elapsedMs: performance.now() - started,
currentMigrations: MIGRATION_MANIFEST.length, actualNativeGets: runtime.actualNativeGets,
completedPublications: runtime.completedPublications, journalRows: runtime.journalRows,
schedulerStarted: false, productionDatabaseTouched: false, wholeInstallation2GbAcceptance: false }))
assert(process.resourceUsage().maxRSS * 1024 <= candidateBackendRssBudgetBytes, "Observed backend process RSS exceeds the fixed 512MiB candidate budget")
} catch (error) {
sample()
console.log("Owned compiled poll memory failure:", JSON.stringify({
nodeVersion: process.version, platform: process.platform, arch: process.arch,
compiledProductionApp: true, execArgv: process.execArgv, heapLimitBytes,
catalogueInitialization:bootstrap?"actual-initDatabase":"migrations-only",bootstrapElapsedMs,
sampledPeak: peak, samples, maxRssKiB: process.resourceUsage().maxRSS,
candidateBackendRssBudgetBytes, elapsedMs: performance.now() - started,
diagnosticOnly, cpuProfilerEnabled: cpuDiagnostic, sqlCosts: costs?.snapshot(), cpu: process.cpuUsage(cpuBefore),
workloadAccepted: false, wholeInstallation2GbAcceptance: false,
}))
throw error
} finally {
clearInterval(sampler)
if (costTimer) clearInterval(costTimer)
try { await closeApp?.() } finally { try { await closePool?.() } finally { costs?.restore() } }
}