Software energy consumption estimation with power samples
In electricity, energy is the amount of work required to transfer charged particles. The standard unit of measurement for energy is joule (J), but another common used unit is kilowatt-hour (kWh). Joules can be converted to/from kilowatt-hour as follows:
Simply put, power is the energy consumed in a given time interval. It is measured in joules per second (J/s) or watts (W). Power is used when it is more desirable to know energy efficiency than to know total energy consumption. This is why it is the metric reported in power monitor tools such as PowerTOP and Powerstat. However, in software projects, we usually want to analyse it by use case, so we want to know its energy consumption.
One way to estimate the energy consumption using power samples is to infer it by the values reported during the software's execution. The energy consumption (\( E \)) is the integral of power consumption (\( P \)) over the interval of time needed (\([t_0,\ t_N]\)) to execute a given operation:
Power monitors do not give us the exact function \( P \), but we can approximate the value of this definite integral with the trapezoidal rule:
This estimation can easily be done in Python using the numpy or scipy packages:
# Parameters:
# power_sample (array-like): Power measurements taken at
# corresponding time points.
# timestamps (array-like): Time points at which the power measurements
# were recorded. Must have the same length
# as power_sample.
import numpy as np
import scipy as sp
np_energy_consumption = np.trapezoid(power_sample, timestamps)
sp_energy_consumption = sp.integrate.trapezoid(power_sample, timestamps)
34.58 kB