Repository navigation
Commit 004940f
test(driver-sql): lighten the SQLite bulk-insert bound fixtures off the per-row auto-number reservation (#38734)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9BSi9ocXhcrBSeFbQv8dAV4)_
Fixes #38713
Clause-②: no
Metadata-protocol: none
## 维护者速读
用户无感:只改了一个数据库驱动的测试文件,产品行为不变。
数据层测试稳定性:这个测试在繁忙的合并队列上偶尔超时,把无关的改动踢出队列。
测试变轻后耗时约为原来的一半到十五分之一,所有断言保留;若有问题只影响这一个测试文件,可直接撤回。
无。
## What changes
One test file,
`packages/drivers/driver-sql/src/sql-driver-38236-sqlite-bulk-insert-bound.test.ts`.
No driver source, no config, no timeout added.
**Root cause (measured).** Every 1200-row case ran on `bound_ticket`,
which has an auto-number field. `bulkCreate` reserves one auto-number
per row before it builds any INSERT, at about seven statements per row.
A 1200-row batch therefore sent **8,407 statements**, of which 3 were
INSERTs. The reservation loop (`fillAutoNumberFields`) took **95–97% of
the case's time**; the split INSERTs took 30–60 ms. The case is about
the statement split, so most of its time went to something it does not
test.
**Fix.**
- The bound/order case (`inserts every row of a 1200-row batch, in
order, …`) now runs the same 1200 rows on `bound_plain`: the same
columns without the auto-number. The rows-in-order assertion is kept,
and so is 500 + 500 + 200. The transaction assertion is now stronger: it
checks the whole statement list (`begin; insert; insert; insert;
commit;`), not just the tail.
- The cases about the reservation itself (contiguous auto-numbers,
burn-once on failure, the caller's transaction, undo inside the caller's
transaction) now run on `PAST_ROW_BOUND = SQLITE_MAX_INSERT_ROWS + 1`
(501) rows. That is the smallest batch SQLite cannot take as one INSERT,
so each case still spans two statements. In the failure cases the bad
row is the last one, alone in the second statement, so a failing last
statement still rolls back an earlier statement that succeeded.
- The pin that "the reservations are all taken before the batch's one
transaction opens" moves to the auto-number contiguity case, because
that case still has reservations to check.
**How the fixtures still cross both bounds** (constants read from
`sql-driver-bulk-operations.ts` on origin/main: `SQLITE_MAX_INSERT_ROWS
= 500`, `SQLITE_MAX_INSERT_VARIABLES = 32766`):
- Row bound: the order case sends 1200 rows, more than 500, as 3
statements. Each reservation case sends 501 rows, more than 500, as 2
statements, and asserts the statement count.
- Parameter bound: `splits a wide batch by the parameter bound even
under 500 rows` is unchanged. Its 450 rows × 81 bound columns come to
more than 36,000 parameters, past 32,766. The `sqliteInsertStatements`
unit cases (`[1200, 6], [450, 80], [100, 2000], [501, 1]`) are unchanged
too, and check that every statement stays within both bounds.
- Every assertion from before is still in the file.
## Tests
All runs at `f31d30bd`, on a 4-core shared container.
<details><summary>Per-test durations, 5 idle runs each, before
(origin/main d2caaec) and after</summary>
| case | before (ms) | after (ms) |
|:---|:---|:---|
| inserts every row of a 1200-row batch (order/bounds) | 1389 · 1592 ·
1693 · 1558 · 1448 | 82 · 76 · 97 · 102 · 78 |
| deals the batch's auto-numbers contiguously | 1425 · 1285 · 1492 ·
1244 · 1398 | 578 · 652 · 784 · 566 · 603 |
| leaves nothing stored when the LAST statement fails | 1666 · 1277 ·
1454 · 1238 · 1164 | 697 · 668 · 693 · 583 · 560 |
| runs inside the caller's transaction | 1390 · 1194 · 1026 · 1039 ·
1305 | 573 · 439 · 443 · 628 · 553 |
| undoes a failed split batch inside the caller's transaction | 1294 ·
1175 · 1141 · 1123 · 1337 | 452 · 568 · 438 · 437 · 410 |
| whole file (13 tests) | 8895 · 8477 · 8453 · 7956 · 8474 | 4012 · 3881
· 3937 · 3889 · 4143 |
All 5 after-runs: 13/13 passed.
</details>
<details><summary>Under load: 6 CPU burners on 4 cores, 2 runs
each</summary>
| case | before (ms) | after (ms) |
|:---|:---|:---|
| order/bounds (1200 rows) | 2013 · 1956 | 214 · 204 |
| auto-number contiguity | 2071 · 2544 | 1414 · 1049 |
| whole file | 13971 · 12245 | 6533 · 7961 |
On CI the order/bounds case took 5,376 ms against an idle time of about
1.5 s, roughly 3.5×. At that ratio the heaviest case after the change,
auto-number contiguity at about 0.6–0.8 s idle, would take about 2.8 s,
inside the 5 s default. No per-test timeout is added.
</details>
<details><summary>Where the time goes: probe of bulkCreate on SQLite in
memory, 3 idle runs</summary>
| fixture | rows | total | fillAutoNumberFields | rest | statements |
INSERTs |
|:---|:---|:---|:---|:---|:---|:---|
| auto-number | 500 | 566–609 ms | 548–592 ms | 18–34 ms | 3503 | 1 |
| auto-number | 501 | 529–595 ms | 513–574 ms | 15–21 ms | 3513 | 2 |
| auto-number | 1200 | 1160–1270 ms | 1116–1237 ms | 33–58 ms | 8407 | 3
|
| plain | 1200 | 29–33 ms | 1–6 ms | 27–29 ms | 5 | 3 |
The probe was a throwaway file. It was deleted and is not in the diff.
</details>
<details><summary>Package suite and typecheck at f31d30b</summary>
`pnpm --filter @objectstack/driver-sql test`: Test Files 274 passed | 14
skipped (288), Tests 4602 passed | 268 skipped (4870). `pnpm --filter
@objectstack/driver-sql typecheck`: exit 0. `tsc --listFiles` includes
the edited test file.
</details>
## Acceptance notes
- Driver observation, not filed: `bulkCreate` reserves auto-numbers one
row at a time. Each reservation is its own committed transaction of
about 7 statements, so on SQLite an N-row batch with one auto-number
field sends about 7N statements before its INSERTs (about 1 ms per row
idle here). This is how the driver is designed to work, not a defect; it
is noted for whoever next looks at seed or import throughput.
- No ablation: the diff changes fixtures only, and every assertion it
moved has the same form as before.
## Verification
Read first: the test file's order/bounds case (the fixture moved to
`bound_plain`), then the auto-number contiguity case (where the moved
transaction pin now lives).
Two-minute check: `pnpm --filter @objectstack/driver-sql exec vitest run
src/sql-driver-38236-sqlite-bulk-insert-bound.test.ts`. Expect 13
passed. The 1200-row order case should take about 0.1 s and the file
about 4 s idle.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01ADHkmTLwBddAv8qQwmLzdk
<!-- signed: repo:objectstack#3 · os-steve ·
session_01ADHkmTLwBddAv8qQwmLzdk -->
Co-authored-by: Claude <noreply@anthropic.com>1 parent cb8d853 commit 004940f
1 file changed
Lines changed: 45 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
22 | 30 | | |
23 | 31 | | |
24 | 32 | | |
| |||
40 | 48 | | |
41 | 49 | | |
42 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
43 | 60 | | |
44 | 61 | | |
45 | 62 | | |
| |||
71 | 88 | | |
72 | 89 | | |
73 | 90 | | |
74 | | - | |
| 91 | + | |
75 | 92 | | |
76 | 93 | | |
77 | 94 | | |
| |||
82 | 99 | | |
83 | 100 | | |
84 | 101 | | |
85 | | - | |
| 102 | + | |
86 | 103 | | |
87 | 104 | | |
88 | 105 | | |
89 | 106 | | |
90 | | - | |
| 107 | + | |
91 | 108 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
97 | 112 | | |
98 | 113 | | |
99 | 114 | | |
| |||
110 | 125 | | |
111 | 126 | | |
112 | 127 | | |
113 | | - | |
| 128 | + | |
| 129 | + | |
114 | 130 | | |
115 | | - | |
| 131 | + | |
116 | 132 | | |
117 | 133 | | |
118 | 134 | | |
119 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
120 | 141 | | |
121 | 142 | | |
122 | 143 | | |
| |||
128 | 149 | | |
129 | 150 | | |
130 | 151 | | |
131 | | - | |
132 | | - | |
133 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
134 | 155 | | |
135 | | - | |
| 156 | + | |
136 | 157 | | |
137 | 158 | | |
138 | | - | |
| 159 | + | |
139 | 160 | | |
140 | 161 | | |
141 | 162 | | |
142 | | - | |
| 163 | + | |
| 164 | + | |
143 | 165 | | |
144 | 166 | | |
145 | 167 | | |
146 | 168 | | |
147 | | - | |
148 | | - | |
149 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
150 | 173 | | |
151 | 174 | | |
152 | 175 | | |
153 | 176 | | |
154 | 177 | | |
155 | 178 | | |
156 | 179 | | |
157 | | - | |
158 | | - | |
| 180 | + | |
| 181 | + | |
159 | 182 | | |
160 | 183 | | |
161 | 184 | | |
| |||
0 commit comments