Skip to content

Commit

Permalink
feat(CFMTech#46): Starts using stashes
Browse files Browse the repository at this point in the history
  • Loading branch information
js-dieu committed Aug 13, 2023
1 parent 9ffed40 commit bf383d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
6 changes: 6 additions & 0 deletions pytest_monitor/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dataclasses import dataclass, field


@dataclass(frozen=True)
class PyTestMonitorConfig:
enabled: bool = field(default=True, init=True)
30 changes: 15 additions & 15 deletions pytest_monitor/pytest_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import memory_profiler
import pytest

from pytest_monitor.models import PyTestMonitorConfig
from pytest_monitor.session import PyTestMonitorSession

# These dictionaries are used to compute members set on each items.
Expand All @@ -23,7 +24,7 @@
}
PYTEST_MONITOR_DEPRECATED_MARKERS = {}

PYTEST_MONITORING_ENABLED = True
config_stash = pytest.StashKey[PyTestMonitorConfig]()


def pytest_addoption(parser):
Expand Down Expand Up @@ -117,13 +118,13 @@ def pytest_configure(config):
)


def pytest_runtest_setup(item):
def pytest_runtest_setup(item: pytest.Item):
"""
Validate marker setup and print warnings if usage of deprecated marker is identified.
Setting marker attribute to the discovered item is done after the above described verification.
:param item: Test item
"""
if not PYTEST_MONITORING_ENABLED:
if not item.session.stash[config_stash].enabled:
return
item_markers = {mark.name: mark for mark in item.iter_markers() if mark and mark.name.startswith("monitor_")}
mark_to_del = []
Expand Down Expand Up @@ -158,7 +159,7 @@ def pytest_runtest_setup(item):


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
def pytest_runtest_makereport(item: pytest.Item, call: pytest.CallInfo):
"""
Used to identify the current call to add times.
:param item: Test item
Expand All @@ -172,8 +173,8 @@ def pytest_runtest_makereport(item, call):
setattr(item, "test_effective_start_time", call.start)


def pytest_runtest_call(item):
if not PYTEST_MONITORING_ENABLED:
def pytest_runtest_call(item: pytest.Item):
if not item.session.stash[config_stash].enabled:
return
setattr(item, "monitor_results", False)
if hasattr(item, "module"):
Expand All @@ -187,7 +188,7 @@ def pytest_runtest_call(item):


@pytest.hookimpl
def pytest_pyfunc_call(pyfuncitem):
def pytest_pyfunc_call(pyfuncitem: pytest.Function):
"""
Core sniffer logic. We encapsulate the test function in a sniffer function to collect
memory results.
Expand All @@ -211,7 +212,7 @@ def prof():
setattr(pyfuncitem, "mem_usage", memuse)
setattr(pyfuncitem, "monitor_results", True)

if not PYTEST_MONITORING_ENABLED:
if not pyfuncitem.session.stash[config_stash].enabled:
wrapped_function()
else:
if not pyfuncitem.session.config.option.mtr_disable_gc:
Expand All @@ -227,7 +228,7 @@ def pytest_make_parametrize_id(config, val, argname):


@pytest.hookimpl(hookwrapper=True)
def pytest_sessionstart(session):
def pytest_sessionstart(session: pytest.Session):
"""
Instantiate a monitor session to save collected metrics.
We yield at the end to let pytest pursue the execution.
Expand All @@ -251,15 +252,14 @@ def pytest_sessionstart(session):
session.pytest_monitor = PyTestMonitorSession(
db=db, remote=remote, component=component, scope=session.config.option.mtr_scope
)
global PYTEST_MONITORING_ENABLED
PYTEST_MONITORING_ENABLED = not session.config.option.mtr_none
session.stash[config_stash] = PyTestMonitorConfig(enabled=not session.config.option.mtr_none)
session.pytest_monitor.compute_info(session.config.option.mtr_description, session.config.option.mtr_tags)
yield


@pytest.fixture(autouse=True, scope="module")
def _prf_module_tracer(request):
if not PYTEST_MONITORING_ENABLED:
def _prf_module_tracer(request: pytest.FixtureRequest):
if not request.session.stash[config_stash].enabled:
yield
else:
t_a = time.time()
Expand Down Expand Up @@ -288,14 +288,14 @@ def _prf_module_tracer(request):

@pytest.fixture(autouse=True)
def _prf_tracer(request: pytest.FixtureRequest):
if not PYTEST_MONITORING_ENABLED:
if not request.session.stash[config_stash].enabled:
yield
else:
ptimes_a = request.session.pytest_monitor.process.cpu_times()
yield
ptimes_b = request.session.pytest_monitor.process.cpu_times()
if not request.node.monitor_skip_test and getattr(request.node, "monitor_results", False):
item_name = request.node.originalname or request.node.name
item_name = request.node.originalname
item_loc, *_ = request.node.location
request.session.pytest_monitor.add_test_info(
item_name,
Expand Down

0 comments on commit bf383d4

Please sign in to comment.