Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LoadScope scheduler: Sort scopes by number of tests to assign biggest scopes first #778

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/632.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``--dist=loadscope`` now sorts scopes by number of tests to assign largets scopes early -- in many cases this should improve overall test session running time, as there is less chance of a large scope being left to be processed near the end of the session, leaving other workers idle.
9 changes: 8 additions & 1 deletion src/xdist/scheduler/loadscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,18 @@ def schedule(self):
return

# Determine chunks of work (scopes)
unsorted_workqueue = OrderedDict()
for nodeid in self.collection:
scope = self._split_scope(nodeid)
work_unit = self.workqueue.setdefault(scope, default=OrderedDict())
work_unit = unsorted_workqueue.setdefault(scope, default=OrderedDict())
work_unit[nodeid] = False

# Insert tests scopes into work queue ordered by number of tests.
for scope, nodeids in sorted(
unsorted_workqueue.items(), key=lambda item: -len(item[1])
):
self.workqueue[scope] = nodeids

# Avoid having more workers than work
extra_nodes = len(self.nodes) - len(self.workqueue)

Expand Down
16 changes: 16 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,22 @@ def test(self, i):
"test_a.py::TestB", result.outlines
) in ({"gw0": 10}, {"gw1": 10})

def test_workqueue_ordered_by_size(self, pytester: pytest.Pytester) -> None:
test_file = """
import pytest
@pytest.mark.parametrize('i', range({}))
def test(i):
pass
"""
pytester.makepyfile(test_a=test_file.format(10), test_b=test_file.format(20))
result = pytester.runpytest("-n2", "--dist=loadscope", "-v")
assert get_workers_and_test_count_by_prefix(
"test_a.py::test", result.outlines
) == {"gw1": 10}
assert get_workers_and_test_count_by_prefix(
"test_b.py::test", result.outlines
) == {"gw0": 20}

def test_module_single_start(self, pytester: pytest.Pytester) -> None:
"""Fix test suite never finishing in case all workers start with a single test (#277)."""
test_file1 = """
Expand Down
Loading