import { bench, beforeAll, afterAll } from 'vitest' import { AtUri } from '@atproto/syntax' import { TestNetworkNoAppView } from '@atproto/dev-env' import { prepareCreate } from './src/repo/index.js' type Op = () => Promise const records: { uri: string; cid: string; rkey: string }[] = [] let network: TestNetworkNoAppView let did: string let writeIdx = 0 beforeAll(async () => { network = await TestNetworkNoAppView.create({ dbPostgresSchema: 'zds_bench', }) const agent = network.pds.getAgent() await agent.createAccount({ email: 'bench@test.com', handle: 'bench.test', password: 'bench-pass', }) did = agent.assertDid for (let i = 0; i < 1000; i++) { const res = await agent.com.atproto.repo.createRecord({ repo: did, collection: 'app.bsky.feed.post', record: { $type: 'app.bsky.feed.post', text: `official pds bench record ${i}`, createdAt: `2026-05-23T00:${(i % 60).toString().padStart(2, '0')}:00.000Z`, }, }) records.push({ uri: res.data.uri, cid: res.data.cid, rkey: new AtUri(res.data.uri).rkey, }) } }, 120_000) afterAll(async () => { await network.close() }) bench('current record CID lookup', async () => { await currentCidLookup() }) bench('apply one record commit', async () => { await applyOneRecordCommit() }) bench('full getRecord materialization', async () => { await fullGetRecord() }) bench('full listRecords materialization, limit 50', async () => { await fullListRecords() }) bench('current record CID lookup batch, 10 callers x 100 ops', async () => { await runConcurrent(10, 100, currentCidLookup) }) bench('current record CID lookup batch, 100 callers x 20 ops', async () => { await runConcurrent(100, 20, currentCidLookup) }) async function currentCidLookup() { const rec = records[Math.floor(Math.random() * records.length)] const cid = await network.pds.ctx.actorStore.read(did, (store) => store.record.getCurrentRecordCid(new AtUri(rec.uri)), ) if (!cid) throw new Error('missing cid') } async function applyOneRecordCommit() { const i = writeIdx++ const write = await prepareCreate({ did, collection: 'app.bsky.feed.post', validate: false, record: { $type: 'app.bsky.feed.post', text: `official pds write bench ${i}`, createdAt: `2026-05-23T01:${(i % 60).toString().padStart(2, '0')}:00.000Z`, }, }) const commit = await network.pds.ctx.actorStore.transact(did, async (actorTxn) => { const next = await actorTxn.repo.processWrites([write]) await network.pds.ctx.sequencer.sequenceCommit(did, next) return next }) await network.pds.ctx.accountManager.updateRepoRoot(did, commit.cid, commit.rev) } async function fullGetRecord() { const rec = records[Math.floor(Math.random() * records.length)] const record = await network.pds.ctx.actorStore.read(did, (store) => store.record.getRecord(new AtUri(rec.uri), rec.cid), ) if (!record) throw new Error('missing record') } async function fullListRecords() { const rows = await network.pds.ctx.actorStore.read(did, (store) => store.record.listRecordsForCollection({ collection: 'app.bsky.feed.post', limit: 50, reverse: false, }), ) if (rows.length === 0) throw new Error('missing records') } async function runConcurrent(callers: number, opsPerCaller: number, op: Op) { await Promise.all( Array.from({ length: callers }, async () => { for (let i = 0; i < opsPerCaller; i++) { await op() } }), ) }