Skip to content

perf(tests): copy a migrated template instead of migrating every test - #43

Open
PermutationResearch wants to merge 8 commits into
fix/hearth-clippyfrom
fix/hearth-template-db
Open

PermutationResearch wants to merge 8 commits into
fix/hearth-clippyfrom
fix/hearth-template-db

Conversation

@PermutationResearch

Copy link
Copy Markdown
Owner

Every TestDb created an empty database and replayed all 99 migrations. On one 16-test binary that was 113.31 s of migrations for 8 s of test work — and the workspace suite is 1,554 tests across 139 binaries, which is most of why a full run passes 40 minutes.

What changed

crates/hearth-control/tests/common/mod.rs only:

  • each migration cut is migrated once into hearth_tpl_<version>_<digest>;
  • every test copies it with CREATE DATABASE … TEMPLATE instead of CREATE DATABASE + db::migrate_with;
  • the name derives from the cut, so an unchanged schema reuses an earlier run's template and a changed one builds beside it;
  • creation is serialised by an advisory lock, and the template's own connection is closed before first use, because Postgres refuses a template with live connections;
  • a copy that races another gets 20 bounded retries on is being accessed by other users.

No change to isolation: every test still gets its own database and drops it (including on panic), and TestDb::existing, partial-migration cuts and HEARTH_TEST_DATABASE_ROLE all behave as before.

Measured here

Same machine, same binary (approval_rules, 16 tests, four macOS-only setpriv tests skipped):

test body
before (CREATE DATABASE + migrate per test) 113.31 s
after (template copy) 15.83 s

7.2× on the test body. The expected effect on CI is the same order: migrations were per test, not per run.

Notes

@PermutationResearch
PermutationResearch changed the base branch from fix/hearth-ci to fix/hearth-clippy December 1, 2026 02:54
Every TestDb created an empty database and replayed all 99 migrations, so one
test binary of 16 tests spent 113 s of its 8 s of actual work on migrations.
The suite is 1,554 tests; this is most of why a full run takes over 40 minutes.

Each migration cut is now migrated once into `hearth_tpl_<version>_<digest>`
and every test copies it with CREATE DATABASE ... TEMPLATE. The name comes from
the cut, so an unchanged schema reuses an earlier run's template, and a changed
one builds beside it. Creation is serialised by an advisory lock, the template
connection is closed before use (Postgres refuses a template with live
connections), and a copy that races another gets 20 bounded retries on
"being accessed by other users". A cut with no migrations gets the tag
"empty", which is shorter than a digest prefix, so the tag is clamped.

Same 16-test binary, same machine, with the four macOS-only `setpriv` tests
skipped: 113.31 s -> 15.83 s. Across the whole suite on CI the test bodies
went from 1148 s to 506 s.
28 of the 31 failing hearth-control tests switch to the restricted
serving role and failed with `role "hearth_app" does not exist`: the
CI cluster never had it. Create it once, as database-roles.md says,
through the service container's psql.

purchase_gateway_e2e runs the payments adapter's SDK peer under Node
24+ with its production dependencies and failed on an unset
HEARTH_TEST_NODE. Install Node 24 and the adapter's locked production
dependencies when integration tests run, and point HEARTH_TEST_NODE
at that node.
the_api_needs_a_live_session signed in with `login`, which also opens
a browser session, so `call` sent the cookie to /auth/logout. That
route ends CLI sessions and reads only a bearer, so it answered 401.
Sign in with `login_cli`, as the test means a CLI session.

the_scanner_knows_the_stores_the_database_lets_it_redact compared a
byte-sorted list with one sorted by the cluster's default collation.
Under en_US.utf8, as in CI, `thread_renders` sorts before
`thread_render_snapshots`. Order the query by COLLATE "C".
migrate grants hearth_app its rights only if the role exists. The
template name came from the migration cut alone, so a template built
before an operator created the role kept being copied without those
grants, and the restricted-role tests failed on that machine. Record
the role's existence in the template name.
Publication fast-forwards with `git merge` through a throwaway git
directory removed by an EXIT trap. After a merge, Git runs automatic
maintenance and by default detaches it into the background, where it
can still be writing in that directory while the trap removes it. Under
`set -euo pipefail` the failed `rm -rf` exits 1, so a completed
publication reports as refused.

CI on Git 2.56.1 hit this once (fleet_integration's sequential merge
test: "rm: cannot remove '/tmp/tmp.…': Directory not empty", then
status 1); the same test passed on the previous run. Not reproduced
locally on Git 2.50.1. Pass maintenance.auto=false to the view's git.
Since "Decisions and anything that widens authority need a browser",
sharing a machine and approving a purchase refuse a CLI token. Two
tests still sent one, hidden until now behind the missing hearth_app
role and Node:

- workspace_reviews granted Bob access with Alice's bearer over HTTP.
  Grant through Harness::call, which sends her browser session, as the
  fixture's own setup already does.
- purchase_gateway_e2e's SDK peer approved purchases with the bearer.
  Pass Alice's browser headers to the peer and approve with them; the
  agent side keeps its token.
The key was Cargo.lock alone, so a cache saved by a run that built one
package was an exact hit for a run that builds the whole workspace.
An exact hit skips the save, so #43's whole-workspace runs restored a
hearth-control-only target, rebuilt most crates (33 min) and saved
nothing.

Work out the scope before restoring and add a hash of it to the key.
A run whose scope has no cache yet restores the newest cache for the
same Cargo.lock, builds the rest and saves its own. The prefix stays
v2: existing v2 entries are complete for their scope and remain
useful fallbacks.
An exact hit on the whole-workspace cache still took 29m50s to build the
test binaries, against 26m52s from a partial fallback: checkout gives
workspace sources new timestamps, so Cargo rebuilds all 25 workspace
crates on every run. Their artifacts doubled the entry to 6 GB and its
restore to four minutes without saving any build time.

Drop workspace artifacts with cargo clean -p before saving. The test
binaries are workspace artifacts, so the save moves after the tests and
runs whenever the build succeeded, failing tests included. Bump the key
to v3 so the full v2 entry is never restored again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants