Skip to content

Commit 9cc7ef0

Browse files
xlyyddyionelmc
authored andcommitted
Defer xdist warning until benchmark collection (#65)
1 parent ef48550 commit 9cc7ef0

5 files changed

Lines changed: 66 additions & 22 deletions

File tree

‎AUTHORS.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ Authors
4444
* Thomas B. Brunner - https://github.com/thomasbbrunner
4545
* Hugo van Kemenade - https://github.com/hugovk
4646
* Aarni Koskela - https://github.com/akx
47+
* xlyyddy - https://github.com/xlyyddy

‎CHANGELOG.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Unreleased
77

88
* Added ``--benchmark-precision`` and ``--benchmark-confidence``:
99
instead of a fixed number of rounds, stop once the mean's relative margin of error falls below the given fraction.
10+
* Defer the xdist auto-disable warning until a benchmark fixture is collected.
11+
Contributed by xlyyddy for `#65 <https://github.com/ionelmc/pytest-benchmark/issues/65>`_.
1012

1113
v5.2.3 (2025-11-09)
1214
--------------------

‎src/pytest_benchmark/plugin.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ def pytest_collection_modifyitems(config, items):
357357
for item in items:
358358
has_benchmark = hasattr(item, 'fixturenames') and 'benchmark' in item.fixturenames
359359
if has_benchmark:
360+
bs.note_xdist_benchmark()
360361
if bs.skip:
361362
item.add_marker(skip_bench)
362363
else:

‎src/pytest_benchmark/session.py‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,11 @@ def __init__(self, config):
7373
self.disabled = config.getoption('benchmark_disable') and not config.getoption('benchmark_enable')
7474

7575
# Only the main process has the 'dist' field in the config.
76-
xdist_worker = os.environ.get('PYTEST_XDIST_WORKER')
77-
xdist_active = config.getoption('dist', 'no') != 'no' or xdist_worker
78-
if xdist_active and not self.skip and not self.disabled:
79-
if not xdist_worker:
80-
self.logger.warning(
81-
'Benchmarks are automatically disabled because xdist plugin is active. '
82-
'Benchmarks cannot be performed reliably in a parallelized environment.',
83-
)
76+
self.xdist_worker = os.environ.get('PYTEST_XDIST_WORKER')
77+
xdist_active = config.getoption('dist', 'no') != 'no' or self.xdist_worker
78+
self.xdist_warning = bool(xdist_active and not self.skip and not self.disabled)
79+
self.xdist_benchmark_found = False
80+
if self.xdist_warning:
8481
self.disabled = True
8582
if hasattr(config, 'slaveinput'):
8683
self.disabled = True
@@ -110,6 +107,18 @@ def __init__(self, config):
110107
self.name_format = NAME_FORMATTERS[config.getoption('benchmark_name')]
111108
self.histogram = first_or_value(config.getoption('benchmark_histogram'), False)
112109

110+
def note_xdist_benchmark(self):
111+
if self.xdist_warning and self.xdist_worker in (None, 'gw0'):
112+
self.xdist_benchmark_found = True
113+
114+
def pytest_unconfigure(self):
115+
# Worker collection captures warnings into pytest's summary, so defer to preserve Logger's stderr output.
116+
if self.xdist_benchmark_found:
117+
self.logger.warning(
118+
'Benchmarks are automatically disabled because xdist plugin is active. '
119+
'Benchmarks cannot be performed reliably in a parallelized environment.',
120+
)
121+
113122
def get_machine_info(self):
114123
obj = self.config.hook.pytest_benchmark_generate_machine_info(config=self.config)
115124
self.config.hook.pytest_benchmark_update_machine_info(config=self.config, machine_info=obj)

‎tests/test_benchmark.py‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -898,30 +898,61 @@ def test_bogus_sort(testdir):
898898
)
899899

900900

901+
XDIST_WARNING = (
902+
'Benchmarks are automatically disabled because xdist plugin is active. '
903+
'Benchmarks cannot be performed reliably in a parallelized environment.'
904+
)
905+
906+
901907
def test_xdist(testdir):
902908
pytest.importorskip('xdist')
903909
test = testdir.makepyfile(SIMPLE_TEST)
904-
result = testdir.runpytest_subprocess('--doctest-modules', '-n', '1', '-rw', test)
905-
result.stderr.fnmatch_lines(
906-
[
907-
'* Benchmarks are automatically disabled because xdist plugin is active. Benchmarks cannot be '
908-
'performed reliably in a parallelized environment.',
909-
]
910-
)
910+
result = testdir.runpytest_subprocess('--doctest-modules', '-n', '2', '-rw', test)
911+
result.stderr.fnmatch_lines([f'* {XDIST_WARNING}'])
912+
assert result.stderr.str().count(f'PytestBenchmarkWarning: {XDIST_WARNING}') == 1
911913

912914

913915
def test_xdist_verbose(testdir):
914916
pytest.importorskip('xdist')
915917
test = testdir.makepyfile(SIMPLE_TEST)
916918
result = testdir.runpytest_subprocess('--doctest-modules', '-n', '1', '--benchmark-verbose', test)
917-
result.stderr.fnmatch_lines(
918-
[
919-
'------*',
920-
' WARNING: Benchmarks are automatically disabled because xdist plugin is active. Benchmarks cannot be performed '
921-
'reliably in a parallelized environment.',
922-
'------*',
923-
]
919+
result.stderr.fnmatch_lines(['------*', f' WARNING: {XDIST_WARNING}', '------*'])
920+
assert result.stderr.str().count(f' WARNING: {XDIST_WARNING}') == 1
921+
922+
923+
def test_xdist_without_benchmark(testdir):
924+
pytest.importorskip('xdist')
925+
test = testdir.makepyfile(
926+
"""
927+
def test_regular():
928+
pass
929+
"""
930+
)
931+
result = testdir.runpytest_subprocess('-n', '1', test)
932+
result.assert_outcomes(passed=1)
933+
assert XDIST_WARNING not in result.stderr.str()
934+
assert XDIST_WARNING not in result.stdout.str()
935+
936+
937+
@pytest.mark.parametrize(
938+
('option', 'outcomes'),
939+
[
940+
('--benchmark-disable', {'passed': 1}),
941+
('--benchmark-skip', {'skipped': 1}),
942+
],
943+
)
944+
def test_xdist_explicit_benchmark_option_does_not_warn(testdir, option, outcomes):
945+
pytest.importorskip('xdist')
946+
test = testdir.makepyfile(
947+
"""
948+
def test_bench(benchmark):
949+
benchmark(lambda: None)
950+
"""
924951
)
952+
result = testdir.runpytest_subprocess('-n', '1', option, test)
953+
result.assert_outcomes(**outcomes)
954+
assert XDIST_WARNING not in result.stderr.str()
955+
assert XDIST_WARNING not in result.stdout.str()
925956

926957

927958
def test_cprofile(testdir):

0 commit comments

Comments
 (0)