railyard/apps/product-analytics/test/a3/capacity-server.ts
caelum0x a98d62c167 perf(product-analytics): A3 capacity within the 192 MiB cap, recording bytes per minute, and the A3 open-source record (A3)
- Recording chunks, their reads and S3 export files use native zlib instead
  of fflate, so compression runs outside the JS heap.
- test/a3/capacity.ts: 10 minutes of A1's 50 events/s plus 20 recording
  sessions flushing every 2 s plus one webhook export: max 186.4 MiB (ps)
  against the 192 MiB cap; inside view rss 100-116 MiB, heap used 15-32
  MiB, no growth. Stored recordings: 13 232 bytes per recorded minute on
  the fixture (49 923 uncompressed). Written to CAPACITY.md.
- NOTICE files for the vendored and ported parts; test/a3/vendor.test.ts
  pins their hashes and checks the served SDK parts equal the wrapper's.
- docs/analytics/oss/A3.md: what was copied, ported and written, the
  aws4fetch substitution for the AWS SDK and why, environment, security
  notes, test map and the hand-offs (A1 flags route merge for segment
  survey targeting, Shell link, package script, NOTICE line, compose env,
  Expo example provider; flags merge switch).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 08:38:02 +03:00

28 lines
1.4 KiB
TypeScript

// The service as its own process for the A3 capacity run: the real app,
// writer, scheduler and A3 jobs (exports every 30 s), recordings written to a
// directory store, one app with recording on.
import { serve } from "@hono/node-server";
import { createService } from "../../src/app.ts";
import { ensurePartitions } from "../../src/jobs/retention.ts";
import { silentLogger } from "../../src/log.ts";
import { MemorySiteDirectory } from "../../src/tenancy/site-directory.ts";
const site = JSON.parse(process.env.CAPACITY_SITE!);
const svc = createService({ databaseUrl: process.env.DATABASE_URL!, sites: new MemorySiteDirectory([site]), log: silentLogger, consoleSecret: process.env.CAPACITY_CONSOLE_SECRET! });
await ensurePartitions(svc.deps.db, new Date());
svc.deps.writer.start();
svc.scheduler.start();
if (process.env.CAPACITY_MEMLOG) {
// Where the memory is: V8 heap, external (Buffers, ArrayBuffers) and the rest.
setInterval(() => {
const m = process.memoryUsage();
const mib = (n: number) => Math.round((n / 1048576) * 10) / 10;
process.stderr.write(`${JSON.stringify({ rss: mib(m.rss), heapTotal: mib(m.heapTotal), heapUsed: mib(m.heapUsed), external: mib(m.external), arrayBuffers: mib(m.arrayBuffers) })}\n`);
}, 15_000).unref();
}
serve({ fetch: svc.app.fetch, port: Number(process.env.PORT), hostname: "127.0.0.1" }, () => process.send?.("ready"));
process.on("SIGTERM", async () => {
await svc.close();
process.exit(0);
});