- BaseKVStorage / BaseEventStorage / BaseDocumentStorage define the API once; implementations provide only raw primitives (client and server share an API) - Drop KV indexes, query planner, unique indexes; document stores take index fields - Escape every char <= '/' so child keys stay contiguous; skip-scan is now simple - Event-sourced documents = event storage + any document store for projections - New folder layout (kv-storage, event-storage, document-storage, utils); tests only in test/ Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { bench, describe } from 'vitest'
|
|
import { KVStorageInMemory } from '../src/kv-storage/kv-storage-in-memory.js'
|
|
|
|
for (const n of [10_000, 100_000]) {
|
|
const db = new KVStorageInMemory()
|
|
await db.writeMany(Array.from({ length: n }, (_, i) => [`users/u${i}`, { name: `user ${i}`, n: i }]))
|
|
const users = db.child('users')
|
|
const keys = Array.from({ length: 100 }, (_, i) => `u${(i * 7919) % n}`)
|
|
let i = 0
|
|
describe(`KV @ ${n / 1000}k keys`, () => {
|
|
bench('get', async () => {
|
|
await users.get(`u${i++ % n}`)
|
|
})
|
|
bench('set', async () => {
|
|
await users.set(`u${i++ % n}`, { name: 'x', n: i })
|
|
})
|
|
bench('getMany x100', async () => {
|
|
await users.getMany(keys)
|
|
})
|
|
})
|
|
}
|
|
|
|
const tree = new KVStorageInMemory()
|
|
const writes: [string, number][] = []
|
|
for (let c = 0; c < 1000; c++) {
|
|
writes.push([`n/c${c}`, c])
|
|
for (let d = 0; d < 100; d++) writes.push([`n/c${c}/d${d}`, d])
|
|
}
|
|
await tree.writeMany(writes)
|
|
|
|
describe('keys() on 1k children x 100 descendants', () => {
|
|
bench('keys() skip-scan', async () => {
|
|
for await (const _ of tree.child('n').keys()) {
|
|
}
|
|
})
|
|
bench('full subtree scan (reference)', async () => {
|
|
for await (const _ of tree.scan({ gte: 'n/', lt: 'n0' })) {
|
|
}
|
|
})
|
|
})
|