perf(tests): reduce per-feature startup cost with shared fixtures and lazy imports #487

Closed
brent.edwards wants to merge 3 commits from perf/shared-fixtures-lazy-imports into perf/optimize-coverage-pipeline
Member

Summary

  • New scripts/create_template_db.py: Creates a pre-migrated SQLite template database using Base.metadata.create_all() + alembic stamp (~5ms for 34 tables, vs ~0.5-3s × 25 Alembic migrations)
  • noxfile.py: unit_tests and coverage_report sessions generate the template before test execution and propagate CLEVERAGENTS_TEMPLATE_DB env var to all workers
  • features/environment.py: before_all() installs a monkey-patch on MigrationRunner.init_or_upgrade that copies the template for fresh scenario temp DBs, falling through to real migrations for :memory:, existing files, and migration-runner unit tests
  • Quick wins: sleep(0.5)sleep(0.05) in cli_streaming wait step; removed redundant Background re-declaration in cli_streaming.feature scenario 7

Impact

Each BDD scenario that previously spent 0.5-3s running 25 Alembic migrations now gets a fully-migrated DB via file copy in ~1ms. Validated with 556 scenarios across 24 diverse features (0 failures).

Testing

  • 24 representative features tested serially: 556 scenarios passed, 0 failed
  • Migration-runner unit tests verified to still use real Alembic (not intercepted by patch)
  • Template DB verified: 34 tables, alembic_version stamped at HEAD (m4_002_skill_flattened_tools)

Part of #478. Closes #483.

## Summary - **New `scripts/create_template_db.py`**: Creates a pre-migrated SQLite template database using `Base.metadata.create_all()` + alembic stamp (~5ms for 34 tables, vs ~0.5-3s × 25 Alembic migrations) - **noxfile.py**: `unit_tests` and `coverage_report` sessions generate the template before test execution and propagate `CLEVERAGENTS_TEMPLATE_DB` env var to all workers - **features/environment.py**: `before_all()` installs a monkey-patch on `MigrationRunner.init_or_upgrade` that copies the template for fresh scenario temp DBs, falling through to real migrations for `:memory:`, existing files, and migration-runner unit tests - **Quick wins**: `sleep(0.5)` → `sleep(0.05)` in cli_streaming wait step; removed redundant Background re-declaration in cli_streaming.feature scenario 7 ## Impact Each BDD scenario that previously spent 0.5-3s running 25 Alembic migrations now gets a fully-migrated DB via file copy in ~1ms. Validated with 556 scenarios across 24 diverse features (0 failures). ## Testing - 24 representative features tested serially: 556 scenarios passed, 0 failed - Migration-runner unit tests verified to still use real Alembic (not intercepted by patch) - Template DB verified: 34 tables, alembic_version stamped at HEAD (`m4_002_skill_flattened_tools`) Part of #478. Closes #483.
Replace per-scenario Alembic migration runs (~0.5-3s × 25 migrations)
with a pre-migrated template SQLite database copied in ~1ms:

- New scripts/create_template_db.py creates a fully-migrated SQLite file
  using Base.metadata.create_all() + alembic stamp (34 tables, ~5ms).
- noxfile.py unit_tests and coverage_report sessions generate the
  template before test execution, propagating CLEVERAGENTS_TEMPLATE_DB
  env var to all behave-parallel workers.
- features/environment.py before_all() installs a monkey-patch on
  MigrationRunner.init_or_upgrade that copies the template for fresh
  scenario temp DBs (cleveragents_* / cleveragents_test_* prefix),
  falling through to real migrations for :memory:, existing files,
  non-SQLite, and custom URLs used by migration-runner unit tests.

Quick wins:
- cli_streaming_steps.py: sleep(0.5) → sleep(0.05) in wait-for-completion
- cli_streaming.feature: remove redundant Background re-declaration in
  scenario 7 (already inherited from Feature-level Background)

Part of #478. Closes #483.
freemo left a comment

Code Review: PR #487 — perf(tests): reduce per-feature startup cost with shared fixtures and lazy imports

What I Fixed

  • Labels: Removed incorrectly applied State/Verified, Priority/High, MoSCoW/Should have, Points/8 labels (these belong on issues only per CONTRIBUTING.md §8.1). Only Type/Task remains.

Code Quality Assessment

