Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions packages/plugins/knowledge-pgvector/bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# knowledge-pgvector search bench

Real `search_knowledge` calls — the tool executor of `@objectstack/service-knowledge`
over a real `KnowledgeService`, this store and ObjectQL + SqlDriver — on any PostgreSQL
with pgvector, timed per segment at each record-scope fraction × concurrency. It
measures and fixes nothing. Nothing here is published: the package's `files` is
`dist`, `README.md` and `CHANGELOG.md`, and vitest runs only `instrument.test.mjs`.

## Run it

```bash
pnpm --filter '@objectstack/knowledge-pgvector...' build # the bench loads the built dist; OS_SKIP_DTS=1 is fine
node packages/plugins/knowledge-pgvector/bench/run.mjs \
--pg-url postgres://USER@HOST:5432/DB --passages 60000 --dims 1024 --isolate-scope --keep
node packages/plugins/knowledge-pgvector/bench/run.mjs --pg-url … --passages 60000 --dims 1024 --reuse --cache cold
```

The role needs `CREATE` on the database (the run makes two schemas) and the `vector`
extension installed or installable. `--help` lists every flag; the ones that shape a
reading:

| flag | default | meaning |
|:--|:--|:--|
| `--pg-url` | `$OS_BENCH_PG_URL` | the database; the run makes `os_kbench_<dataset>` (records) and `os_kbench_<dataset>_k` (the store) there and drops both at the end unless `--keep` |
| `--passages` / `--dims` / `--units` | 2000 / 1024 / 100 | one passage per record, spread evenly over the units |
| `--scopes` | `1,10,30,100,none` | the record scope as a % of units (so of passages); `none` is the unscoped run |
| `--concurrency` | `1,5,10,20` | closed-loop clients, each issuing its next search when the last returns |
| `--searches` | 100 | timed searches per cell (the sample count of every percentile in that cell) |
| `--searches-per-question` | 1 | searches one simulated question issues back to back |
| `--top-k`, `--granularity`, `--group-by unit` | 5, chunk | the `search_knowledge` arguments |
| `--cache` | `warm` | `warm`: `--warmup` untimed searches before each cell; `cold`: a fresh store pool per cell and no warm-up (see below) |
| `--pool-max`, `--connect-timeout-ms` | 4, 5000 | the store's pool, as `KnowledgePgvectorPlugin` builds it by default |
| `--embedder` | `fake` | `fake`: words hashed into slots, in process, no network; `openai`: an OpenAI-compatible `/embeddings` at `OS_BENCH_EMBEDDER_URL` with `OS_BENCH_EMBEDDER_MODEL` (and `OS_BENCH_EMBEDDER_API_KEY`); every passage is embedded once at build |
| `--queries FILE` | synthetic | a JSON array of question strings |
| `--isolate-scope` | off | the derived scope-predicate pass after each scoped cell |
| `--reuse` / `--keep` | off | keep a seeded dataset between runs (a reuse still walks every record once: a no-op rebuild) |

It writes the JSON to `--out` (default `$TMPDIR/os-kbench/<dataset>-<cache>.json`) and
prints a markdown table.

## What one search records

Each search runs inside its own `AsyncLocalStorage` store, so concurrent searches through
the same wrappers are attributed to the right one. Every segment is read through a seam the
code already takes — no file under either package's `src/` is changed:

| segment | seam | what it is |
|:--|:--|:--|
| `embed` | the adapter's `embedder` option | each embed call of the search (one per round per source) |
| `pool_wait` | the adapter's `pool` option | each checkout of a pooled connection: a one-shot query's, or one the store holds for a transaction |
| `ranking` | the same pool | the ranking statement's execution, one-shot or inside the store's transaction: vector leg, keyword leg when the query has keyword terms, and the scope predicate |
| `other_sql` | the same pool | the store's other statements: the generation read, and a transaction's own `BEGIN`, `SET LOCAL` and `COMMIT` |
| `visible_ids` | the service's `dataEngine` option | the caller's readable record ids, read before ranking |
| `permission_recheck` | the same engine | every hit re-read as the caller after ranking |
| `system_read` | the same engine | reads the service runs as the system (freshness, verification) |
| `unaccounted` | — | the call's wall time minus every segment: the service's, the tool's and the adapter's own work |

Per cell: p50 / p95 / max / mean of the total and of each segment, each with its sample
count; hit count; `backfillShare` (the share of searches whose source was searched a
second time, the refill round; scoped and unscoped cells are separate rows); adapter calls
per search; embedding-call failures; failed and refused searches with their codes; empty
answers; the service's `error` lines and how many of them say `search failed`; the
ranking statements' shapes (how many carried the keyword leg or each scope term); the
store tables' block reads and hits during the cell; and the machine shape (client CPUs and
memory; server version, pgvector version and memory settings).

The unit test pins which statement is the ranking one, what its WHERE holds, and how many
calls of each kind one search makes, over the real adapter and service from source. It
never pins a duration.

## The scope predicate is not an in-situ segment

The store sends the record scope as a conjunct of the ranking statement's one WHERE, so
inside a search its cost cannot be told from the vector leg's: `ranking` carries both,
and the table says `NOT MEASURED` for it. With `--isolate-scope` each scoped cell is
followed by a derived pass, out of situ: on one connection, serially, the cell's last
ranking statement, the same WHERE as a bare `count(*)`, and the source-and-generation
WHERE as a bare `count(*)`, each `--isolate-repeats` times, timed by the server
(`EXPLAIN (ANALYZE, TIMING OFF)`). `scopePredicateMsP50` is scoped count − base count;
`rankingBeyondPredicateMsP50` is ranking − scoped count. The plans' scan nodes are
recorded beside them, so a reader sees whether the three read the table the same way, and
the rows in scope give the real selectivity. Measuring it in situ needs the store to time
its own statement parts, a change to `src/` this bench does not make.

## Cold and warm

`warm` runs `--warmup` untimed searches before each cell on the same pool. `cold` builds a
fresh store pool for each cell and runs no warm-up; a client cannot flush the server's
buffer cache or the OS page cache, so for a true cold read restart PostgreSQL and drop the
OS cache before the run. `knowledgeTableBlocks.read` in each cell shows how cold it was.

## Quoting a number

A number from this bench is quoted only from a run that has passed review, with the
JSON's `machine`, `parameters` and `dataset` beside it. The data is synthetic (a
3,000-word vocabulary, about 60 words a passage) and the caller is a non-system user over
an engine with no security plugin, so the re-check reads every row it asks for.
Loading
Loading