Skip to content

perf(testing): copy a per-process SQLite schema into test databases; cache the Alembic revision map - #866

Merged
antosubash merged 4 commits into
mainfrom
perf/test-app-fixture
Nov 6, 2026
Merged

antosubash merged 4 commits into
mainfrom
perf/test-app-fixture

Conversation

@antosubash

Copy link
Copy Markdown
Owner

Summary

make test-py spent most of its time building the per-test app fixture, not running tests. In a profiled run, fixture setup was 1,796 s of the 2,372 s of worker time (76%). A warm app build took 0.32 s: create_all 0.115 s, lifespan startup 0.13 s, create_app 0.05 s, admin seed 0.025 s.

This PR removes the avoidable parts. Each test still gets its own database.

SQLite schema template (simple_module_test._sqlite_template)

  • The first in-memory schema a process builds is kept in a plain sqlite3 connection.
  • Later empty in-memory databases are filled from it with SQLite's backup API, so CREATE TABLE runs once per worker instead of once per test. This applies to the app and db_session fixtures.
  • A database that already has tables gets the ordinary create_all, so its rows are untouched.
  • The template is rebuilt when the set of tables, their column names or their index names change.
  • File databases and Postgres are unaffected.
  • The schema build now always creates alembic_version. A template built while no Alembic heads resolved therefore can't reach a test that stamps it. That case was found in review; the regression test fails without the fix.

Alembic revision map cache (simple_module_hosting.migrations.script_directory)

  • Each app boot loaded every migration file twice: resolve_head_revisions and then migration_status.
  • The ScriptDirectory is now reused per resolved ini path while the revision files' paths, mtimes and sizes are unchanged. An added, removed or edited file rebuilds it.

Celery task packages (background_tasks.worker.celery_app)

  • The entry-point scan for installed task packages is cached. The installed set cannot change while the process runs.
  • _discover_task_packages keeps its signature, so the tests that monkeypatch it are unaffected.

Numbers

Same machine, 8 workers, pytest -n 8 --dist loadgroup.

before after
pytest time 324 s 249–256 s
setup worker-time 1,796 s 1,189 s
warm app build (idle) 0.32 s 0.19 s
create_all in that build 0.115 s 0.013 s

Test bodies are unchanged (563 → 566 s worker-time).

Verification

  • make lint green.
  • make test-py: 8,172 passed, 47 skipped, in 249 s.
  • make test-js: 900 passed.
  • /ship: an optimize pass, then two reviewers over 3 passes. The last pass was clean. Triage notes are kept locally.
  • New tests:
    • framework/testing/tests/test_sqlite_schema_template.py: the build runs once, indexes are copied, databases are isolated, a database that already has tables keeps its rows, a changed table set rebuilds, and the stamp table survives a build with no heads.
    • framework/hosting/tests/test_migration_script_cache.py: the cache is reused, and a new, edited or removed revision is seen.

https://claude.ai/code/session_01M9neheZZEe3sVpDi2S3zT4

The schema build always creates alembic_version, so a template built while no
Alembic heads resolved cannot reach a test that stamps it. Template key uses
column and index names; template tests close their connections.

Claude-Session: https://claude.ai/code/session_01M9neheZZEe3sVpDi2S3zT4
Revision-file scan tolerates a file removed mid-scan and keys on size too.

Claude-Session: https://claude.ai/code/session_01M9neheZZEe3sVpDi2S3zT4
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Deploying simple-module-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: af085c0
Status: ✅  Deploy successful!
Preview URL: https://8f9b49ca.simple-module-python.pages.dev
Branch Preview URL: https://perf-test-app-fixture.simple-module-python.pages.dev

View logs

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-11-06T13:08:27.143019Z af085c0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@antosubash
antosubash merged commit 4cf3047 into main Nov 6, 2026
17 checks passed
@antosubash
antosubash deleted the perf/test-app-fixture branch November 6, 2026 13:19
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.

1 participant