Skip to content

Commit 777877d

Browse files
test(qa): SQL performance M0 baseline — SQLite and PostgreSQL at 100k and 1M rows (#33662)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DWLrF6ZURGN6BVpSbuDyHTg)_ Fixes #33633 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:在 `packages/qa/dogfood/bench/sql-perf/` 加了一套 SQL 性能基准(固定测试应用 + HTTP 基准脚本 + "现状 SQL 对比目标 SQL"脚本),只测量,不动任何产品代码。 - 为什么改:SQL 性能专题(#33631)的 M0 要求先量后修:在 SQLite 和 PostgreSQL 上、10 万和 100 万行规模下给出每条路径的耗时和 SQL 条数,排出 M2 的优先级。排序清单已贴在 #33631。 - 风险与回滚:无运行时风险——私有包里的脚本与夹具,不发布、不进测试套件;回滚即删除该目录并还原 dogfood 的 tsconfig 一行。 - 先看:`packages/qa/dogfood/bench/sql-perf/bench.mjs`(场景定义与计数方式)、`micro.mjs`(每个"预期收益"数字的来源)、`app/objectstack.config.ts`(数据形状)。 - 两分钟验证:`pnpm --filter '@objectstack/cli...' build` 之后跑 `node packages/qa/dogfood/bench/sql-perf/bench.mjs --dialect sqlite --scale 1000 --iterations 3`,预期约 1 分钟内打印 19 个场景并写出 `results/sqlite-1000.md`,其中 `cross_object_filter_large` 在此规模为 200、`cascade_delete_opportunity` 为 155 条 SQL。 - 需要你做的:在 #33631 的排序清单里挑出 M2 要立的卡。 ## What changes - `packages/qa/dogfood/bench/sql-perf/app/objectstack.config.ts` — the fixture app: `bench_region` (20) -> `bench_account` (scale/100, private OWD) -> `bench_opportunity` (scale/10, controlled_by_parent) -> `bench_line_item` (scale, controlled_by_parent, lookup `bench_product`). A default permission set gives a member `own` read on accounts, so the member's reads compose the nested master chain. - `bench.mjs` — boots the fixture with `os dev --no-watch` on SQLite (`better-sqlite3` SqlDriver) or PostgreSQL, mints a member, writes one template row per object through the API, stops the server, bulk-loads the tables directly, runs `ANALYZE`, reboots and times 18 read scenarios (admin and member) plus a cascade delete over HTTP. Per scenario: client p50/p95, server total, and the exact SQL statement count from the `db` mark of `Server-Timing` (`OS_SERVER_TIMING=true`); the admin's SQL text from `X-OS-Debug-Timing: json`. Every phase has a deadline; the server is torn down by process group. - `micro.mjs` — on a seeded database, times the statement the engine runs today beside the statement it could run (the "expected gain" column of the ranked list), including two temporary-index pairs it drops again. - `README.md` — how to run and read it. - `packages/qa/dogfood/tsconfig.json` — `include` gains `bench/**/*.ts`, so the fixture config is type-checked with the package. Vitest still collects `test/**` only; nothing here runs in CI's test jobs. The inventory, the baseline tables and the ranked list are on the epic: #33631 (ranked-list comment). (Corrected after review#2, see the correction comment on #33631.) Headline: R1 per-measure aggregation filter loads every row into the app (PG 7.3 s at 1M). A conditional aggregate does it in 0.14 s: that was `sum(case when … end)`, and PG `FILTER (WHERE …)` measures the same. R2 an analytics dimension through a lookup answers 500 above the bind limit on both dialects. R3 the default sort column `created_at` has no index. On PG the index alone takes the admin default page, as emitted today, from 229 ms to 0.9 ms at 1M. On SQLite it helps only together with R4: the chained page keeps its plan, and the page goes 1.2–1.7 s to 0.09 ms only once the vacuous chain is dropped. R5: 400 INVALID_FILTER at 100k (~2,000 matching opportunities; ~20,000 at 1M). ## Tests No product code changed, so no unit test; the deliverable is the harness and its readings. <details><summary>Benchmark runs (all four cells of the card's matrix)</summary> - SQLite 100k, SQLite 1M, PG 100k, PG 1M — every scenario measured; seed 1.2 s / 9.3 s / 3.7 s / 32.6 s. Runs were taken at `fc532017` and (PG 1M) `e78703bd`; `bench.mjs` and the fixture are byte-identical between the two and the final head. - Smoke at scale 1000: 19 scenarios, exit 0. - Lock verdicts: `VERDICT command-exit 0` (100k pair), `VERDICT batch-last-exit 0` (1M pair, both parts printed their `wrote … .json` line), `VERDICT command-exit 0` (SQL pairs, both dialects). </details> ## Acceptance notes - PostgreSQL ran on a private cluster in the container (`fsync=off`, `synchronous_commit=off` to speed the bulk load; reads are what is timed). The box is shared with other agents: statement counts are exact, milliseconds indicative. - The member's SQL text is not shown (the server discloses `X-OS-Debug-Timing-Detail` to an admin only); its statement count is exact. - Not covered by this fixture, so not measured: sharing grants (`sys_record_share`), roll-up summaries, Turso remote, a network database. They are in the ranked list as unmeasured items. - Read-only inference, not reproduced, not filed: `SharingService#buildReadFilter` reads at most 5000 `sys_record_share` rows into an `id IN (…)`, so grants past 5000 look silently dropped (fail-closed). Needs a share-seeding leg to measure. - Location: the harness lives in the private `@objectstack/dogfood` package because it needs `@objectstack/spec` (for `defineStack`), the `os` binary and driver-sql's clients, all already dependencies there — no lockfile change, no new package. ## Verification <details><summary>Local gates at head (this PR's derived families)</summary> - `node scripts/pm/dispatch-gates.mjs --commands`: 46 families derived from the diff; all 46 run and reconciled with `--ran` (46 derived, 46 run, 0 unrun). 43 exit 0. - NOT MEASURED locally (environment, not the diff): `check:dts-closure` (exit 1 — this worktree built the CLI closure with `OS_SKIP_DTS=1`, so declaration files are absent), `check:dual-build-cjs-loads` (exit 3, PREREQUISITE NOT MET — no full `pnpm build`), `check-plugin-teardown-shape.mjs --self-test` (exit 3 — shallow clone cannot reach its pinned positive-control commit). CI's full build runs all three. - `tsc -p` over the dogfood compiler options restricted to `bench/**/*.ts`: exit 0, `objectstack.config.ts` in the program (`--listFiles`). - `eslint --no-inline-config packages/qa/dogfood/bench`: exit 0. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_016WV4d1QwJJ5KGZTThP4fnq)_ <!-- signed: domain:server#5 · os-elon-musk · session_016WV4d1QwJJ5KGZTThP4fnq --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 854d383 commit 777877d

5 files changed

Lines changed: 1029 additions & 1 deletion

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# SQL performance baseline
2+
3+
A repeatable benchmark of the read paths the SQL-performance epic is about, on a
4+
real SQL database: SQLite (the `better-sqlite3` SqlDriver) and PostgreSQL. It is
5+
the M0 baseline: it measures, it fixes nothing, and it is not part of the
6+
dogfood test suite (vitest only collects `test/**`).
7+
8+
## Run it
9+
10+
```bash
11+
pnpm --filter '@objectstack/cli...' build # the closure `os dev` needs; OS_SKIP_DTS=1 is fine
12+
node packages/qa/dogfood/bench/sql-perf/bench.mjs --dialect sqlite --scale 100000
13+
node packages/qa/dogfood/bench/sql-perf/bench.mjs --dialect pg --scale 100000 \
14+
--pg-url postgres://postgres@127.0.0.1:5432/postgres
15+
```
16+
17+
| flag | default | meaning |
18+
|:--|:--|:--|
19+
| `--dialect` | `sqlite` | `sqlite` or `pg` |
20+
| `--scale` | `100000` | line-item rows; parents are `scale/10` opportunities and `scale/100` accounts |
21+
| `--iterations` / `--warmup` | `20` / `3` | timed and untimed requests per scenario |
22+
| `--pg-url` | `$OS_BENCH_PG_URL` | an admin connection; the run drops and recreates `os_sqlperf_<scale>` there |
23+
| `--work-dir` | `$TMPDIR/os-sql-perf-bench` | the SQLite file, the server logs, `results/` |
24+
| `--only` | all | a comma-separated subset of scenario names |
25+
| `--boot-timeout-ms`, `--request-timeout-ms`, `--seed-timeout-ms` | 240 s, 120 s, 30 min | every phase has a deadline; an overrun fails the run instead of hanging |
26+
27+
The run writes `results/<dialect>-<scale>.json` (every sample, plus the SQL text
28+
of each admin scenario) and `results/<dialect>-<scale>.md` (the table).
29+
30+
## What it does
31+
32+
1. Boots `./app` with `os dev --no-watch` on the chosen database, so schema sync
33+
creates the tables and indexes exactly as a deployment would.
34+
2. Signs in the dev admin, mints a member through `POST /auth/admin/create-user`,
35+
and writes one row per object through the API, to learn the system columns
36+
(organization, owner) the runtime stamps.
37+
3. Stops the server and bulk-loads the tables directly (a million rows through
38+
the API would take hours), then runs `ANALYZE`.
39+
4. Boots again and times each scenario over HTTP, sequentially, with
40+
`OS_SERVER_TIMING=true`.
41+
42+
### The data set
43+
44+
`bench_region` (20) <- `bench_account` (scale/100, **private** OWD, lookup region)
45+
<- `bench_opportunity` (scale/10, `controlled_by_parent`, master_detail account)
46+
<- `bench_line_item` (scale, `controlled_by_parent`, master_detail opportunity,
47+
lookup `bench_product` (200)). The member owns every tenth account, so it reads
48+
10% of every level, through the nested controlled-by-parent chain.
49+
50+
### The numbers
51+
52+
- **p50 / p95 ms** — client wall time per request (nearest rank).
53+
- **server p50 ms** — the `total` mark of `Server-Timing`.
54+
- **db ms p50** and **SQL statements** — the `db` mark: the SQL driver counts
55+
every statement a request runs, identity and permission reads included.
56+
- The admin's SQL text comes from `X-OS-Debug-Timing: json`, which the server
57+
discloses to an admin only; the member's count is exact, its text is not shown.
58+
59+
The machine is shared and the numbers are wall-clock: compare runs on the same
60+
box, read the statement counts as exact and the milliseconds as indicative.
61+
62+
## SQL pairs (`micro.mjs`)
63+
64+
On a database `bench.mjs` has seeded, `micro.mjs` times the statement the engine
65+
runs today beside the one it could run (same flags: `--dialect`, `--scale`,
66+
`--pg-url`, `--work-dir`). Its "today" SQL is hand-copied from the statements
67+
`bench.mjs` captured, so each report carries the commit it was taken at. The
68+
index pairs create a temporary index, drop any left-over copy before the
69+
"before" reading, and print the planner's plan beside the time: a gain that
70+
comes with an unchanged plan is noise, not the index. The default-order pair is
71+
timed twice on the admin page: chained, as the engine emits it today, and with
72+
the vacuous controlled-by-parent chain dropped.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* The SQL performance baseline's fixture app (epic "SQL performance", M0).
5+
*
6+
* Three levels of parents over one high-volume child, plus one lookup target:
7+
*
8+
* bench_region (L0, public) 20 rows
9+
* bench_account (L1, private OWD) lookup region scale / 100
10+
* bench_opportunity (L2, CBP) master_detail account scale / 10
11+
* bench_line_item (child, CBP) master_detail opportunity, lookup product scale
12+
* bench_product (lookup target, public) 200 rows
13+
*
14+
* `bench_account` is `private` and both details are `controlled_by_parent`, so a
15+
* non-admin member's read of a line item composes the nested master chain
16+
* (line item -> opportunity -> account owner). That is the read-scoped scenario.
17+
*
18+
* Rows are NOT seeded here: `../bench.mjs` bulk-loads them straight into the
19+
* tables after schema sync, because a million rows through the API would take
20+
* hours. See `../README.md`.
21+
*/
22+
import { defineStack } from '@objectstack/spec';
23+
24+
const readWrite = { allowRead: true, allowCreate: true, allowEdit: true };
25+
26+
export default defineStack({
27+
manifest: {
28+
id: 'com.objectstack.bench.sqlperf',
29+
namespace: 'bench',
30+
version: '0.0.0',
31+
type: 'app',
32+
name: 'SQL performance baseline fixture',
33+
description: 'Fixture for the SQL performance baseline: three parent levels over one high-volume child.',
34+
engines: { protocol: '^18' },
35+
},
36+
37+
objects: [
38+
{
39+
name: 'bench_region',
40+
label: 'Region',
41+
sharingModel: 'public_read_write',
42+
fields: {
43+
name: { type: 'text', label: 'Name', required: true },
44+
code: { type: 'text', label: 'Code' },
45+
},
46+
},
47+
{
48+
name: 'bench_product',
49+
label: 'Product',
50+
sharingModel: 'public_read_write',
51+
fields: {
52+
name: { type: 'text', label: 'Name', required: true },
53+
family: {
54+
type: 'select',
55+
label: 'Family',
56+
options: [
57+
{ label: 'Hardware', value: 'hardware' },
58+
{ label: 'Software', value: 'software' },
59+
{ label: 'Services', value: 'services' },
60+
{ label: 'Training', value: 'training' },
61+
],
62+
},
63+
},
64+
},
65+
{
66+
name: 'bench_account',
67+
label: 'Account',
68+
sharingModel: 'private',
69+
fields: {
70+
name: { type: 'text', label: 'Name', required: true },
71+
region: { type: 'lookup', label: 'Region', reference: 'bench_region' },
72+
industry: {
73+
type: 'select',
74+
label: 'Industry',
75+
options: [
76+
{ label: 'Tech', value: 'tech' },
77+
{ label: 'Finance', value: 'finance' },
78+
{ label: 'Retail', value: 'retail' },
79+
{ label: 'Health', value: 'health' },
80+
{ label: 'Energy', value: 'energy' },
81+
],
82+
},
83+
annual_revenue: { type: 'number', label: 'Annual revenue' },
84+
},
85+
},
86+
{
87+
name: 'bench_opportunity',
88+
label: 'Opportunity',
89+
sharingModel: 'controlled_by_parent',
90+
fields: {
91+
name: { type: 'text', label: 'Name', required: true },
92+
account: {
93+
type: 'master_detail',
94+
label: 'Account',
95+
reference: 'bench_account',
96+
required: true,
97+
deleteBehavior: 'cascade',
98+
},
99+
stage: {
100+
type: 'select',
101+
label: 'Stage',
102+
options: [
103+
{ label: 'Prospecting', value: 'prospecting' },
104+
{ label: 'Proposal', value: 'proposal' },
105+
{ label: 'Negotiation', value: 'negotiation' },
106+
{ label: 'Won', value: 'won' },
107+
{ label: 'Lost', value: 'lost' },
108+
],
109+
},
110+
amount: { type: 'number', label: 'Amount' },
111+
close_date: { type: 'date', label: 'Close date' },
112+
},
113+
},
114+
{
115+
name: 'bench_line_item',
116+
label: 'Line item',
117+
sharingModel: 'controlled_by_parent',
118+
fields: {
119+
name: { type: 'text', label: 'Name', required: true },
120+
opportunity: {
121+
type: 'master_detail',
122+
label: 'Opportunity',
123+
reference: 'bench_opportunity',
124+
required: true,
125+
deleteBehavior: 'cascade',
126+
},
127+
product: { type: 'lookup', label: 'Product', reference: 'bench_product' },
128+
quantity: { type: 'number', label: 'Quantity' },
129+
unit_price: { type: 'number', label: 'Unit price' },
130+
status: {
131+
type: 'select',
132+
label: 'Status',
133+
options: [
134+
{ label: 'Open', value: 'open' },
135+
{ label: 'Shipped', value: 'shipped' },
136+
{ label: 'Invoiced', value: 'invoiced' },
137+
{ label: 'Cancelled', value: 'cancelled' },
138+
],
139+
},
140+
},
141+
},
142+
],
143+
144+
permissions: [
145+
{
146+
name: 'bench_member',
147+
label: 'Bench member',
148+
isDefault: true,
149+
objects: {
150+
bench_region: readWrite,
151+
bench_product: readWrite,
152+
// `own` on purpose: the member reads the accounts it owns, and the two
153+
// controlled_by_parent details follow — the read-scoped scenario.
154+
bench_account: { ...readWrite, readScope: 'own' },
155+
bench_opportunity: readWrite,
156+
bench_line_item: readWrite,
157+
},
158+
},
159+
],
160+
});

0 commit comments

Comments
 (0)