"""Runtime profiling helpers."""

from __future__ import annotations

import logging
from time import perf_counter

from bot.core.metrics import incr


logger = logging.getLogger("bobi.profiler")


async def profile_async(name: str, coro):
    started = perf_counter()

    try:
        return await coro
    finally:
        elapsed = (perf_counter() - started) * 1000

        incr(f"profile.calls.{name}")

        if elapsed >= 250:
            logger.warning("Slow operation: %s took %.2f ms", name, elapsed)
