agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

perf(ci): Go build-test takes 12 to 19 minutes as SQLite race tests scale

Open harshitsinghbhandari opened this issue 2 months ago • 4 comments

Bug

The required GitHub Actions check Go / build-test (pull_request) now takes roughly 12 to 19 minutes on successful pull requests. The full race-enabled backend test step is the critical path, particularly backend/internal/storage/sqlite.

Reported by: Harshit Singh Bhandari Analyzed against: 24e101914976a14145d7302f49626db3541ef8b5 Confidence: High. The workflow, job logs, package timings, historical runs, test fixture path, and migration serialization were traced directly. Priority: P1. This delays every backend pull request and has steadily regressed, but does not break correctness.

Current workflow

.github/workflows/go.yml runs this sequence on ubuntu-latest:

  1. actions/checkout@v4
  2. actions/setup-go@v5 using backend/go.mod, with cache: false
  3. Formatting check
  4. go build ./...
  5. go vet ./...
  6. go test -race -timeout=15m ./...

There is no wrapper script, artifact upload, or large log-processing step. The 15 minute package timeout was added in cf350730 after the SQLite package approached Go's default 10 minute timeout.

Measured impact

A sample of 17 successful pull request runs from 2026-09-06 17:47 UTC through 2026-09-07 15:15 UTC showed:

Measurement Minimum Median Approximate p90 Maximum
Whole build-test job 11m40s 15m13s 18m01s 18m40s
Test step 10m42s 14m04s 16m43s 17m21s

The test step consumes about 92 percent of median job time.

Representative runs:

Run Started Job Setup Build Vet Test SQLite package
Fast successful run 2026-09-07 13:04 UTC 11m40s 10s 28s 9s 10m42s 448.732s
Slow successful run 2026-09-07 15:03 UTC 18m40s 15s 42s 14s 17m21s 758.741s
Another slow run 2026-09-07 15:13 UTC 17m39s 12s 40s 13s 16m27s 714.313s

This is a sustained regression, not a one-off slow runner:

Date and run Whole job Test step SQLite package
2026-07-07 2m30s 1m46s 3.499s
2026-08-07 4m23s 3m27s 66.263s
2026-08-26 9m05s 8m04s 293.734s

Across corresponding commits, backend test files grew from 167 to 329 to 444 to 546, SQLite test files from 10 to 20 to 33 to 45, and migrations from 21 to 63 to 93 to about 112. The SQLite package grew much faster than the suite itself.

Root cause

1. Full race instrumentation is the dominant multiplier

On current main, a local comparison of only the two hottest packages with a warm compilation cache produced:

Command Wall time SQLite Store
go test -count=1 ./internal/storage/sqlite ./internal/storage/sqlite/store 22.93s 12.184s 7.903s
go test -race -count=1 ./internal/storage/sqlite ./internal/storage/sqlite/store 597.59s 577.884s 399.236s

Race instrumentation increased wall time by about 26 times and reported package time by roughly 47 to 50 times. Full race coverage is valuable and should be preserved. The suite and fixtures need to make that coverage affordable.

2. Test fixtures repeatedly enter serialized migration work

backend/internal/storage/sqlite/db.go makes every sqlite.Open acquire gooseMu, run the migration-history repair and preparation probes, call goose.Up, and reconcile schema. Current main contains about 112 migration files.

The SQLite tree has 319 tests, roughly 83 open helper calls, and only 9 t.Parallel() calls. Production migration work is intentionally serialized because Goose uses package-level globals.

backend/internal/storage/sqlite/sqlitetest/sqlitetest.go builds one migrated database template with sync.Once, but each clone still calls sqlite.Open. The store suite invokes its test store helper roughly 171 times, so already-current clones repeatedly re-enter Goose and schema repair checks. Dedicated migration compatibility tests also perform many real opens, usually serially.

3. Runner variability amplifies the structural cost

Recent fast and slow runs used distinct GitHub-hosted x64 runners but the same ubuntu-24.04 image version 20260831.293.1. Build, vet, SQLite, store, chat, usage, controller, and spec generation times all move together by roughly 1.5 to 1.7 times. That is consistent with runner CPU or I/O variability, not one intermittent test.

4. Cold caches and duplicate Go toolchain setup add smaller fixed costs

