| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
|
| 5 |
"""End-to-end benchmarks running astroid over real projects the way pylint |
| 6 |
does: parse, walk every node, and ``safe_infer`` the ones that matter |
| 7 |
(``Call``, ``Attribute``, ``Name``), plus a cold-start subprocess lint to |
| 8 |
catch import-time regressions the in-process benches cannot see. |
| 9 |
|
| 10 |
Projects and their pinned SHAs mirror pylint's primer corpus: |
| 11 |
https://github.com/pylint-dev/pylint/blob/main/tests/primer/packages_to_prime.json |
| 12 |
""" |
| 13 |
|
| 14 |
# Fixture-name / argument-name match is the standard pytest idiom. |
| 15 |
# pylint: disable=redefined-outer-name |
| 16 |
|
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
import subprocess |
| 20 |
import sys |
| 21 |
from pathlib import Path |
| 22 |
from typing import TYPE_CHECKING, NamedTuple |
| 23 |
|
| 24 |
import pytest |
| 25 |
|
| 26 |
from astroid import helpers, manager, nodes |
| 27 |
|
| 28 |
if TYPE_CHECKING: |
| 29 |
from pytest_codspeed import BenchmarkFixture |
| 30 |
|
| 31 |
|
| 32 |
class _Project(NamedTuple): |
| 33 |
url: str |
| 34 |
sha: str |
| 35 |
subdirs: tuple[tuple[str, ...], ...] |
| 36 |
|
| 37 |
|
| 38 |
# Mirrors tests/primer/packages_to_prime.json in pylint. |
| 39 |
_PROJECTS: dict[str, _Project] = { |
| 40 |
"flask": _Project( |
| 41 |
url="https://github.com/pallets/flask", |
| 42 |
sha="4cae5d8e411b1e69949d8fae669afeacbd3e5908", |
| 43 |
subdirs=(("src", "flask"),), |
| 44 |
), |
| 45 |
"black": _Project( |
| 46 |
url="https://github.com/psf/black", |
| 47 |
sha="acc73dc4ff97d2c1d5999914a9a182b6e2728b8a", |
| 48 |
subdirs=(("src", "black"), ("src", "blackd"), ("src", "blib2to3")), |
| 49 |
), |
| 50 |
} |
| 51 |
|
| 52 |
_INFERABLE = (nodes.Call, nodes.Attribute, nodes.Name) |
| 53 |
|
| 54 |
|
| 55 |
def _clone_primer_project( |
| 56 |
name: str, tmp_path_factory: pytest.TempPathFactory |
| 57 |
) -> list[Path]: |
| 58 |
"""Sparse-clone a primer project at its pinned SHA, return its ``.py`` files. |
| 59 |
|
| 60 |
``--filter=blob:none`` skips fetching file contents until they are |
| 61 |
needed; ``sparse-checkout`` restricts that fetch to the project's |
| 62 |
declared source subdirs (essential for huge repos like pandas). |
| 63 |
""" |
| 64 |
project = _PROJECTS[name] |
| 65 |
clone_root = tmp_path_factory.mktemp(f"{name}-bench") |
| 66 |
subprocess.run( |
| 67 |
[ |
| 68 |
"git", |
| 69 |
"clone", |
| 70 |
"--no-checkout", |
| 71 |
"--filter=blob:none", |
| 72 |
"--no-tags", |
| 73 |
"--single-branch", |
| 74 |
project.url, |
| 75 |
str(clone_root), |
| 76 |
], |
| 77 |
check=True, |
| 78 |
capture_output=True, |
| 79 |
) |
| 80 |
subprocess.run( |
| 81 |
[ |
| 82 |
"git", |
| 83 |
"-C", |
| 84 |
str(clone_root), |
| 85 |
"sparse-checkout", |
| 86 |
"set", |
| 87 |
*("/".join(parts) for parts in project.subdirs), |
| 88 |
], |
| 89 |
check=True, |
| 90 |
capture_output=True, |
| 91 |
) |
| 92 |
subprocess.run( |
| 93 |
["git", "-C", str(clone_root), "checkout", project.sha], |
| 94 |
check=True, |
| 95 |
capture_output=True, |
| 96 |
) |
| 97 |
files: list[Path] = [] |
| 98 |
for subdir in project.subdirs: |
| 99 |
files.extend(clone_root.joinpath(*subdir).rglob("*.py")) |
| 100 |
return sorted(files) |
| 101 |
|
| 102 |
|
| 103 |
@pytest.fixture(scope="session") |
| 104 |
def flask_files(tmp_path_factory: pytest.TempPathFactory) -> list[Path]: |
| 105 |
return _clone_primer_project("flask", tmp_path_factory) |
| 106 |
|
| 107 |
|
| 108 |
@pytest.fixture(scope="session") |
| 109 |
def black_files(tmp_path_factory: pytest.TempPathFactory) -> list[Path]: |
| 110 |
return _clone_primer_project("black", tmp_path_factory) |
| 111 |
|
| 112 |
|
| 113 |
def _parse_all(files: list[Path], mgr: manager.AstroidManager) -> int: |
| 114 |
"""Cold-cache parse of every file (rebuilder hot path).""" |
| 115 |
mgr.clear_cache() |
| 116 |
for f in files: |
| 117 |
mgr.ast_from_file(str(f)) |
| 118 |
return len(files) |
| 119 |
|
| 120 |
|
| 121 |
def _walk_and_infer(files: list[Path], mgr: manager.AstroidManager) -> int: |
| 122 |
"""Pylint-shaped traversal: parse, walk, safe_infer call sites.""" |
| 123 |
mgr.clear_cache() |
| 124 |
count = 0 |
| 125 |
for f in files: |
| 126 |
mod = mgr.ast_from_file(str(f)) |
| 127 |
for node in mod.nodes_of_class(nodes.NodeNG): |
| 128 |
if isinstance(node, _INFERABLE): |
| 129 |
helpers.safe_infer(node) |
| 130 |
count += 1 |
| 131 |
return count |
| 132 |
|
| 133 |
|
| 134 |
_MINIMAL_LINT_TARGET = '''\ |
| 135 |
"""Minimal module for the cold-start benchmark. |
| 136 |
|
| 137 |
Intentionally tiny but exercises brain_builtin_inference (always-on, |
| 138 |
loaded by both the eager and lazy registration paths) without |
| 139 |
triggering any optional brain plugin: no stdlib imports, no |
| 140 |
dataclasses, no typing, no enum. That keeps cold-start cost dominated |
| 141 |
by Python + pylint + astroid startup, which is the point. |
| 142 |
""" |
| 143 |
|
| 144 |
|
| 145 |
def add(a, b): |
| 146 |
return a + b |
| 147 |
|
| 148 |
|
| 149 |
result = add(len("ab"), 2) |
| 150 |
''' |
| 151 |
|
| 152 |
|
| 153 |
@pytest.fixture(scope="session") |
| 154 |
def minimal_module(tmp_path_factory: pytest.TempPathFactory) -> Path: |
| 155 |
"""Tiny Python file used as the lint target for cold-start runs.""" |
| 156 |
path = tmp_path_factory.mktemp("lint-target") / "minimal.py" |
| 157 |
path.write_text(_MINIMAL_LINT_TARGET, encoding="utf-8") |
| 158 |
return path |
| 159 |
|
| 160 |
|
| 161 |
def _pylint_one_file(target: Path) -> None: |
| 162 |
"""Run ``python -m pylint <target>`` in a fresh subprocess. |
| 163 |
|
| 164 |
pylint exits non-zero whenever it emits any message (warnings, |
| 165 |
refactor hints, etc.), which is expected on most files. We don't |
| 166 |
care about the exit code here, only the wall time. |
| 167 |
""" |
| 168 |
subprocess.run( |
| 169 |
[sys.executable, "-m", "pylint", str(target)], |
| 170 |
check=False, |
| 171 |
capture_output=True, |
| 172 |
) |
| 173 |
|
| 174 |
|
| 175 |
def test_bench_endtoend_cold_lint( |
| 176 |
benchmark: BenchmarkFixture, minimal_module: Path |
| 177 |
) -> None: |
| 178 |
benchmark(_pylint_one_file, minimal_module) |
| 179 |
|
| 180 |
|
| 181 |
def test_bench_endtoend_parse_flask( |
| 182 |
benchmark: BenchmarkFixture, flask_files: list[Path] |
| 183 |
) -> None: |
| 184 |
assert flask_files, "Expected at least one .py file in flask" |
| 185 |
mgr = manager.AstroidManager() |
| 186 |
benchmark(_parse_all, flask_files, mgr) |
| 187 |
|
| 188 |
|
| 189 |
def test_bench_endtoend_walk_infer_flask( |
| 190 |
benchmark: BenchmarkFixture, flask_files: list[Path] |
| 191 |
) -> None: |
| 192 |
assert flask_files, "Expected at least one .py file in flask" |
| 193 |
mgr = manager.AstroidManager() |
| 194 |
benchmark(_walk_and_infer, flask_files, mgr) |
| 195 |
|
| 196 |
|
| 197 |
def test_bench_endtoend_walk_infer_black( |
| 198 |
benchmark: BenchmarkFixture, black_files: list[Path] |
| 199 |
) -> None: |
| 200 |
assert black_files, "Expected at least one .py file in black" |
| 201 |
mgr = manager.AstroidManager() |
| 202 |
benchmark(_walk_and_infer, black_files, mgr) |