Skip to content

ci(python): shard the unit tests across three runners - #46356

Closed
rusackas wants to merge 1 commit into
masterfrom
ci/shard-python-unit-tests
Closed

rusackas wants to merge 1 commit into
masterfrom
ci/shard-python-unit-tests

Conversation

@rusackas

Copy link
Copy Markdown
Member

SUMMARY

unit-tests (current) has started hitting its 45-minute timeout with every test passing. On #46262, all 39,979 unit tests passed in 40:12, then the scoped coverage gates ran and the job was cancelled at 45 minutes, during the Codecov upload. #46108 proposed raising the timeout to 60, but the suite keeps growing and the runner gives pytest-xdist only 2 workers, so a bigger timeout just buys time.

This splits the suite across a 3-way matrix instead:

  • tests/pytest_shard.py, a small dependency-free pytest plugin (-p tests.pytest_shard --num-shards N --shard-id K). Each test file hashes (crc32 of its path) to exactly one shard, so module-scoped fixtures and --dist loadfile behave as in a full run. Every xdist worker computes the same selection, and the shards together run each test once. An out-of-range --shard-id is a usage error.
  • Workflow: shard: [1, 2, 3] in the matrix with fail-fast: false. The SDK tests and the scoped 100% coverage gates run on shard 1 only. JUnit artifacts are named per shard (junit-results-current-<shard>), which the report workflow's junit-results-* pattern already picks up. Each shard uploads its coverage, and Codecov merges uploads for the same commit and flags.
  • Branch protection is unchanged: unit-tests-required already aggregates the matrix result.

Estimated from #46262's JUnit timings (72 serial minutes across 951 files), the three shards get about 22, 20 and 30 serial minutes, so roughly 11, 10 and 15 minutes of tests on two workers. Shard 1 adds about 3.5 minutes of coverage gates, so each job should land around 15-17 minutes instead of 40+. Hashing by file isn't perfectly balanced, but it needs no stored timing data and stays stable as files are added.

Follow-ups worth considering (not in this PR):

  • About 1.5s of each module's first-test setup goes to building a fresh app (app is module-scoped and app_context is autouse). Across ~1,000 modules that's roughly 25 of the run's ~80 worker-minutes. A shared app for modules that don't parametrize app would cut that a lot, but it needs care around tests that mutate config.
  • The scoped coverage gates re-run the sql/ and semantic_layers/ tests (~3 minutes) only to measure their own coverage.
  • .codecov.yml still has after_n_builds: 4 from 2021, well below the current number of uploads.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A (CI only).

TESTING INSTRUCTIONS

  • CI on this PR: three unit-tests (current, N) jobs, each well under the timeout, and unit-tests-required green.
  • pytest tests/unit_tests/pytest_shard_test.py
  • Locally, on a synthetic suite of 40 files with module-scoped fixtures under -n 2 --dist loadfile, the three shards ran 40, 18 and 22 of the 80 tests, with no duplicates.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration
  • Introduces new feature or API
  • Removes existing feature or API

An alternative to #46108's timeout bump.

🤖 Generated with Claude Code

@github-actions github-actions Bot added github_actions Pull requests that update GitHub Actions code preset-io labels Nov 19, 2026
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.67%. Comparing base (f357ec3) to head (cf627d3).
⚠️ Report is 8 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #46356   +/-   ##
=======================================
  Coverage   85.66%   85.67%           
=======================================
  Files        3111     3111           
  Lines      214938   214962   +24     
  Branches    50908    50915    +7     
=======================================
+ Hits       184136   184171   +35     
+ Misses      27381    27376    -5     
+ Partials     3421     3415    -6     
Flag Coverage Δ
hive 33.99% <ø> (-0.01%) ⬇️
mysql 51.66% <ø> (-0.01%) ⬇️
postgres 51.69% <ø> (?)
presto 35.68% <ø> (-0.01%) ⬇️
python 88.22% <ø> (-0.08%) ⬇️
sqlite 51.37% <ø> (-0.01%) ⬇️
unit 83.46% <ø> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot added the requires:rebase Requires rebasing on top of current master label Nov 19, 2026
The unsharded unit-test job had grown to ~40 minutes of tests on a
2-core runner (pytest-xdist only gets two workers), so with setup,
the scoped coverage gates and uploads it was hitting the 45-minute
timeout on ordinary variance even when every test passed.

Split the suite by test file across a 3-way matrix with a small
pytest plugin (tests/pytest_shard.py): each file hashes to one shard,
so module-scoped fixtures and --dist loadfile behave as before, and
the shards together run every test once. The SDK tests and the scoped
100% coverage gates run on shard 1 only. Each shard uploads coverage
and its own JUnit artifact; unit-tests-required already aggregates the
matrix, so branch protection is unchanged.

Co-Authored-By: Evan Rusackas <evan@preset.io>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rusackas
rusackas force-pushed the ci/shard-python-unit-tests branch from ecbcf44 to cf627d3 Compare November 20, 2026 00:20
@github-actions github-actions Bot removed the requires:rebase Requires rebasing on top of current master label Nov 20, 2026
@rusackas

Copy link
Copy Markdown
Member Author

Closing this one. Sharding got the unit tests back to about 15 minutes of wall time, but it cost roughly 5 to 13 extra runner-minutes per run (about 41 total across the three shards vs. 28 to 36 for the single job), and we're trying to free up runners, not use more. Most of the extra wasn't setup either... each shard repeats collection and app startup, and the split by file is uneven.

I also tried sharing the app fixture across modules to cut per-file setup instead. That only saved about 10% locally and broke around 15 test files that expect a fresh app, so it's not worth it either.

The single job is still well inside the 45-minute limit, so the timeouts look like slow-runner outliers for now. If they keep happening, skipping this job on PRs that don't touch Python is probably the cheaper win.

@rusackas rusackas closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

github_actions Pull requests that update GitHub Actions code preset-io review:draft size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants