diff --git a/tests/files/performance/import_time_linux.json b/tests/files/performance/import_time_linux.json new file mode 100644 index 00000000000..3492c93a840 --- /dev/null +++ b/tests/files/performance/import_time_linux.json @@ -0,0 +1,19 @@ +{ + "from pymatgen.core.bonds import CovalentBond": 289.5851116666108, + "from pymatgen.core.composition import Composition": 292.8479909999548, + "from pymatgen.core.interface import Interface": 969.5693099999593, + "from pymatgen.core.ion import Ion": 291.07530133334575, + "from pymatgen.core.lattice import Lattice": 288.8340153333881, + "from pymatgen.core.libxcfunc import LibxcFunc": 293.4184753333587, + "from pymatgen.core.molecular_orbitals import MolecularOrbitals": 294.19796566658835, + "from pymatgen.core.operations import SymmOp": 296.4627546666634, + "from pymatgen.core.periodic_table import Element": 295.95872066662804, + "from pymatgen.core.sites import Site": 292.66485499999817, + "from pymatgen.core.spectrum import Spectrum": 486.72776566669046, + "from pymatgen.core.structure import Structure": 291.01618733333606, + "from pymatgen.core.surface import Slab": 301.90875833329756, + "from pymatgen.core.tensors import Tensor": 304.27744800003137, + "from pymatgen.core.trajectory import Trajectory": 300.45536066666045, + "from pymatgen.core.units import Unit": 305.4779056666348, + "from pymatgen.core.xcfunc import XcFunc": 309.1085626666275 +} \ No newline at end of file diff --git a/tests/files/performance/import_time_macos.json b/tests/files/performance/import_time_macos.json new file mode 100644 index 00000000000..9ac1e63fc9d --- /dev/null +++ b/tests/files/performance/import_time_macos.json @@ -0,0 +1,19 @@ +{ + "from pymatgen.core.bonds import CovalentBond": 321.8502360000457, + "from pymatgen.core.composition import Composition": 292.35445800009074, + "from pymatgen.core.interface import Interface": 855.005861000033, + "from pymatgen.core.ion import Ion": 240.8930970000256, + "from pymatgen.core.lattice import Lattice": 329.09868066659936, + "from pymatgen.core.libxcfunc import LibxcFunc": 306.6966386666839, + "from pymatgen.core.molecular_orbitals import MolecularOrbitals": 281.78087466661356, + "from pymatgen.core.operations import SymmOp": 299.9741943333447, + "from pymatgen.core.periodic_table import Element": 293.6565829999533, + "from pymatgen.core.sites import Site": 280.3443330000543, + "from pymatgen.core.spectrum import Spectrum": 459.20266666666976, + "from pymatgen.core.structure import Structure": 265.4675833332476, + "from pymatgen.core.surface import Slab": 306.0919996667053, + "from pymatgen.core.tensors import Tensor": 310.54281933325, + "from pymatgen.core.trajectory import Trajectory": 335.25658333329983, + "from pymatgen.core.units import Unit": 294.03472200003006, + "from pymatgen.core.xcfunc import XcFunc": 309.3993196666058 +} \ No newline at end of file diff --git a/tests/files/performance/import_time_windows.json b/tests/files/performance/import_time_windows.json new file mode 100644 index 00000000000..270b73a9d65 --- /dev/null +++ b/tests/files/performance/import_time_windows.json @@ -0,0 +1,19 @@ +{ + "from pymatgen.core.bonds import CovalentBond": 443.7560000000455, + "from pymatgen.core.composition import Composition": 441.06553333335796, + "from pymatgen.core.interface import Interface": 1828.751033333295, + "from pymatgen.core.ion import Ion": 443.58053333333675, + "from pymatgen.core.lattice import Lattice": 445.729999999988, + "from pymatgen.core.libxcfunc import LibxcFunc": 459.24773333338936, + "from pymatgen.core.molecular_orbitals import MolecularOrbitals": 440.4825999999957, + "from pymatgen.core.operations import SymmOp": 440.62226666665083, + "from pymatgen.core.periodic_table import Element": 441.64050000002436, + "from pymatgen.core.sites import Site": 442.1802333333744, + "from pymatgen.core.spectrum import Spectrum": 737.3025000000174, + "from pymatgen.core.structure import Structure": 445.0546333332568, + "from pymatgen.core.surface import Slab": 463.0683333333157, + "from pymatgen.core.tensors import Tensor": 463.5761666666743, + "from pymatgen.core.trajectory import Trajectory": 443.9995333333779, + "from pymatgen.core.units import Unit": 446.352766666602, + "from pymatgen.core.xcfunc import XcFunc": 469.42599999996065 +} \ No newline at end of file diff --git a/tests/performance/test_import_time.py b/tests/performance/test_import_time.py index 49adb49cddd..9b52ee51c34 100644 --- a/tests/performance/test_import_time.py +++ b/tests/performance/test_import_time.py @@ -1,7 +1,9 @@ """ Test the import time of several important modules. + +NOTE: + - Toggle the "GEN_REF_TIME" to generate reference import time. """ -# ruff: noqa: T201 (check for print statement) from __future__ import annotations @@ -9,17 +11,20 @@ import os import subprocess import time +import warnings from typing import TYPE_CHECKING import pytest +from pymatgen.util.testing import TEST_FILES_DIR + if TYPE_CHECKING: from typing import Literal -# Toggle this to generate reference import times -GEN_REF_TIME = True +# NOTE: Toggle this to generate reference import time +GEN_REF_TIME: bool = False -MODULES_TO_TEST = ( +MODULES_TO_TEST: tuple[str, ...] = ( "from pymatgen.core.bonds import CovalentBond", "from pymatgen.core.composition import Composition", "from pymatgen.core.interface import Interface", @@ -40,9 +45,10 @@ ) # Get runner OS and reference file -RUNNER_OS: Literal["linux", "windows", "macos"] = os.getenv("RUNNER_OS", "").lower() # type: ignore[assignment] +RUNNER_OS: Literal["linux", "windows", "macos"] = os.getenv("RUNNER_OS", "macos").lower() # type: ignore[assignment] assert RUNNER_OS in {"linux", "windows", "macos"} -REF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), f"import_time_{RUNNER_OS}.json") + +REF_FILE: str = f"{TEST_FILES_DIR}/performance/import_time_{RUNNER_OS}.json" @pytest.mark.skipif(not GEN_REF_TIME, reason="Set GEN_REF_TIME to generate reference import time.") @@ -69,11 +75,9 @@ def test_import_time(grace_percent: float = 0.20, hard_percent: float = 0.50) -> hard_percent (float): Maximum allowed percentage increase in import time before the test fails. """ - try: - with open(REF_FILE, encoding="utf-8") as file: - ref_import_times = json.load(file) - except FileNotFoundError: - pytest.fail(f"Reference file {REF_FILE} not found. Please generate it.") + + with open(REF_FILE, encoding="utf-8") as file: + ref_import_times = json.load(file) for module_import_cmd, ref_time in ref_import_times.items(): current_time = _measure_import_time_in_ms(module_import_cmd) @@ -86,9 +90,9 @@ def test_import_time(grace_percent: float = 0.20, hard_percent: float = 0.50) -> if current_time > hard_threshold: pytest.fail(f"{module_import_cmd} import too slow! {hard_threshold=:.2f} ms") else: - pytest.warns( - UserWarning, + warnings.warn( f"{module_import_cmd} import slightly slower than reference: {grace_threshold=:.2f} ms", + stacklevel=2, )