// Timing of the vault-side work on a big synthetic vault: node scripts/bench.cjs [notes=10000] // Run with the Electron binary as node so better-sqlite3 loads: ELECTRON_RUN_AS_NODE=1 electron scripts/bench.cjs const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const { Vault } = require('../electron/vault.cjs'); const { openDb } = require('../electron/db.cjs'); const links = require('../electron/links.cjs'); const tasks = require('../electron/tasks.cjs'); const N = Number(process.argv[2]) || 10000; const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ink-bench-')); const words = 'alpha beta gamma delta project roadmap meeting notes idea review plan budget design draft summary weekly sync'.split(' '); const rnd = (n) => Math.floor(Math.random() * n); const sentence = () => Array.from({ length: 8 + rnd(10) }, () => words[rnd(words.length)]).join(' '); const t0 = Date.now(); for (let i = 0; i < N; i++) { const folder = path.join(dir, `Area ${i % 40}`, `Topic ${i % 400}`); fs.mkdirSync(folder, { recursive: true }); let body = `# Note ${i}\n\n`; for (let p = 0; p < 6; p++) body += sentence() + (rnd(3) === 0 ? ` [[Note ${rnd(N)}]]` : '') + '.\n\n'; body += `- [ ] task ${i} #work\n`; fs.writeFileSync(path.join(folder, `Note ${i}.md`), `---\ntags: [t${i % 50}]\n---\n\n${body}`); } console.log(`generated ${N} notes in ${Date.now() - t0} ms`); const time = async (label, fn) => { const s = process.hrtime.bigint(); const r = await fn(); console.log(label.padEnd(34), String(Number(process.hrtime.bigint() - s) / 1e6 | 0).padStart(7), 'ms'); return r; }; (async () => { const v = new Vault(dir, { trash: async () => {} }); const pages = await time('vault.list (cold)', () => v.list()); await time('vault.list (warm cache)', () => v.list()); const db = openDb(path.join(dir, 'index.sqlite')); await time('db.rebuild', async () => db.rebuild(pages)); await time('db.search "roadmap budget"', async () => db.search('roadmap budget')); await time('links.graph', async () => links.graph(pages)); const target = pages.find((p) => p.title === 'Note 5').id; await time('links.backlinks (one page)', async () => links.backlinks(target, pages)); await time('links.unlinked (one page)', async () => links.unlinked(target, pages)); await time('tasks.findTasks', async () => tasks.findTasks(pages)); await time('JSON size of page list (no body)', async () => JSON.stringify(pages.map(({ body, ...p }) => p)).length); db.close(); fs.rmSync(dir, { recursive: true, force: true }); })();