/** * For object-store.test.ts: MiB through ObjectMediaStore.begin() in a * process of its own, so the RSS it reports is this upload's alone and not * whatever other test files left for the collector. `retainedMiB` is RSS * after a full collection at every 32 MiB written: what the upload holds, * not how lazily garbage is returned (`peakMiB`). Prints JSON. * * bun tests/fixtures/object-upload-rss.ts */ import { MemoryMediaIndex, ObjectMediaStore, S3Bucket } from '../../src/server.js'; import { toHex } from '../../src/bytes.js'; const [url, prefix, mib] = process.argv.slice(2) as [string, string, string]; const { bucket: s3 } = S3Bucket.fromUrl(url); const store = new ObjectMediaStore(s3, new MemoryMediaIndex(), { prefix, partSize: 8 * 1024 * 1024 }); const chunk = new Uint8Array(1024 * 1024); const total = Number(mib) * 1024 * 1024; Bun.gc(true); const before = process.memoryUsage().rss; let peak = before; let retained = before; const id = toHex(new Uint8Array(16).fill(0x5a)); const up = await store.begin(id); for (let off = 0; off < total; off += chunk.length) { chunk.fill((off / chunk.length) & 0xff); await up.write(chunk); peak = Math.max(peak, process.memoryUsage().rss); if ((off / chunk.length) % 32 === 31) { Bun.gc(true); retained = Math.max(retained, process.memoryUsage().rss); } } await up.commit({ id, owner: 'o', size: total, readTokenHash: null, createdAt: Date.now() }); const at = Math.floor(total / 2); const body = await store.body(id, at - 1, at + 1); const probe = [...new Uint8Array(await new Response(body).arrayBuffer())]; await store.delete(id); const expect = [((at - 1) / chunk.length) & 0xff, (at / chunk.length) & 0xff]; const MiB = 1024 * 1024; console.log(JSON.stringify({ retainedMiB: (retained - before) / MiB, peakMiB: (peak - before) / MiB, readBack: probe.join() === expect.join() }));