From a944a34e63c86ff172e874185df4cfa9e4e9e13b Mon Sep 17 00:00:00 2001
From: Masen Furer <m_github@0x26.net>
Date: Thu, 6 Feb 2025 11:15:39 -0800
Subject: [PATCH] add console.timing context

Log debug messages with timing for different processes.
---
 reflex/utils/console.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/reflex/utils/console.py b/reflex/utils/console.py
index d5b7a0d6e..5c47eee6f 100644
--- a/reflex/utils/console.py
+++ b/reflex/utils/console.py
@@ -2,8 +2,10 @@
 
 from __future__ import annotations
 
+import contextlib
 import inspect
 import shutil
+import time
 from pathlib import Path
 from types import FrameType
 
@@ -317,3 +319,20 @@ def status(*args, **kwargs):
         A new status.
     """
     return _console.status(*args, **kwargs)
+
+
+@contextlib.contextmanager
+def timing(msg: str):
+    """Create a context manager to time a block of code.
+
+    Args:
+        msg: The message to display.
+
+    Yields:
+        None.
+    """
+    start = time.time()
+    try:
+        yield
+    finally:
+        debug(f"[white]\\[timing] {msg}: {time.time() - start:.2f}s[/white]")