2d819b476c
* fix(db): forward DatabaseOpts to tauri-plugin-turso NativeDatabaseService.open ignored its opts parameter, dropping any experimental feature flags (e.g. 'index_method' needed for FTS / vector indexes) and any encryption config before they could reach the Tauri plugin. Translate DatabaseOpts to the plugin's LoadOptions shape and forward as the single argument Database.load accepts. Skip translation when no relevant opts are set so the existing path-string call shape is preserved for callers without experimental/encryption needs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(db): cover Turso vector primitives + add benchmark harness Verifies the Turso functions Reedy retrieval depends on, with a brute-force per-book kNN test that runs against every DatabaseService backend (node, native, WASM): SELECT vector_distance_cos(embedding, vector32(?)) AS d FROM book_chunks WHERE book_hash = ? ORDER BY d ASC LIMIT k This is the path Turso's own founder recommended in tursodatabase/turso#3778 ("First, focus on efficient SIMD-accelerated brute-force search") and what shipped at commit 1aba105df4f. Native vector index modules don't exist in this engine: `libsql_vector_idx`, `vector_top_k`, and `USING vector/hnsw/diskann/ivfflat` all parse-error against @tursodatabase/database@0.6.0-pre.28 (libsql_vector_idx is a libSQL/sqld fork feature; DiskANN was closed not-planned upstream in #832). The test asserts cross-book isolation and nearest-first ordering using only `WHERE book_hash = ?` and `ORDER BY` — no DDL, no identifier interpolation, no index plumbing. Also adds bench/ harness for manual perf checks: pnpm bench [name] run benchmarks (refuses in CI) pnpm bench --list list available benchmarks pnpm bench --no-record skip results.jsonl append pnpm bench --force override the CI guard Uses Node 24's --experimental-strip-types so no tsx devDep is needed. Appends one JSON line per run to bench/results.jsonl (gitignored, local history; share by pasting tabular stdout into PRs/issues). Explicitly NOT in CI — shared-tenant variance makes synthetic-benchmark regression detection unreliable; production telemetry (reedy_metrics, plan §M1.9) is the right tool for that. First benchmark: vector-retrieval. Measured on M1 Pro: 400 chunks × 384 dim → 0.35 ms / query 400 chunks × 768 dim → 0.45 ms / query 2000 chunks × 768 dim → 2.23 ms / query 10000 chunks × 768 dim → 14.00 ms / query 400 chunks × 1536 dim → 0.70 ms / query Per-chunk cost ~1.1 µs at 768 dim = ~1.4 ns/dim. NEON-class on Apple Silicon, ~50× faster than scalar — confirms SIMD acceleration is active in 0.6.0-pre.28. Per-query latency stays sub-ms at Reedy MVP corpus sizes; the ceiling is ~10K chunks per book before phone-class hardware notices. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
120 lines
3.5 KiB
TypeScript
120 lines
3.5 KiB
TypeScript
import { performance } from 'node:perf_hooks';
|
|
import { cpus, platform, arch, totalmem } from 'node:os';
|
|
import { readFileSync } from 'node:fs';
|
|
|
|
export interface BenchContext {
|
|
/** Whether to print verbose per-iteration info. */
|
|
verbose: boolean;
|
|
}
|
|
|
|
export interface BenchResult {
|
|
scenario: string;
|
|
unit: 'ms' | 'us' | 'ns';
|
|
value: number;
|
|
/** Optional metadata: chunk count, dim, etc. */
|
|
meta?: Record<string, string | number>;
|
|
}
|
|
|
|
export interface Bench {
|
|
name: string;
|
|
description: string;
|
|
run(ctx: BenchContext): Promise<BenchResult[]>;
|
|
}
|
|
|
|
/** High-resolution timer; returns elapsed milliseconds. */
|
|
export async function timed<T>(fn: () => Promise<T>): Promise<{ result: T; ms: number }> {
|
|
const t0 = performance.now();
|
|
const result = await fn();
|
|
const ms = performance.now() - t0;
|
|
return { result, ms };
|
|
}
|
|
|
|
/** Run `fn` `reps` times, return average milliseconds (after warmup). */
|
|
export async function avg(fn: () => Promise<unknown>, reps: number, warmup = 3): Promise<number> {
|
|
for (let i = 0; i < warmup; i++) await fn();
|
|
const t0 = performance.now();
|
|
for (let i = 0; i < reps; i++) await fn();
|
|
return (performance.now() - t0) / reps;
|
|
}
|
|
|
|
/** Generate a random unit vector serialized as JSON, suitable for `vector32(?)`. */
|
|
export function randomUnitVectorJson(dim: number): string {
|
|
const v = new Float32Array(dim);
|
|
let norm = 0;
|
|
for (let i = 0; i < dim; i++) {
|
|
const x = Math.random() * 2 - 1;
|
|
v[i] = x;
|
|
norm += x * x;
|
|
}
|
|
const inv = 1 / Math.sqrt(norm);
|
|
for (let i = 0; i < dim; i++) v[i] = (v[i] ?? 0) * inv;
|
|
return JSON.stringify(Array.from(v));
|
|
}
|
|
|
|
export interface MachineInfo {
|
|
platform: string;
|
|
arch: string;
|
|
cpu: string;
|
|
cpuCount: number;
|
|
memGiB: number;
|
|
node: string;
|
|
packages: Record<string, string>;
|
|
}
|
|
|
|
export function machineInfo(packages: string[] = []): MachineInfo {
|
|
const cpuList = cpus();
|
|
const firstCpu = cpuList[0];
|
|
const versions: Record<string, string> = {};
|
|
for (const name of packages) {
|
|
try {
|
|
const pkg = JSON.parse(readFileSync(`./node_modules/${name}/package.json`, 'utf8'));
|
|
versions[name] = pkg.version;
|
|
} catch {
|
|
versions[name] = 'not installed';
|
|
}
|
|
}
|
|
return {
|
|
platform: platform(),
|
|
arch: arch(),
|
|
cpu: firstCpu?.model.trim() ?? 'unknown',
|
|
cpuCount: cpuList.length,
|
|
memGiB: Math.round(totalmem() / 1024 ** 3),
|
|
node: process.version,
|
|
packages: versions,
|
|
};
|
|
}
|
|
|
|
export function formatHeader(info: MachineInfo): string {
|
|
const lines = [
|
|
'═'.repeat(70),
|
|
` Platform : ${info.platform}/${info.arch}`,
|
|
` CPU : ${info.cpu} (${info.cpuCount} cores)`,
|
|
` Memory : ${info.memGiB} GiB`,
|
|
` Node : ${info.node}`,
|
|
];
|
|
for (const [name, ver] of Object.entries(info.packages)) {
|
|
lines.push(` ${name.padEnd(9)}: ${ver}`);
|
|
}
|
|
lines.push('═'.repeat(70));
|
|
return lines.join('\n');
|
|
}
|
|
|
|
export function formatResults(benchName: string, results: BenchResult[]): string {
|
|
const lines = [`\n[${benchName}]`];
|
|
for (const r of results) {
|
|
const metaStr = r.meta
|
|
? ` ${Object.entries(r.meta)
|
|
.map(([k, v]) => `${k}=${v}`)
|
|
.join(' ')}`
|
|
: '';
|
|
lines.push(` ${r.scenario.padEnd(40)} ${formatValue(r.value, r.unit).padStart(12)}${metaStr}`);
|
|
}
|
|
return lines.join('\n');
|
|
}
|
|
|
|
function formatValue(value: number, unit: 'ms' | 'us' | 'ns'): string {
|
|
if (unit === 'ms') return `${value.toFixed(3)} ms`;
|
|
if (unit === 'us') return `${value.toFixed(2)} µs`;
|
|
return `${value.toFixed(0)} ns`;
|
|
}
|