Files
PotentialStorage/bench/kv-storage.bench.ts
T
Claude 631c75428b
CI / Node 22 (pull_request) Successful in 12s
CI / Node 26 (pull_request) Successful in 13s
CI / Node 24 (pull_request) Successful in 14s
Plain operations: last write wins, hash-keyed events, simpler composite
- KV: one write primitive, write(changes) with { key, value? }; writeIfAbsent removed.
  The server executes exactly what clients send and never reads values, so they can be
  encrypted end to end. Adds setFields/getFields on the base class.
- HTTP: no server history or event ids; SSE relays every change (own writes included) and
  the client emits resync when a dropped stream reconnects.
- Composite: writes apply locally, then go to the source (undone locally if refused);
  pushed changes are applied and announced only where they differ from local values;
  offline writes go to a queue store (any KV storage); sync() = replay, diff, replay.
  Removes KVReplicaSet, KVOutbox, KeyCounter, SerialQueue and RetryTimer.
- Events: id = <time>_<sha256(time + canonical data)>, so concurrent appends never
  collide and retries can't duplicate; no versions or ConcurrencyError.
- Event-sourced documents: read, resolve, append, apply; catchUp folds in foreign events;
  getAt takes an event id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
2026-10-23 13:33:07 +00:00

42 lines
1.3 KiB
TypeScript

import { bench, describe } from 'vitest'
import type { KVChange } from '../src/kv-storage/base-kv-storage.js'
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) => ({ key: `users/u${i}`, value: { 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: KVChange[] = []
for (let c = 0; c < 1000; c++) {
writes.push({ key: `n/c${c}`, value: c })
for (let d = 0; d < 100; d++) writes.push({ key: `n/c${c}/d${d}`, value: 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' })) {
}
})
})