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 <abhishek.jaiswal@windriver.com>
This commit is contained in:
Abhishek jaiswal 2025-05-08 04:21:55 -04:00
parent 4c777fa108
commit cebcdaf3f3
2 changed files with 38 additions and 0 deletions

31
framework/kpi/time_kpi.py Normal file
View File

@ -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)

View File

@ -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