// Pipe into `docker exec -i CONTAINER node --input-type=module` on a disposable DB. import { createRequire } from 'node:module' import { createHash } from 'node:crypto' import { fileURLToPath } from 'node:url' import { resolve } from 'node:path' export const benchmarkPrefix = (i, first = 10) => `${first}.${i >>> 16}.${(i >>> 8) & 255}.${i & 255}/32` export function seedFixture(db) { db.pragma('foreign_keys = ON') db.transaction(() => { for (const table of ['agents', 'ip_lists', 'agent_telemetry_state', 'agent_stats_samples', 'agent_stats_rollups']) { if (db.prepare(`SELECT count(*) AS n FROM ${table}`).get().n !== 0) throw new Error(`Fresh benchmark database required: ${table} is not empty`) } db.prepare('INSERT INTO ip_lists(id,name,type) VALUES (?,?,?)').run('bench-list', 'Benchmark 50k', 'static') db.prepare('INSERT INTO policy_sets(id,name) VALUES (?,?)').run('bench-set', 'Benchmark') db.prepare('INSERT INTO policy_rules(id,set_id,priority,action,list_id) VALUES (?,?,?,?,?)').run('bench-rule', 'bench-set', 10, 'deny', 'bench-list') const entry = db.prepare('INSERT INTO ip_list_entries(id,list_id,cidr) VALUES (?,?,?)') for (let i = 0; i < 50_000; i++) entry.run(`bench-entry-${i}`, 'bench-list', benchmarkPrefix(i)) const agent = db.prepare('INSERT INTO agents(id,name,token_prefix,token_hash,status) VALUES (?,?,?,?,?)') const assign = db.prepare('INSERT INTO agent_policy_sets(agent_id,set_id) VALUES (?,?)') for (let i = 0; i < 100; i++) { const token = `bench-agent-token-${i}` agent.run(`bench-agent-${i}`, `Benchmark ${i}`, token.slice(0, 12), createHash('sha256').update(token).digest('hex'), 'approved') assign.run(`bench-agent-${i}`, 'bench-set') } })() return { fixture: { agents: 100, prefixes: 50_000 }, integrity: db.pragma('integrity_check', { simple: true }) } } if (!process.argv[1] || resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const require = createRequire('/app/node_modules/@evofw/db/package.json') const Database = require('better-sqlite3') const db = new Database('/data/app.db') try { console.log(JSON.stringify(seedFixture(db))) } finally { db.close() } }