Skip to main content

An energy estimation reporter for pytest

Project description

An Energy Reporter Plugin for pytest

The pytest_energy_reporter is a pytest plugin can be used to report on energy and power consumption usage of code, by easily integrating in existing test suites.

It relies on the Energy Consumption Reporter to get approximations for energy measurements.

Installation

To install pytest energy reporter, simply run:

pip install pytest_energy_reporter

Usage

The package is automatically enabled in pytest. If it is not, make sure to read the documentation on how to enable plugins in pytest. To start measuring energy consumption, mark tests as energy test using:

import pytest

@pytest.mark.energy
def test_fn():

# Or define the exact number of iterations to measure
@pytest.mark.energy(n=3)
def test_fn():

# Or use the measurement directly
from pytest_energy_reporter.measurement import measure_energy

def test_fn():
  measure = measure_energy(fn)
  assert measure.energy_j < 200

# Or use the measurement and the result of the method
from pytest_energy_reporter.measurement import measure

def test_fn():
  measure, result, error = measure(fn, n=2)
  assert result == # ...
  assert error == None
  assert result.energy_j < 200

The plugin exposes a few flags in pytest

Flag Argument Default Meaning
--energy-iterations int 3 Specify the number of iterations for the energy measurement.
--save-energy-report <none> False Feature flag to save the energy report.
--energy-report-path str 'reports/energy' The path to save the energy report in.
--energy-offset-zero <none> False Feature flag to compensate the power measurement by the power draw of zero utilization.

Contributing

Prerequistes

Make sure you have poetry installed.

Installation

To install the pytest energy reporter repository, run:

  1. Clone the repository
git clone git@github.com:delanoflipse/pytest-energy-reporter.git
  1. Install dependencies
poetry install

Usage

To develop the plugin, use:

poetry shell

And run the example test suite:

pytest --log-cli-level=DEBUG

Publishing

  1. Build the project:
poetry build
  1. Publish to pip (make sure you have credentials setup):
poetry publish

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_energy_reporter-0.5.2.tar.gz (18.3 kB view hashes)

Uploaded Source

Built Distribution

pytest_energy_reporter-0.5.2-py3-none-any.whl (19.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page