diff --git a/performance/people/README.md b/performance/people/README.md index 1cbd036..473db01 100644 --- a/performance/people/README.md +++ b/performance/people/README.md @@ -11,3 +11,6 @@ Measurements of cli-core's people: who is listed, and how fast. - [kinds](kinds/README.md): what a kind per person, message counts and the newest contact cost the people list. About 0.5–0.6 s per list on a loaded machine with 1.5 million messages, 1.9 s sorted by the newest contact; a search stays under 0.1 s (2026-11-09). +- [bots](bots/README.md): what moving programs off people costs (cli-core store version 75). With 20,000 program + senders, the first list holds the write lock for about 1 s, the upgrade from version 74 takes 0.3–3.2 s, and warm + pages of people and bots stay under 60 ms (2026-11-25). diff --git a/performance/people/bots/README.md b/performance/people/bots/README.md new file mode 100644 index 0000000..7048024 --- /dev/null +++ b/performance/people/bots/README.md @@ -0,0 +1,61 @@ +# People bots: what moving programs off people costs the people list and the upgrade + +## Why it was run + +The owner ruled that a bot (a no-reply or alert address, a messenger's bot, a service's mail) is its own entity, never +a person: its identities leave the person they were linked to, and it is never listed with people (contact-kinds plan +in cli-core [#3323](https://github.com/WireCatLabs/cli-core/pull/3323)). cli-core store version 75 does it +([#3356](https://github.com/WireCatLabs/cli-core/pull/3356)). Two moments do heavy work under the store's write lock, +which other writers wait for at most 5 s: + +1. the first people list on a store whose programs were never sorted: the rules move each program's identities to a + new bot; +2. the upgrade from version 74, which moves every existing bot's identities off its person row. + +The question: on a store with 20,000 program senders, does either come near the 5 s wait, and do the warm lists stay +fast? + +## Setup + +- Machine: AMD Ryzen AI 9 HX 370 (24 threads), Linux 7.0, Node 24.19.0. Other agents' runs kept the load average at + 12 to 18, so the numbers vary a lot between runs and are higher than on an idle machine. +- Code: cli-core branch `feat/bot-entity` (store version 75), built with `pnpm build`. +- Data, all synthetic: 20,000 senders from `noreply-@shop.ru` and 20,000 from `person@firm.ru`, + saved through `store.people.savePeople` in batches of 2,000, no messages. For the upgrade, the same store is turned + back into version 74 (version 75's objects dropped) with one `sender_bots` row per program, anchored on its person. +- Each run below is one run of the script; the table gives the range of 4 runs. + +```sh +node bench.mjs 20000 +``` + +## Results + +| Step | Range over 4 runs | +| --- | ---: | +| first people list: 20,000 programs moved to bots, 40,000 kinds worked out | 2.4–6.0 s | +| of which under the write lock (moves, organizations from domains, kinds) | about 1.0–1.2 s | +| warm people list, first 50 | 17–40 ms | +| warm people list `kind:all`, first 50 | 23–57 ms | +| people counts | 1–3 ms | +| bots list, first 50 | 14–54 ms | +| bots list, a search | 19–48 ms | +| one bot with its moves | 1–3 ms | +| opening a version 74 store with 20,000 bots: the upgrade to 75 | 0.3–3.2 s | +| first people list after the upgrade | 0.6–2.4 s | + +The part under the lock was read with timers inside cli-core's `keep` on two runs; the script does not print it. + +## What it showed + +- The work under the write lock stays at about 1 s, well inside another writer's 5 s wait. Most of the first list's + time is working out 40,000 kinds outside the lock, as before version 75. +- The upgrade moves 20,000 identities in a fraction of a second to about 3 s, depending on the load. It runs once. +- Warm pages of people and bots stay under 60 ms. +- Giving a rule's bot back once the owner writes to it is checked on every kept list: 25–256 ms with 20,000 bots. + +## Not measured + +- Stores with messages and mail from the bots: the bots list's counts and last contact read the + `messages_by_sender_identity_id` and `emails_by_from_identity_id` indexes for a page only. +- An idle machine, and the owner's real store. diff --git a/performance/people/bots/bench.mjs b/performance/people/bots/bench.mjs new file mode 100644 index 0000000..8a2dfb2 --- /dev/null +++ b/performance/people/bots/bench.mjs @@ -0,0 +1,88 @@ +// Times store version 75 on synthetic senders: the v74 -> v75 upgrade with N bots, the rule pass that moves N +// program senders to bots, and warm reads of the people and bots lists. Usage: node bench.mjs [N] +import { mkdtempSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" + +const repo = process.argv[2] +const N = Number(process.argv[3] ?? 20000) +const { openStore } = await import(join(repo, "dist/store/index.js")) +const { openCache } = await import(join(repo, "dist/store/open.js")) +const MAIL = { provider: "email", account: "me@acme.ru" } +const TG = { provider: "telegram", account: "1" } +const say = (line) => process.stdout.write(`${line}\n`) + +const time = async (label, work) => { + const start = performance.now() + const result = await work() + say(`${label}: ${Math.round(performance.now() - start)} ms`) + return result +} + +const seeded = async (programs, humans) => { + const path = join(mkdtempSync(join(tmpdir(), "bot-scale-")), "store.db") + const store = await openStore({ path }) + await store.people.savePeople(TG, [{ id: "1", name: "Me" }]) + await store.people.linkToOwner({ provider: "telegram", id: "1" }) + await store.accounts.saveAccount(MAIL, { name: null }) + const senders = [ + ...Array.from({ length: programs }, (_, n) => ({ id: `noreply-${n}@shop${n % 500}.ru`, name: `Shop ${n}` })), + ...Array.from({ length: humans }, (_, n) => ({ id: `person${n}@firm${n % 300}.ru`, name: `Person ${n}` })), + ] + for (let at = 0; at < senders.length; at += 2000) await store.people.savePeople(MAIL, senders.slice(at, at + 2000)) + return { store, path } +} + +say( + `node ${process.version}, N = ${N}, load ${(await import("node:os")) + .loadavg() + .map((x) => x.toFixed(1)) + .join(" ")}`, +) + +// 1. The rule pass: N program senders and N humans, kinds never worked out, then the first list. +{ + const { store } = await seeded(N, N) + await time(`first people list, ${N} programs moved to bots + ${N} humans sorted`, () => + store.people.list({ limit: 50 }), + ) + await time("warm people list, page of 50", () => store.people.list({ limit: 50 })) + await time("warm people list kind:all, page of 50", () => store.people.list({ limit: 50, kind: "all" })) + await time("people counts", () => store.people.counts()) + const bots = await time("bots list, page of 50", () => store.people.listBots({ limit: 50 })) + await time("bots list, query", () => store.people.listBots({ limit: 50, query: "shop1" })) + await time("bot show", () => store.people.showBot(bots.items[0].bot)) + say(`bots: ${bots.total}`) + await store.close() +} + +// 2. The upgrade: a version 74 store whose N bots are person rows holding their identities. +{ + const { store, path } = await seeded(N, N) + await store.close() + const db = await openCache(path) + db.exec( + "DROP TRIGGER person_kind_sender_bot_origin_au; DROP TRIGGER sender_bot_identity_link_bi; " + + "DROP TRIGGER sender_bot_identity_link_au; " + + "DROP TRIGGER sender_bot_identity_person_ai; DROP TRIGGER sender_bot_identities_linked_bi; " + + "DROP TRIGGER sender_bots_holding_bd; DROP TRIGGER sender_bot_identities_identity_ad; " + + "DROP TRIGGER sender_bots_emptied_ad; DROP TRIGGER sender_bots_emptied_au; " + + "DROP INDEX sender_bot_moves_by_from_person_id; DROP TABLE sender_bot_identities; " + + "DELETE FROM schema_migrations WHERE version >= 75;", + ) + db.prepare( + "INSERT INTO sender_bots (person_id, author, reason, created_at, updated_at) SELECT l.person_id, 'rules', 'no-reply', 1, 1 " + + "FROM identity_links l JOIN identities i ON i.id = l.identity_id WHERE i.external_id LIKE 'noreply-%'", + ).run() + say(`version 74 bots: ${db.prepare("SELECT count(*) AS n FROM sender_bots").get().n}`) + db.close() + const upgraded = await time(`open with the upgrade to 75, ${N} bots`, () => openStore({ path })) + const after = await openCache(path) + say( + `after: ${after.prepare("SELECT count(*) AS n FROM sender_bot_identities").get().n} bot identities, ` + + `${after.prepare("SELECT count(*) AS n FROM sender_bots WHERE person_id > 0").get().n} positive person_id`, + ) + after.close() + await time("first people list after the upgrade", () => upgraded.people.list({ limit: 50 })) + await upgraded.close() +}