Add 'logical' CPU count choice for --numprocesses
This can significantly speed up runs when testing is not CPU-bound.
This commit is contained in:
3
changelog/646.feature.rst
Normal file
3
changelog/646.feature.rst
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Add ``--numprocesses=logical`` flag, which automatically uses the number of logical CPUs available, instead of physical CPUs with ``auto``.
|
||||||
|
|
||||||
|
This is very useful for test suites which are not CPU-bound.
|
||||||
@@ -5,13 +5,14 @@ import py
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def pytest_xdist_auto_num_workers():
|
def pytest_xdist_auto_num_workers(config):
|
||||||
try:
|
try:
|
||||||
import psutil
|
import psutil
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
count = psutil.cpu_count(logical=False) or psutil.cpu_count()
|
use_logical = config.option.numprocesses == "logical"
|
||||||
|
count = psutil.cpu_count(logical=use_logical) or psutil.cpu_count()
|
||||||
if count:
|
if count:
|
||||||
return count
|
return count
|
||||||
try:
|
try:
|
||||||
@@ -36,8 +37,8 @@ def pytest_xdist_auto_num_workers():
|
|||||||
|
|
||||||
|
|
||||||
def parse_numprocesses(s):
|
def parse_numprocesses(s):
|
||||||
if s == "auto":
|
if s == "auto" or s == "logical":
|
||||||
return "auto"
|
return s
|
||||||
elif s is not None:
|
elif s is not None:
|
||||||
return int(s)
|
return int(s)
|
||||||
|
|
||||||
@@ -51,9 +52,10 @@ def pytest_addoption(parser):
|
|||||||
metavar="numprocesses",
|
metavar="numprocesses",
|
||||||
action="store",
|
action="store",
|
||||||
type=parse_numprocesses,
|
type=parse_numprocesses,
|
||||||
help="shortcut for '--dist=load --tx=NUM*popen', "
|
help="Shortcut for '--dist=load --tx=NUM*popen'. With 'auto', attempt "
|
||||||
"you can use 'auto' here for auto detection CPUs number on "
|
"to detect physical CPU count. With 'logical', detect logical CPU "
|
||||||
"host system and it will be 0 when used with --pdb",
|
"count. If physical CPU count cannot be found, falls back to logical "
|
||||||
|
"count. This will be 0 when used with --pdb.",
|
||||||
)
|
)
|
||||||
group.addoption(
|
group.addoption(
|
||||||
"--maxprocesses",
|
"--maxprocesses",
|
||||||
@@ -190,7 +192,7 @@ def pytest_configure(config):
|
|||||||
@pytest.mark.tryfirst
|
@pytest.mark.tryfirst
|
||||||
def pytest_cmdline_main(config):
|
def pytest_cmdline_main(config):
|
||||||
usepdb = config.getoption("usepdb", False) # a core option
|
usepdb = config.getoption("usepdb", False) # a core option
|
||||||
if config.option.numprocesses == "auto":
|
if config.option.numprocesses == "auto" or config.option.numprocesses == "logical":
|
||||||
if usepdb:
|
if usepdb:
|
||||||
config.option.numprocesses = 0
|
config.option.numprocesses = 0
|
||||||
config.option.dist = "no"
|
config.option.dist = "no"
|
||||||
|
|||||||
@@ -69,6 +69,12 @@ def test_auto_detect_cpus(testdir, monkeypatch):
|
|||||||
assert config.getoption("numprocesses") == 0
|
assert config.getoption("numprocesses") == 0
|
||||||
assert config.getoption("dist") == "no"
|
assert config.getoption("dist") == "no"
|
||||||
|
|
||||||
|
config = testdir.parseconfigure("-nlogical", "--pdb")
|
||||||
|
check_options(config)
|
||||||
|
assert config.getoption("usepdb")
|
||||||
|
assert config.getoption("numprocesses") == 0
|
||||||
|
assert config.getoption("dist") == "no"
|
||||||
|
|
||||||
monkeypatch.delattr(os, "sched_getaffinity", raising=False)
|
monkeypatch.delattr(os, "sched_getaffinity", raising=False)
|
||||||
monkeypatch.setenv("TRAVIS", "true")
|
monkeypatch.setenv("TRAVIS", "true")
|
||||||
config = testdir.parseconfigure("-nauto")
|
config = testdir.parseconfigure("-nauto")
|
||||||
@@ -81,12 +87,16 @@ def test_auto_detect_cpus_psutil(testdir, monkeypatch):
|
|||||||
|
|
||||||
psutil = pytest.importorskip("psutil")
|
psutil = pytest.importorskip("psutil")
|
||||||
|
|
||||||
monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: 42)
|
monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: 84 if logical else 42)
|
||||||
|
|
||||||
config = testdir.parseconfigure("-nauto")
|
config = testdir.parseconfigure("-nauto")
|
||||||
check_options(config)
|
check_options(config)
|
||||||
assert config.getoption("numprocesses") == 42
|
assert config.getoption("numprocesses") == 42
|
||||||
|
|
||||||
|
config = testdir.parseconfigure("-nlogical")
|
||||||
|
check_options(config)
|
||||||
|
assert config.getoption("numprocesses") == 84
|
||||||
|
|
||||||
|
|
||||||
def test_hook_auto_num_workers(testdir, monkeypatch):
|
def test_hook_auto_num_workers(testdir, monkeypatch):
|
||||||
from xdist.plugin import pytest_cmdline_main as check_options
|
from xdist.plugin import pytest_cmdline_main as check_options
|
||||||
@@ -101,6 +111,10 @@ def test_hook_auto_num_workers(testdir, monkeypatch):
|
|||||||
check_options(config)
|
check_options(config)
|
||||||
assert config.getoption("numprocesses") == 42
|
assert config.getoption("numprocesses") == 42
|
||||||
|
|
||||||
|
config = testdir.parseconfigure("-nlogical")
|
||||||
|
check_options(config)
|
||||||
|
assert config.getoption("numprocesses") == 42
|
||||||
|
|
||||||
|
|
||||||
def test_boxed_with_collect_only(testdir):
|
def test_boxed_with_collect_only(testdir):
|
||||||
from xdist.plugin import pytest_cmdline_main as check_options
|
from xdist.plugin import pytest_cmdline_main as check_options
|
||||||
|
|||||||
Reference in New Issue
Block a user