mirror of
https://github.com/esphome/esphome.git
synced 2026-10-14 10:12:52 +00:00
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
"""CodSpeed benchmarks for importing entity components.
|
|
|
|
Every ``esphome config`` or ``esphome compile`` imports the Python module of
|
|
each component the YAML uses. A component that imports its optional
|
|
integrations (mqtt, web_server, zigbee) at module scope drags their whole
|
|
import chains in for every user, so the cost of a fresh import is what these
|
|
benchmarks pin down.
|
|
|
|
The interpreter is shared across iterations, so each run first drops the
|
|
esphome modules from ``sys.modules`` and imports the component again from
|
|
scratch; the import machinery and the module bodies run every time, as they
|
|
do in a new process.
|
|
|
|
Skipped when ``pytest-codspeed`` isn't installed so the regular unit-test
|
|
suite keeps working unchanged.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("pytest_codspeed")
|
|
|
|
COMPONENTS = ("binary_sensor", "sensor", "logger")
|
|
|
|
|
|
def _forget_esphome_modules() -> None:
|
|
for name in [n for n in sys.modules if n == "esphome" or n.startswith("esphome.")]:
|
|
del sys.modules[name]
|
|
|
|
|
|
def _import_component(name: str) -> object:
|
|
_forget_esphome_modules()
|
|
return importlib.import_module(f"esphome.components.{name}")
|
|
|
|
|
|
@pytest.mark.parametrize("component", COMPONENTS)
|
|
def test_import_component(benchmark, component: str) -> None:
|
|
benchmark(_import_component, component)
|