diff --git a/cpu_governor_auto_adjust/trigger.py b/cpu_governor_auto_adjust/trigger.py index 407fdb1..7f0efcf 100644 --- a/cpu_governor_auto_adjust/trigger.py +++ b/cpu_governor_auto_adjust/trigger.py @@ -14,13 +14,22 @@ class Trigger(AppClass): self._active: bool = False self.log.setLevel(self.config.loglevel.upper()) self._timestamp_last_statistics = monotonic() + self._total_consumed_time = 0 + self.runs = 0 def __hash__(self): return super().__hash__(hash(self.name)) + @property + def average_consumed_time(self) -> str: + if self.runs == 0: + return 0 + return f"{(self._total_consumed_time / self.runs) * 1_000_000:.3f} µs" + def log_statistics(self): class_vars = {} + start = monotonic() for base in reversed(self.__class__.__mro__[:-1]): if hasattr(base, '__dict__'): for key, value in base.__dict__.items(): @@ -40,7 +49,9 @@ class Trigger(AppClass): if not isinstance(value, (int, float, Governor)): value = str(value) class_vars[key] = value - self.log.info("info and statistics: %s", class_vars) + + end = monotonic() + self.log.info("info and statistics [time taken: %.3fµs]: %s", (end - start) * 1_000_000, class_vars) @property def trigger_state(self) -> str: @@ -78,11 +89,15 @@ class Trigger(AppClass): async def run(self) -> None: while True: + start = monotonic() self.log.debug("run trigger code of %s", self.__class__.__name__) if self._timestamp_last_statistics + int(self._config.statistics_interval_in_seconds) < monotonic(): self.log_statistics() self._timestamp_last_statistics = monotonic() self.trigger_code() - self.log.debug("trigger state: %s", self.trigger_state) + end = monotonic() + self._total_consumed_time += end - start + self.runs += 1 + self.log.debug("trigger state: %s, finished in %.3f µs", self.trigger_state, (end - start) * 1_000_000) await asyncio.sleep(self.config.interval_in_seconds) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 1dde605..79534c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "cpu_governor_auto_adjust" -version = "0.5.7" +version = "0.5.12" description = "This application has been developed to automatically change cpu governor based on certain triggers." authors = [ { name = "Martin Reurekas", email = "martin@semrks.nl" }