mirror of
https://github.com/elisspace/core.git
synced 2026-10-01 06:49:55 +00:00
Add object source logger to profiler (#90650)
* Add object source logger to profiler * fixes * cleanup * tweaks * logging * logging * too intensive * adjust * Update homeassistant/bootstrap.py * fixes * fixes * coverage
This commit is contained in:
@@ -19,7 +19,9 @@ from homeassistant.components.profiler import (
|
||||
SERVICE_LRU_STATS,
|
||||
SERVICE_MEMORY,
|
||||
SERVICE_START,
|
||||
SERVICE_START_LOG_OBJECT_SOURCES,
|
||||
SERVICE_START_LOG_OBJECTS,
|
||||
SERVICE_STOP_LOG_OBJECT_SOURCES,
|
||||
SERVICE_STOP_LOG_OBJECTS,
|
||||
)
|
||||
from homeassistant.components.profiler.const import DOMAIN
|
||||
@@ -130,13 +132,20 @@ async def test_object_growth_logging(
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_START_LOG_OBJECTS, {CONF_SCAN_INTERVAL: 10}, blocking=True
|
||||
)
|
||||
with pytest.raises(HomeAssistantError, match="Object logging already started"):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_START_LOG_OBJECTS,
|
||||
{CONF_SCAN_INTERVAL: 10},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert "Growth" in caplog.text
|
||||
caplog.clear()
|
||||
assert "Growth" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=11))
|
||||
await hass.async_block_till_done()
|
||||
assert "Growth" in caplog.text
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=11))
|
||||
await hass.async_block_till_done()
|
||||
assert "Growth" in caplog.text
|
||||
|
||||
await hass.services.async_call(DOMAIN, SERVICE_STOP_LOG_OBJECTS, {}, blocking=True)
|
||||
caplog.clear()
|
||||
@@ -145,6 +154,17 @@ async def test_object_growth_logging(
|
||||
await hass.async_block_till_done()
|
||||
assert "Growth" not in caplog.text
|
||||
|
||||
with pytest.raises(HomeAssistantError, match="Object logging not running"):
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_STOP_LOG_OBJECTS, {}, blocking=True
|
||||
)
|
||||
|
||||
with patch("objgraph.growth"):
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_START_LOG_OBJECTS, {CONF_SCAN_INTERVAL: 10}, blocking=True
|
||||
)
|
||||
caplog.clear()
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -276,3 +296,92 @@ async def test_lru_stats(hass: HomeAssistant, caplog: pytest.LogCaptureFixture)
|
||||
assert "_dummy_test_lru_stats" in caplog.text
|
||||
assert "CacheInfo" in caplog.text
|
||||
assert "sqlalchemy_test" in caplog.text
|
||||
|
||||
|
||||
async def test_log_object_sources(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test we can setup and the service and we can dump objects to the log."""
|
||||
|
||||
entry = MockConfigEntry(domain=DOMAIN)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.services.has_service(DOMAIN, SERVICE_START_LOG_OBJECT_SOURCES)
|
||||
assert hass.services.has_service(DOMAIN, SERVICE_STOP_LOG_OBJECT_SOURCES)
|
||||
|
||||
class FakeObject:
|
||||
"""Fake object."""
|
||||
|
||||
def __repr__(self):
|
||||
"""Return a fake repr.""."""
|
||||
return "<FakeObject>"
|
||||
|
||||
fake_object = FakeObject()
|
||||
|
||||
with patch("gc.collect"), patch("gc.get_objects", return_value=[fake_object]):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_START_LOG_OBJECT_SOURCES,
|
||||
{CONF_SCAN_INTERVAL: 10},
|
||||
blocking=True,
|
||||
)
|
||||
with pytest.raises(HomeAssistantError, match="Object logging already started"):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_START_LOG_OBJECT_SOURCES,
|
||||
{CONF_SCAN_INTERVAL: 10},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert "New object FakeObject (0/1)" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=11))
|
||||
await hass.async_block_till_done()
|
||||
assert "No new object growth found" in caplog.text
|
||||
|
||||
fake_object2 = FakeObject()
|
||||
|
||||
with patch("gc.collect"), patch(
|
||||
"gc.get_objects", return_value=[fake_object, fake_object2]
|
||||
):
|
||||
caplog.clear()
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=21))
|
||||
await hass.async_block_till_done()
|
||||
assert "New object FakeObject (1/2)" in caplog.text
|
||||
|
||||
many_objects = [FakeObject() for _ in range(30)]
|
||||
with patch("gc.collect"), patch("gc.get_objects", return_value=many_objects):
|
||||
caplog.clear()
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=31))
|
||||
await hass.async_block_till_done()
|
||||
assert "New object FakeObject (2/30)" in caplog.text
|
||||
assert "New objects overflowed by {'FakeObject': 25}" in caplog.text
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_STOP_LOG_OBJECT_SOURCES, {}, blocking=True
|
||||
)
|
||||
caplog.clear()
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=41))
|
||||
await hass.async_block_till_done()
|
||||
assert "FakeObject" not in caplog.text
|
||||
assert "No new object growth found" not in caplog.text
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=51))
|
||||
await hass.async_block_till_done()
|
||||
assert "FakeObject" not in caplog.text
|
||||
assert "No new object growth found" not in caplog.text
|
||||
|
||||
with pytest.raises(HomeAssistantError, match="Object logging not running"):
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_STOP_LOG_OBJECT_SOURCES, {}, blocking=True
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user