Well-designed performance optimization. Key observations:

  1. scripts/create_template_db.py — Clean implementation. Uses Base.metadata.create_all() for fast DDL + alembic stamp to mark the DB as fully migrated. Proper error handling for missing revisions. The script is self-contained and reusable.

  2. Monkey-patch in environment.py — The _install_template_db_patch() function is well-structured with clear fall-through logic for non-matching cases (non-SQLite, :memory:, existing files, non-matching prefixes). The _SCENARIO_DB_PREFIXES tuple correctly scopes the optimization.

  3. noxfile.py changes — The _create_template_db() helper is clean. Integration into both unit_tests and coverage_report sessions via env var propagation is correct.

  4. cli_streaming.feature — Removing the redundant Background re-declaration (Given steps that were inherited from Feature-level Background) is a legitimate cleanup.

  5. cli_streaming_steps.pysleep(0.5)sleep(0.05) is a reasonable quick win for a wait-for-completion step.

  6. Minor concern: tempfile.mktemp() is used in before_scenario (deprecated due to race conditions), but since this is test scaffolding, the risk is negligible.

No bugs identified. The template-copy approach is a sound architectural choice.

Process Violations Requiring Changes

  1. Merge commits present — This PR has 3 commits (2 merge commits from syncing with perf/optimize-coverage-pipeline + 1 real commit). Per CONTRIBUTING.md §4.1, each PR should contain exactly one atomic commit. Please rebase and squash to a single commit.

  2. Missing ISSUES CLOSED: footer in commit message — The commit uses Closes #483. in the body, but CONTRIBUTING.md §5.5 requires:

    ISSUES CLOSED: #483
    
  3. Missing CHANGELOG.md update — Per CONTRIBUTING.md §8.3, every PR must include a CHANGELOG entry.

  4. Missing milestone — Per CONTRIBUTING.md §8.2, every PR must have a milestone matching the linked issue. Neither this PR nor issue #483 has a milestone set.

  5. Missing dependency links — Per CONTRIBUTING.md §7.3-7.4, this PR should "block" #483, and issue #483 should "depend on" #487.

Verdict

REQUEST CHANGES — Code quality is good; process requirements must be addressed.

## Code Review: PR #487 — perf(tests): reduce per-feature startup cost with shared fixtures and lazy imports ### What I Fixed - **Labels**: Removed incorrectly applied `State/Verified`, `Priority/High`, `MoSCoW/Should have`, `Points/8` labels (these belong on issues only per CONTRIBUTING.md §8.1). Only `Type/Task` remains. ### Code Quality Assessment Well-designed performance optimization. Key observations: 1. **`scripts/create_template_db.py`** — Clean implementation. Uses `Base.metadata.create_all()` for fast DDL + `alembic stamp` to mark the DB as fully migrated. Proper error handling for missing revisions. The script is self-contained and reusable. 2. **Monkey-patch in `environment.py`** — The `_install_template_db_patch()` function is well-structured with clear fall-through logic for non-matching cases (non-SQLite, `:memory:`, existing files, non-matching prefixes). The `_SCENARIO_DB_PREFIXES` tuple correctly scopes the optimization. 3. **`noxfile.py` changes** — The `_create_template_db()` helper is clean. Integration into both `unit_tests` and `coverage_report` sessions via env var propagation is correct. 4. **`cli_streaming.feature`** — Removing the redundant Background re-declaration (Given steps that were inherited from Feature-level Background) is a legitimate cleanup. 5. **`cli_streaming_steps.py`** — `sleep(0.5)` → `sleep(0.05)` is a reasonable quick win for a wait-for-completion step. 6. **Minor concern**: `tempfile.mktemp()` is used in `before_scenario` (deprecated due to race conditions), but since this is test scaffolding, the risk is negligible. No bugs identified. The template-copy approach is a sound architectural choice. ### Process Violations Requiring Changes 1. **Merge commits present** — This PR has 3 commits (2 merge commits from syncing with `perf/optimize-coverage-pipeline` + 1 real commit). Per CONTRIBUTING.md §4.1, each PR should contain exactly one atomic commit. Please rebase and squash to a single commit. 2. **Missing `ISSUES CLOSED:` footer in commit message** — The commit uses `Closes #483.` in the body, but CONTRIBUTING.md §5.5 requires: ``` ISSUES CLOSED: #483 ``` 3. **Missing CHANGELOG.md update** — Per CONTRIBUTING.md §8.3, every PR must include a CHANGELOG entry. 4. **Missing milestone** — Per CONTRIBUTING.md §8.2, every PR must have a milestone matching the linked issue. Neither this PR nor issue #483 has a milestone set. 5. **Missing dependency links** — Per CONTRIBUTING.md §7.3-7.4, this PR should "block" #483, and issue #483 should "depend on" #487. ### Verdict **REQUEST CHANGES** — Code quality is good; process requirements must be addressed.
Owner

Superseded by #493 — this PR has been consolidated with all code review fixes applied into a single clean branch (perf/bdd-test-optimization) with one commit per issue and no merge commits.

Superseded by #493 — this PR has been consolidated with all code review fixes applied into a single clean branch (`perf/bdd-test-optimization`) with one commit per issue and no merge commits.
freemo closed this pull request 2026-03-02 01:45:31 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core!487
No description provided.