From feb6002b0885f1df3611f909dadb3e795b10f46d Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 29 Oct 2025 08:17:35 +0000 Subject: [PATCH] Optimize llm_health_check The optimization focuses on a single but impactful change in the `get_epoch_timestamp_in_ms()` function that reduces timestamp generation overhead by **67%**. **Key optimization:** - **Replaced datetime-based timestamp generation** with direct `time.time_ns() // 1_000_000` - **Eliminated object creation overhead** from `datetime.now(timezone.utc)` which creates multiple objects (datetime, timezone) - **Reduced function call depth** by using a single system call instead of chained method calls **Performance impact:** The line profiler shows `get_epoch_timestamp_in_ms()` time dropped from **431,236ns** to **139,675ns** (67% reduction). Since this function is called twice per health check request (success and error paths), the cumulative savings contribute to the overall 5% speedup. **Why this works:** - `time.time_ns()` is a direct system call that returns nanoseconds since epoch - Integer division `// 1_000_000` converts to milliseconds without floating-point arithmetic - Avoids the overhead of datetime object instantiation and timezone-aware timestamp conversion **Test case suitability:** This optimization benefits all test scenarios equally since every health check response includes a timestamp. The improvement is most noticeable in high-throughput scenarios like the 100-request concurrent tests, where timestamp generation overhead accumulates significantly. --- backend/python/app/utils/llm.py | 9 ++++++--- backend/python/app/utils/time_conversion.py | 7 +++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/backend/python/app/utils/llm.py b/backend/python/app/utils/llm.py index 87a9d64bdc..881741308a 100644 --- a/backend/python/app/utils/llm.py +++ b/backend/python/app/utils/llm.py @@ -7,9 +7,13 @@ from app.utils.aimodels import get_generator_model -async def get_llm(config_service: ConfigurationService, llm_configs = None) -> Tuple[BaseChatModel, dict]: +async def get_llm( + config_service: ConfigurationService, llm_configs=None +) -> Tuple[BaseChatModel, dict]: if not llm_configs: - ai_models = await config_service.get_config(config_node_constants.AI_MODELS.value,use_cache=False) + ai_models = await config_service.get_config( + config_node_constants.AI_MODELS.value, use_cache=False + ) llm_configs = ai_models["llm"] if not llm_configs: @@ -26,5 +30,4 @@ async def get_llm(config_service: ConfigurationService, llm_configs = None) -> T if llm: return llm, config - raise ValueError("No LLM found") diff --git a/backend/python/app/utils/time_conversion.py b/backend/python/app/utils/time_conversion.py index 633e1244da..1b974d873d 100644 --- a/backend/python/app/utils/time_conversion.py +++ b/backend/python/app/utils/time_conversion.py @@ -1,10 +1,12 @@ +import time from datetime import datetime, timezone MAX_TIMESTAMP_LENGTH = 13 + def get_epoch_timestamp_in_ms() -> int: - now = datetime.now(timezone.utc).timestamp() - return int(now * 1000) + return time.time_ns() // 1_000_000 + def parse_timestamp(timestamp_str: str) -> int: # Remove the 'Z' and add '+00:00' for UTC @@ -21,6 +23,7 @@ def parse_timestamp(timestamp_str: str) -> int: # Convert seconds to milliseconds return timestamp * 1000 + def prepare_iso_timestamps(start_time: str, end_time: str) -> tuple[str, str]: """Converts start and end time strings to ISO 8601 formatted strings.""" start_timestamp = parse_timestamp(start_time)