Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease event loop latency by binding time.monotonic to loop.time directly #98288

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions homeassistant/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import subprocess
import threading
from time import monotonic
import traceback
from typing import Any

Expand Down Expand Up @@ -114,6 +115,10 @@ def new_event_loop(self) -> asyncio.AbstractEventLoop:
loop.set_default_executor = warn_use( # type: ignore[method-assign]
loop.set_default_executor, "sets default executor on the event loop"
)
# bind the built-in time.monotonic directly as loop.time to avoid the
# overhead of the additional method call since its the most called loop
# method and its roughly 10%+ of all the call time in base_events.py
loop.time = monotonic # type: ignore[method-assign]
return loop


Expand Down
5 changes: 4 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ def async_fire_time_changed(
_async_fire_time_changed(hass, utc_datetime, fire_all)


_MONOTONIC_RESOLUTION = time.get_clock_info("monotonic").resolution


@callback
def _async_fire_time_changed(
hass: HomeAssistant, utc_datetime: datetime | None, fire_all: bool
Expand All @@ -432,7 +435,7 @@ def _async_fire_time_changed(
continue

mock_seconds_into_future = timestamp - time.time()
future_seconds = task.when() - hass.loop.time()
future_seconds = task.when() - (hass.loop.time() + _MONOTONIC_RESOLUTION)

if fire_all or mock_seconds_into_future >= future_seconds:
with patch(
Expand Down
7 changes: 5 additions & 2 deletions tests/components/statistics/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from unittest.mock import patch

from freezegun import freeze_time
import pytest

from homeassistant import config as hass_config
from homeassistant.components.recorder import Recorder
Expand Down Expand Up @@ -1286,12 +1287,14 @@ async def test_initialize_from_database(
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS


@pytest.mark.freeze_time(
bdraco marked this conversation as resolved.
Show resolved Hide resolved
datetime(dt_util.utcnow().year + 1, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC)
)
async def test_initialize_from_database_with_maxage(
recorder_mock: Recorder, hass: HomeAssistant
) -> None:
"""Test initializing the statistics from the database."""
now = dt_util.utcnow()
current_time = datetime(now.year + 1, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC)
current_time = dt_util.utcnow()

# Testing correct retrieval from recorder, thus we do not
# want purging to occur within the class itself.
Expand Down
9 changes: 8 additions & 1 deletion tests/patch_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from __future__ import annotations

import datetime
import time

from homeassistant import util
from homeassistant import runner, util
from homeassistant.util import dt as dt_util


Expand All @@ -12,5 +13,11 @@ def _utcnow() -> datetime.datetime:
return datetime.datetime.now(datetime.UTC)


def _monotonic() -> float:
"""Make monotonic patchable by freezegun."""
return time.monotonic()


dt_util.utcnow = _utcnow # type: ignore[assignment]
util.utcnow = _utcnow # type: ignore[assignment]
runner.monotonic = _monotonic # type: ignore[assignment]