magpie icon indicating copy to clipboard operation
magpie copied to clipboard

TestTOMLBigFileParsesInLinearTime: a 1s wall-clock bound on a 3 MB parse fails under load (1.27s)

Open yetone opened this issue 1 month ago • 3 comments

Seen while running go test -tags nogui ./... for #1928 on a worktree of origin/main 429698f96 plus the #1928 commit (which touches only internal/catalog and internal/plugin), on a Mac with load average ~190:

--- FAIL: TestTOMLBigFileParsesInLinearTime (1.40s)
    toml_shape_test.go:62: a 3 MB file took 1.272504875s to parse

Rerun alone, -count=3, it passes (internal/edit 2.4s).

Cause: the test bounds the parse by wall-clock time (d > time.Second), which a loaded runner can pass without the parse being quadratic. The code standards' "a timing test leaves room on both sides" applies: the quadratic version took far longer (a third of a second per start on a 1.5 MB file, per the test's comment), so the check can compare against size instead, e.g. time a 3 MB and a 6 MB file and require the ratio to stay well under 4, or count line scans rather than time them.

Not fixed here; filed so the red has an owner.

yetone avatar Oct 14 '26 15:10 yetone

Took this one (state was OPEN with no claim; git log --grep=2219 shows no fix on main as of b1d02fa6). Since the repo doesn't take pull requests, the fix is on a branch of my fork for cherry-picking:

git fetch https://github.com/Niko-Wu/magpie fix/2219 && git cherry-pick 818573f7

(based on current b1d02fa6, single commit, test-only, no conflict surface)

Root cause — internal/edit/toml_shape_test.go:61 bounded the 3 MB parse by one wall-clock second (d > time.Second). The parse is linear; a runner at load average ~190 preempts it past the bound (the report's 1.27s) and the test goes red without anything being quadratic. That is the code standards' "a timing test leaves room on both sides": never bound a test by wall-clock time a loaded runner can exceed.

The fix (internal/edit/toml_shape_test.go, the only file touched)

  • toml_shape_test.go:48-55 — the fixture becomes tomlBigFile(size), parameterized.
  • toml_shape_test.go:59-75 — parseTOMLTimed parses three times and keeps the fastest, so a preemption on one run can't decide the check.
  • toml_shape_test.go:87-101 — the test now parses a 2 MB and an 8 MB file and requires the 8 MB time to stay under 8× the 2 MB one. A linear parse needs 4× the time for the 4× bigger file (measured 3.7–4.5× here); the per-key line scan this test guards against needs 16×. The bound sits between the two, far from both: load slows both parses together, so their ratio holds where a wall-clock second does not.

Test, red before and green after — I reverted tomlShaper's offset table locally to counting each key's line from the top of the file (the pre-2a4042a9 behavior), and the new check fails on it:

toml_shape_test.go:99: an 8 MB file took 1m24.964746625s to parse, 16.0× the 2 MB one's 5.321180583s: near the 16× of size squared, not the 4× of size
--- FAIL: TestTOMLBigFileParsesInLinearTime (271.35s)

The injection was reverted after; the shipped diff is the test file only.

Verification

  • go test -tags nogui ./internal/edit/ → ok github.com/yetone/magpie/internal/edit 3.329s
  • the two tests of this file with -count=3 → 6/6 pass
  • gofmt -l internal/edit/ → empty
  • GOOS=windows go vet -tags nogui ./internal/edit/ (CGO_ENABLED=0) and GOOS=linux the same → clean. With cgo on, this Mac's SDK cannot compile a dependency's linux_syscall.c; the same failure shows on unmodified main, so the cross-OS vet ran with CGO_ENABLED=0.
  • I did not reproduce the load red locally; the issue's own output is the red. The evidence here is that the new check still catches the real regression (the quadratic injection above) and passes 6/6 on the linear path.

Sibling timing assertions — grepped every package's tests for the same shape (a success path bounded by absolute wall-clock time where load alone can cross it). Most time.Since assertions bound waiting for an event against a generous multiple of an injected delay (e.g. internal/proc/wait_test.go:26, 3s against a 300ms deadline) or read a staleness threshold in minutes; those are load-tolerant by construction. The one same-shape site left is internal/agent/preview_test.go:25 (diffLines on a large fixture bounded at 2s wall clock). Its input is smaller and its bound looser than #2219's, so I left it out of this fix — filing it is this issue's maintainer's call.

No .github/, no site/, no dependency changes.

Niko-Wu avatar Oct 15 '26 03:10 Niko-Wu

Seen again 10-20, full go test -timeout 45m ./... on a batch at 8ea089104 (on origin/main b9d1cd36f), load average ~305:

--- FAIL: TestTOMLBigFileParsesInLinearTime (2.35s)
    toml_shape_test.go:62: a 3 MB file took 1.957135083s to parse

The batch doesn't touch internal/edit. Run alone right after, it passed 3 of 3 (ok 2.5s). Still the wall-clock bound under load, as filed; nobody has taken it yet.

yetone avatar Oct 19 '26 19:10 yetone

Fixed on main: 36200091a is @Niko-Wu's patch from this thread as written (thanks, and sorry it sat this long), and 99299c7b1 widens it. On mote at load 250 the 2 MB/8 MB bound of 8 went red 1 run in 13, so the sizes are now 1 MB and 8 MB (linear 8×, quadratic 64×) under a bound of 24, fastest of five parses each. 30 of 30 ok at load 214; with the shaper broken back to a scan from the top it is red at 71.4×.

yetone avatar Oct 28 '26 17:10 yetone