Skip to main content

Pytest plugin for reporting running time and peak memory usage

Project description

pytest-resource-usage

This is a personal experiment to add running times and peak memory usage (including swap) of tests to the output of pytest. Memory usage is tracked through tracemalloc, which can have a high overhead. Since tracemalloc is a standard library, the only dependency of pytest-resource-usage is pytest.

If you want something more feature-rich and ambitious, you should use pytest-monitor.

Example usage

Reporting is triggered by the presence of the report_duration and report_tracemalloc markers.

from time import sleep

import pytest


@pytest.mark.report_duration
def test_sleep():
    sleep(99)


@pytest.mark.report_tracemalloc
@pytest.mark.parametrize("elements", [2_000_000, 1_000_000])
def test_allocate(elements):
    _ = list(range(elements))


@pytest.mark.report_duration
@pytest.mark.report_tracemalloc
def test_sleep_trace_overhead():
    _ = list(range(100_000))
    sleep(0.2)

Running the above tests produces the following pytest output

============================================== test session starts ===============================================
plugins: pytest_resource_usage-0.0.1
collected 4 items                                                                                                

tests/test_readme.py ....                                                                                  [100%]

================================================= resource usage =================================================
tests/test_readme.py::test_sleep (call) running time: 0:01:39
tests/test_readme.py::test_allocate[2000000] (call) peak allocated memory: 72MB
tests/test_readme.py::test_allocate[1000000] (call) peak allocated memory: 36MB
tests/test_readme.py::test_sleep_trace_overhead (call) peak allocated memory: 3.59MB, running time: 0.228 seconds
========================================= 4 passed in 100.20s (0:01:40) ==========================================

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_resource_usage-0.1.0.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

pytest_resource_usage-0.1.0-py3-none-any.whl (5.2 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