From cebcdaf3f359ca81bc804fda959e75a77030e068 Mon Sep 17 00:00:00 2001 From: Abhishek jaiswal Date: Thu, 8 May 2025 04:21:55 -0400 Subject: [PATCH] Added Time KPI metrics This commit introduces a new feature for logging elapsed time for specific Key Performance Indicators (KPIs). Usage: time_kpi = TimeKPI(time.time()) code ... code ... . . . code ... time_kpi.log_elapsed_time(time.time(), "Kpi_name/function_name") Example LOG: [2025-05-08 03:41:55] KTE INFO MainThread time_kpi.log_elapsed_time 52 :: test_func_test elapsed time: 2.06 seconds Change-Id: Idefbdb2dc707b05ac5d24194b34af1dcb236e152 Signed-off-by: Abhishek jaiswal --- framework/kpi/time_kpi.py | 31 ++++++++++++++++++++++++++ framework/logging/automation_logger.py | 7 ++++++ 2 files changed, 38 insertions(+) create mode 100644 framework/kpi/time_kpi.py diff --git a/framework/kpi/time_kpi.py b/framework/kpi/time_kpi.py new file mode 100644 index 00000000..0c96aa66 --- /dev/null +++ b/framework/kpi/time_kpi.py @@ -0,0 +1,31 @@ +import time + +from framework.logging.automation_logger import get_logger + + +class TimeKPI: + """ + A class to represent a time-based KPI (Key Performance Indicator). + """ + + def __init__(self, start_time: float): + """Initialize the TimeKPI with a name and value. + + Args: + start_time (float): The time when the KPI started. + """ + self.start_time = start_time + + def log_elapsed_time(self, end_time: float, kpi_name: str) -> float: + """Calculate the elapsed time since the start time. + + Args: + end_time (float): The time when the KPI ended. + kpi_name (str): The time when the KPI ended. + + Returns: + float: The elapsed time. + """ + elapsed_time = time.time() - self.start_time + message = f"{kpi_name} elapsed time: {elapsed_time:.2f} seconds" + get_logger().log_kpi(message) diff --git a/framework/logging/automation_logger.py b/framework/logging/automation_logger.py index 15f727d6..d8e71d3d 100644 --- a/framework/logging/automation_logger.py +++ b/framework/logging/automation_logger.py @@ -97,6 +97,13 @@ class AutomationLogger(logging.getLoggerClass()): """ self._log(logging.INFO, message, None, stacklevel=2, extra={'source': 'SSH'}) + def log_kpi(self, message): + """This info-level log statement is used to log elapsed time for KPIs. + Args: + message: The message that will be logged. + """ + self._log(logging.INFO, message, None, stacklevel=2, extra={"source": "KPI"}) + def get_log_folder(self) -> str: """ Getter for log folder