def measure_power_and_energy(self, last_duration: float) -> Tuple[Power, Energy]: """ Base implementation: we get the power from the hardware and convert it to energy. """ power = self.total_power() energy = Energy.from_power_and_time( power=power, time=Time.from_seconds(last_duration)) return power, energy
def _measure_power(self) -> None: """ A function that is periodically run by the `BackgroundScheduler` every `self._measure_power` seconds. :return: None """ last_duration = time.time() - self._last_measured_time warning_duration = self._measure_power_secs * 3 if last_duration > warning_duration: warn_msg = ( "Background scheduler didn't run for a " + "long period (%ds), results might be inacurate" ) logger.warning(warn_msg, last_duration) self._total_energy += Energy.from_power_and_time( power=self._hardware.total_power, time=Time.from_seconds(last_duration) ) self._last_measured_time = time.time()
def _measure_power(self) -> None: """ A function that is periodically run by the `BackgroundScheduler` every `self._measure_power` seconds. :return: None """ last_duration = time.time() - self._last_measured_time warning_duration = self._measure_power_secs * 3 if last_duration > warning_duration: warn_msg = ( "CODECARBON : Background scheduler didn't run for a long period" + " (%ds), results might be inaccurate") logger.warning(warn_msg, last_duration) for hardware in self._hardware: self._total_energy += Energy.from_power_and_time( power=hardware.total_power(), time=Time.from_seconds(last_duration)) logger.info( f"CODECARBON : Energy consumed {hardware.__class__.__name__} : {self._total_energy}" ) self._last_measured_time = time.time()