Skip to content

Use worksteal distribution strategy with pytest-xdist - #1916

Open
mhucka wants to merge 5 commits into
quantumlib:mainfrom
mhucka:add-pytest-worksteal
Open

Use worksteal distribution strategy with pytest-xdist#1916
mhucka wants to merge 5 commits into
quantumlib:mainfrom
mhucka:add-pytest-worksteal

Conversation

@mhucka

@mhucka mhucka commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Pytest-xdist has options to control distribution of jobs across workers. Experimenting with different choices led to the discovery that the worksteal strategy speeds up execution of the Qualtran test suite. It appears to have no negative consequences. Using --dist worksteal affords a roughly 20–40% speedup for the quick test and 6% for the full tests.

Rough average timing results:

pytest-quick:

  • Before: ~100 seconds
  • After: ~80 seconds

pytest-full:

  • Before: ~630 seconds
  • After: ~380 seconds

mhucka added 3 commits July 29, 2026 05:38
Pytest-xdist has options to [control distribution of jobs across
workers](https://pytest-xdist.readthedocs.io/en/stable/distribution.html).
Experimenting with different choices led to the discovery that the
`worksteal` strategy speeds up execution of the Qualtran test suite. It
appears to have no negative consequences.

Running this without the thread-limiting PR (), using `--dist worksteal`
affords a roughly 12% speedup for the quick tests, which is a modest
amount but still something.

Rough average timing results:

`pytest-quick`:
* Before: ~185 seconds
* After: ~165 seconds

`pytest-full`:
* Before: ~970 seconds
* After: ~915 seconds
@mhucka mhucka changed the title Add --dist worksteal to Pytest options Use worksteal distribution strategy with pytest-xdist Jul 30, 2026
@mhucka
mhucka marked this pull request as ready for review July 30, 2026 03:05
@mhucka
mhucka requested review from fdmalone and mpharrigan July 30, 2026 03:05

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request refactors pytest_configure in qualtran/conftest.py by extracting thread-limiting logic into _config_set_thread_limits and introducing _config_set_xdist_worksteal to set --dist worksteal as the default distribution mode when using pytest-xdist. It also adds corresponding unit tests in qualtran/conftest_test.py. Feedback highlights two issues in _config_set_xdist_worksteal: it incorrectly overrides the distribution mode during sequential runs (when parallel execution is disabled) and fails to respect --dist configurations defined in addopts within configuration files. A code suggestion is provided to address these issues and safely handle cases where pytest-xdist is not installed.

Comment thread qualtran/conftest.py Outdated
@mhucka mhucka added the devops Involves build systems, Make files, Bazel files, continuous integration, and or other DevOps topics label Jul 30, 2026
This makes it take account of possible user settings.
@mhucka

mhucka commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist

Copy link
Copy Markdown
Contributor

This is a great optimization! Using worksteal is a well-known strategy in pytest-xdist for improving load balancing, especially when test execution times vary significantly. Your implementation correctly handles the configuration logic and preserves user-provided overrides, which is crucial for maintainability. The accompanying tests also look solid and cover the necessary edge cases. One minor suggestion: ensure that the pytest_configure function remains compatible with other plugins that might also be modifying config.option.dist by keeping the logic focused on the default case as you have done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

devops Involves build systems, Make files, Bazel files, continuous integration, and or other DevOps topics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant