Skip to content

Commit cb8d853

Browse files
chore(knowledge-pgvector): per-search segment timing and a load bench for search_knowledge (objectstack-ai#38729)
<!-- claude-projects-attribution: {"github_login":"os-warren"} --> _Requested by **Warren** · [project thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ88oxgpPaPHgsNM7N7PvvwbS)_ Refs objectstack-ai#38719 (part 1) — the committed segment timing and the load bench. Part 2, the baseline on the A4 environment, stays on the card, which this PR does not close. Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户无感:只新增一个提交进仓库的测速脚本,产品行为不变。 - 属于「私有部署企业版知识检索 10 人同时提问不超时」专题的基线一项,给第三期每项做不做提供数据。 - 出错只会让测速读数不准,不影响任何客户;脚本不随软件包发布,可直接撤回。 - 无。 ## What changes A new, unpublished directory `packages/plugins/knowledge-pgvector/bench/` (the package's `files` is `dist`, `README.md`, `CHANGELOG.md`, so nothing here ships): - `instrument.mjs` — per-search segment timing read through seams the code already takes. Nothing under `src/` of this package or of `service-knowledge` is changed: - `embed` — the adapter's `embedder` option; - `pool_wait` + `ranking` + `other_sql` — the adapter's `pool` option, a `PgPoolLike` whose one-shot query is split into checkout, statement and release, the same steps `pg.Pool#query` runs; - `visible_ids` + `permission_recheck` + `system_read` — the service's `dataEngine` option, each read classified by the arguments the service passes; - adapter rounds (a refill round = backfill) — the adapter as registered with the service. Concurrent searches are kept apart by an `AsyncLocalStorage` store per search. - `run.mjs` — the load bench, runnable against any PostgreSQL URL with pgvector. It seeds a synthetic dataset into two throwaway schemas, builds the index through the real `KnowledgeService`, then makes real `search_knowledge` calls (the tool executor, as a non-system caller) over record-scope fraction × concurrency. Default grid: 1/10/30/100 % and unscoped × 1/5/10/20 clients, closed loop. - Per cell: p50/p95/max/mean of the total and of every segment, each with its sample count; hit count; searches per question; backfill share (scoped and unscoped cells are separate rows); embedding-call failures; failed and refused searches with their codes; empty answers; the service's `search failed` log lines; block reads of the store's tables. - It also records the machine shape (client CPUs and memory; server version, pgvector version and memory settings) and whether the cell ran cold or warm. - `instrument.test.mjs` — pins statement shape and call counts over the real adapter and service from source, with no database. It never pins a duration. - `README.md` — the manual. ### Segment status | segment (card) | status | seam | |:--|:--|:--| | embed | measured | adapter `embedder` option | | vector statement | measured as `ranking` | adapter `pool` option; the statement also carries the keyword leg for any query with keyword terms (counted per cell) and the scope predicate | | scope predicate | **NOT MEASURED in situ** | it is a conjunct of the ranking statement's one WHERE. `--isolate-scope` adds a DERIVED out-of-situ number: the same WHERE as a bare count minus the source-and-generation WHERE as a bare count, run serially and timed by the server. An in-situ number needs the store to time its statement parts itself, a change under `knowledge-pgvector/src` (held by objectstack-ai#38692) | | permission re-check | measured | service `dataEngine` option (`permission_recheck` after ranking, `visible_ids` before it) | | pool wait | measured (not on the card) | adapter `pool` option | ## Tests - `pnpm --filter @objectstack/knowledge-pgvector test` at `8e3e522f`: 6 files passed, 8 skipped (the live-PostgreSQL files: no `OS_TEST_POSTGRES_URL`); 29 tests passed, 93 skipped. - `pnpm --filter @objectstack/knowledge-pgvector typecheck`: exit 0. The bench files are `.mjs` and outside the package's TS programs. - Ablation: with `permission_recheck` reclassified on disk, `bench/instrument.test.mjs` went red (1 failed / 4 passed); restored with `git restore`. <details><summary>Local smoke run — 5,000 passages, 1024 dims, 40 searches a cell (NOT a baseline, not quotable)</summary> Machine: 4 vCPU, 15.7 GiB, shared container. PostgreSQL 16.15 with pgvector **0.6.0** (Ubuntu package; the kit ships 0.8.7), `shared_buffers` 512MB, pool 4, fake in-process embedder, warm. Its job is to show the script runs the whole grid; it is not the A4 baseline. Columns: scope · concurrency · n · total p50/p95 ms · pool wait p50 · ranking p50 · re-check p50 · backfill · `search failed` lines · rows in scope. ``` 1% | 1 | 40 | 53.73 / 73.57 | 0.1 | 50.46 | 1.95 | 0 | 0 | 50 1% | 20 | 40 | 434.59 / 625.09 | 350.72 | 71.83 | 2.21 | 0 | 0 | 50 10% | 1 | 40 | 73.35 / 106.8 | 0.09 | 70.21 | 1.87 | 0 | 0 | 500 30% | 1 | 40 | 114.04 / 205.01 | 0.08 | 111.08 | 1.73 | 0 | 0 | 1500 100% | 1 | 40 | 263.53 / 453.83 | 0.07 | 260.75 | 1.69 | 0 | 0 | 5000 100% | 20 | 40 | 1776.19 / 2117.96 | 1451.12 | 323.8 | 5.45 | 0 | 0 | 5000 none | 1 | 40 | 250.89 / 491.17 | 0.07 | 245.3 | 1.82 | 0 | 0 | - none | 20 | 40 | 1295.42 / 1570.26 | 1046.31 | 241.46 | 1.56 | 0 | 0 | - ``` All 20 cells completed with 0 failed searches; the full table is in the run's JSON. </details> ## Acceptance notes - **First files to read:** 1. `bench/instrument.mjs` — `classifyEngineRead`, which decides what counts as the permission re-check, and `timedPool`, which reproduces `pg.Pool#query`'s checkout, statement and release. 2. `bench/run.mjs` — `isolateScope`, the derived scope-predicate pass, and its stated method. 3. `bench/instrument.test.mjs` — the pins over the real adapter and service. - The bench composes its own service over synthetic data (ObjectQL + SqlDriver, no security plugin), so the re-check reads every row it asks for. It cannot yet point at an already-indexed source in a kit deployment (the BrightRule source on A4); part 2 has to decide that. - At concurrency 10–20, the smoke run's time goes mostly to waiting for one of the pool's 4 connections. That is the topic's B2 (pool size from settings), not new scope. - No changeset: no published file moves (`skip-changeset`). - Seat routing (2026-11-30T08:16Z), part 2 composition: B — on A4 the bench composes the source itself at 60k×1024; an existing-source option (A) may follow after objectstack-ai#38700 only if needed. - Seat routing (2026-11-30T08:16Z), scope predicate: keep the labelled DERIVED number (`--isolate-scope`); no store seam under objectstack-ai#38692's files. ## Verification Two-minute check, against any PostgreSQL where the role can `CREATE` and the `vector` extension is available: ```bash pnpm --filter '@objectstack/knowledge-pgvector...' build node packages/plugins/knowledge-pgvector/bench/run.mjs --pg-url postgres://USER@HOST:5432/DB --passages 500 --scopes 10,none --concurrency 1,5 --searches 20 --isolate-scope ``` Expected: a markdown table with four rows, every `ok` equal to `n`, the 10% rows showing `… (derived)` in the last column and the unscoped rows `NOT MEASURED`; the JSON path printed on stderr. Both schemas are dropped at the end. --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01JQwxbXQcnpisuAAJRr8yNr <!-- signed: repo:objectstack@os-warren · os-warren · session_01JQwxbXQcnpisuAAJRr8yNr --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5bb397d commit cb8d853

4 files changed

Lines changed: 1407 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# knowledge-pgvector search bench
2+
3+
Real `search_knowledge` calls — the tool executor of `@objectstack/service-knowledge`
4+
over a real `KnowledgeService`, this store and ObjectQL + SqlDriver — on any PostgreSQL
5+
with pgvector, timed per segment at each record-scope fraction × concurrency. It
6+
measures and fixes nothing. Nothing here is published: the package's `files` is
7+
`dist`, `README.md` and `CHANGELOG.md`, and vitest runs only `instrument.test.mjs`.
8+
9+
## Run it
10+
11+
```bash
12+
pnpm --filter '@objectstack/knowledge-pgvector...' build # the bench loads the built dist; OS_SKIP_DTS=1 is fine
13+
node packages/plugins/knowledge-pgvector/bench/run.mjs \
14+
--pg-url postgres://USER@HOST:5432/DB --passages 60000 --dims 1024 --isolate-scope --keep
15+
node packages/plugins/knowledge-pgvector/bench/run.mjs --pg-url … --passages 60000 --dims 1024 --reuse --cache cold
16+
```
17+
18+
The role needs `CREATE` on the database (the run makes two schemas) and the `vector`
19+
extension installed or installable. `--help` lists every flag; the ones that shape a
20+
reading:
21+
22+
| flag | default | meaning |
23+
|:--|:--|:--|
24+
| `--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` |
25+
| `--passages` / `--dims` / `--units` | 2000 / 1024 / 100 | one passage per record, spread evenly over the units |
26+
| `--scopes` | `1,10,30,100,none` | the record scope as a % of units (so of passages); `none` is the unscoped run |
27+
| `--concurrency` | `1,5,10,20` | closed-loop clients, each issuing its next search when the last returns |
28+
| `--searches` | 100 | timed searches per cell (the sample count of every percentile in that cell) |
29+
| `--searches-per-question` | 1 | searches one simulated question issues back to back |
30+
| `--top-k`, `--granularity`, `--group-by unit` | 5, chunk | the `search_knowledge` arguments |
31+
| `--cache` | `warm` | `warm`: `--warmup` untimed searches before each cell; `cold`: a fresh store pool per cell and no warm-up (see below) |
32+
| `--pool-max`, `--connect-timeout-ms` | 4, 5000 | the store's pool, as `KnowledgePgvectorPlugin` builds it by default |
33+
| `--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 |
34+
| `--queries FILE` | synthetic | a JSON array of question strings |
35+
| `--isolate-scope` | off | the derived scope-predicate pass after each scoped cell |
36+
| `--reuse` / `--keep` | off | keep a seeded dataset between runs (a reuse still walks every record once: a no-op rebuild) |
37+
38+
It writes the JSON to `--out` (default `$TMPDIR/os-kbench/<dataset>-<cache>.json`) and
39+
prints a markdown table.
40+
41+
## What one search records
42+
43+
Each search runs inside its own `AsyncLocalStorage` store, so concurrent searches through
44+
the same wrappers are attributed to the right one. Every segment is read through a seam the
45+
code already takes — no file under either package's `src/` is changed:
46+
47+
| segment | seam | what it is |
48+
|:--|:--|:--|
49+
| `embed` | the adapter's `embedder` option | each embed call of the search (one per round per source) |
50+
| `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 |
51+
| `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 |
52+
| `other_sql` | the same pool | the store's other statements: the generation read, and a transaction's own `BEGIN`, `SET LOCAL` and `COMMIT` |
53+
| `visible_ids` | the service's `dataEngine` option | the caller's readable record ids, read before ranking |
54+
| `permission_recheck` | the same engine | every hit re-read as the caller after ranking |
55+
| `system_read` | the same engine | reads the service runs as the system (freshness, verification) |
56+
| `unaccounted` | — | the call's wall time minus every segment: the service's, the tool's and the adapter's own work |
57+
58+
Per cell: p50 / p95 / max / mean of the total and of each segment, each with its sample
59+
count; hit count; `backfillShare` (the share of searches whose source was searched a
60+
second time, the refill round; scoped and unscoped cells are separate rows); adapter calls
61+
per search; embedding-call failures; failed and refused searches with their codes; empty
62+
answers; the service's `error` lines and how many of them say `search failed`; the
63+
ranking statements' shapes (how many carried the keyword leg or each scope term); the
64+
store tables' block reads and hits during the cell; and the machine shape (client CPUs and
65+
memory; server version, pgvector version and memory settings).
66+
67+
The unit test pins which statement is the ranking one, what its WHERE holds, and how many
68+
calls of each kind one search makes, over the real adapter and service from source. It
69+
never pins a duration.
70+
71+
## The scope predicate is not an in-situ segment
72+
73+
The store sends the record scope as a conjunct of the ranking statement's one WHERE, so
74+
inside a search its cost cannot be told from the vector leg's: `ranking` carries both,
75+
and the table says `NOT MEASURED` for it. With `--isolate-scope` each scoped cell is
76+
followed by a derived pass, out of situ: on one connection, serially, the cell's last
77+
ranking statement, the same WHERE as a bare `count(*)`, and the source-and-generation
78+
WHERE as a bare `count(*)`, each `--isolate-repeats` times, timed by the server
79+
(`EXPLAIN (ANALYZE, TIMING OFF)`). `scopePredicateMsP50` is scoped count − base count;
80+
`rankingBeyondPredicateMsP50` is ranking − scoped count. The plans' scan nodes are
81+
recorded beside them, so a reader sees whether the three read the table the same way, and
82+
the rows in scope give the real selectivity. Measuring it in situ needs the store to time
83+
its own statement parts, a change to `src/` this bench does not make.
84+
85+
## Cold and warm
86+
87+
`warm` runs `--warmup` untimed searches before each cell on the same pool. `cold` builds a
88+
fresh store pool for each cell and runs no warm-up; a client cannot flush the server's
89+
buffer cache or the OS page cache, so for a true cold read restart PostgreSQL and drop the
90+
OS cache before the run. `knowledgeTableBlocks.read` in each cell shows how cold it was.
91+
92+
## Quoting a number
93+
94+
A number from this bench is quoted only from a run that has passed review, with the
95+
JSON's `machine`, `parameters` and `dataset` beside it. The data is synthetic (a
96+
3,000-word vocabulary, about 60 words a passage) and the caller is a non-system user over
97+
an engine with no security plugin, so the re-check reads every row it asks for.

0 commit comments

Comments
 (0)