setup-go caching is explicitly disabled. A representative build emitted 45 dependency download lines within about 2.46 seconds, then spent most of its 33 seconds compiling.

Setup installs Go 1.25.7 from backend/go.mod, but root go.work declares Go 1.26.5. GOTOOLCHAIN=auto then downloads Go 1.26.5 as a second toolchain. This costs roughly 5 to 10 seconds, not the multi-minute regression.

5. Actions bookkeeping is negligible

Representative logs are about 469 lines and 55 KB. Post-job cleanup takes 2 to 3 seconds. There are no artifacts.

Recommended optimization order

  1. Measure individual SQLite costs first. Capture go test -race -json timings and count production Open calls per test. Benchmark -p=1, -p=2, and -p=4 to determine whether package concurrency is causing runner thrash.

  2. Open known-current cloned test databases without rerunning migrations. Give sqlitetest a test-only path that establishes the normal writer and reader pools against its verified template without running Goose and repair checks again. Keep every migration compatibility test on the production sqlite.Open path. Assert the cloned schema version and isolation. Estimated pull request wall-time reduction: 4 to 10 minutes, depending on contention.

  3. Reduce repeated migration setup without reducing coverage. Reuse immutable historical schema snapshots where multiple cases start from the same version, consolidate compatible table-driven cases, and measure each change. Preserve tests for clean install, supported historical upgrades, burned or renumbered histories, idempotent reopen, rollback where supported, and schema reconciliation.

  4. Preserve full race coverage while parallelizing its delivery. If fixture work is insufficient, shard stable migration test groups or move the complete race suite into parallel jobs. Do not drop packages from race coverage. Sharding may save 3 to 8 minutes of wall time but increases runner minutes and setup duplication.

  5. Enable correctly keyed Go caching. Configure actions/setup-go with explicit dependency paths for backend/go.sum and workspace inputs. Use -count=1 if all tests must execute while still retaining compilation-cache benefits. Expected warm saving: 30 to 90 seconds. Measure hit rate, restore time, save time, and cache size.

  6. Align the installed toolchain with go.work. Avoid installing 1.25.7 and then automatically downloading 1.26.5. Expected saving: 5 to 10 seconds.

  7. Only then evaluate larger runners. First use the -p measurements to establish whether more CPU improves the critical path enough to justify cost.

Acceptance criteria

  1. Full backend race coverage remains in CI. Optimizing or sharding it must not silently exclude packages.
  2. Migration compatibility coverage remains explicit and continues to exercise production sqlite.Open.
  3. Known-current test clones do not rerun the full migration and repair path.
  4. Compare at least 10 successful pull request runs before and after. Report median and p90 build-test, Test, SQLite, and store timings.
  5. Target a median pull request build-test time below 5 minutes, with no package approaching the test timeout.

Related

  • PR #3669 introduced migrated SQLite test fixtures and reported reducing a full local race run to 2m06s. Continued suite growth has erased that gain, and each cloned fixture still calls sqlite.Open.

No open or closed issue, and no open pull request, was found for this exact current slowdown after searches for build-test, slow CI, go test -race, SQLite fixtures, race detector SQLite, and migration package CI.

harshitsinghbhandari avatar Sep 08 '26 15:09 harshitsinghbhandari

I dug into the slowdown and the issue’s diagnosis holds up. sqlitetest.Open creates a migrated template once, but each cloned DB still goes through sqlite.Open → migrate(), repeating the repair/probe work behind gooseMu.

this gets especially expensive under -race. The main fix looks like giving sqlitetest a path to open verified-current clones without re-running migrations, while keeping production sqlite.Open coverage intact for migration tests.

I’m looking into that path and the schema-version assertion needed to make it safe.

kekubhai avatar Sep 09 '26 04:09 kekubhai

Assigned to @mohakchakraborty2004.

Rishet11 avatar Sep 14 '26 14:09 Rishet11

@Rishet11 , a PR #5162 already solves this , so ill be reviewing it , and mention you there if we can merge it or not

mohakchakraborty2004 avatar Sep 14 '26 18:09 mohakchakraborty2004

Addressed by #5162, #5632 and #6089 on main: migration tests reuse snapshots and run sharded. Reopen if CI time regresses.

Rishet11 avatar Oct 25 '26 16:10 Rishet11