Skip to content

Commit 9e011e7

Browse files
gh-82054: allow test runner to split test_asyncio to execute in parallel by sharding. (#103927)
This runs test_asyncio sub-tests in parallel using sharding from Cinder. This suite is typically the longest-pole in runs because it is a test package with a lot of further sub-tests otherwise run serially. By breaking out the sub-tests as independent modules we can run a lot more in parallel. After porting we can see the direct impact on a multicore system. Without this change: Running make test is 5 min 26 seconds With this change: Running make test takes 3 min 39 seconds That'll vary based on system and parallelism. On a `-j 4` run similar to what CI and buildbot systems often do, it reduced the overall test suite completion latency by 10%. The drawbacks are that this implementation is hacky and due to the sorting of the tests it obscures when the asyncio tests occur and involves changing CPython test infrastructure but, the wall time saved it is worth it, especially in low-core count CI runs as it pulls a long tail. The win for productivity and reserved CI resource usage is significant. Future tests that deserve to be refactored into split up suites to benefit from are test_concurrent_futures and the way the _test_multiprocessing suite gets run for all start methods. As exposed by passing the -o flag to python -m test to get a list of the 10 longest running tests. --------- Co-authored-by: Carl Meyer <[email protected]> Co-authored-by: Gregory P. Smith <[email protected]> [Google, LLC]
1 parent 00e2c59 commit 9e011e7

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Lib/test/libregrtest/runtest.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ def __str__(self) -> str:
143143
# set of tests that we don't want to be executed when using regrtest
144144
NOTTESTS = set()
145145

146+
#If these test directories are encountered recurse into them and treat each
147+
# test_ .py or dir as a separate test module. This can increase parallelism.
148+
# Beware this can't generally be done for any directory with sub-tests as the
149+
# __init__.py may do things which alter what tests are to be run.
150+
151+
SPLITTESTDIRS = {
152+
"test_asyncio",
153+
}
146154

147155
# Storage of uncollectable objects
148156
FOUND_GARBAGE = []
@@ -158,16 +166,21 @@ def findtestdir(path=None):
158166
return path or os.path.dirname(os.path.dirname(__file__)) or os.curdir
159167

160168

161-
def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
169+
def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS, *, split_test_dirs=SPLITTESTDIRS, base_mod=""):
162170
"""Return a list of all applicable test modules."""
163171
testdir = findtestdir(testdir)
164172
names = os.listdir(testdir)
165173
tests = []
166174
others = set(stdtests) | nottests
167175
for name in names:
168176
mod, ext = os.path.splitext(name)
169-
if mod[:5] == "test_" and ext in (".py", "") and mod not in others:
170-
tests.append(mod)
177+
if mod[:5] == "test_" and mod not in others:
178+
if mod in split_test_dirs:
179+
subdir = os.path.join(testdir, mod)
180+
mod = f"{base_mod or 'test'}.{mod}"
181+
tests.extend(findtests(subdir, [], nottests, split_test_dirs=split_test_dirs, base_mod=mod))
182+
elif ext in (".py", ""):
183+
tests.append(f"{base_mod}.{mod}" if base_mod else mod)
171184
return stdtests + sorted(tests)
172185

173186

0 commit comments

Comments
 (0)