Skip to main content

pytest-resource-usage

Add running times and peak memory usage of tests to the output of pytest. Two ways of measuring memory usage are implemented. The first uses tracemalloc, which is unaffected by swapping. Since tracemalloc is a standard library, this way of measuring introduces no dependencies beyond pytest. However, measuring with tracemalloc can have a high overhead. The other way of measuring memory usage is by means of sampling the unique set size. This method has lower overhead, but can be less accurate and is only available if the psutil package is installed.

The pytest-resource-usage package is intentionally kept fairly simple. If you want something more feature-rich and ambitious, you should use pytest-monitor.

Example usage

Reporting is triggered by the presence of one or more of the following markers:

  • report_duration,
  • report_tracemalloc,
  • report_uss (requires psutil).
from time import sleep

import pytest


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


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


@pytest.mark.report_uss(interval=0.01)
@pytest.mark.report_duration
def test_unique_set_size():
    _ = list(range(1_000_000))


@pytest.mark.report_duration
def test_no_overhead():
    _ = list(range(1_000_000))

Running the above tests produces the following pytest output.

================================================================ test session starts =================================================================
plugins: pytest-resource-usage-1.0.0
collected 5 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_tracemalloc_overhead[2000000] (call) peak allocated memory: 72MB, peak unique set size: 243MB, running time: 0.627 seconds
tests/test_readme.py::test_tracemalloc_overhead[1000000] (call) peak allocated memory: 36MB, peak unique set size: 115MB, running time: 0.329 seconds
tests/test_readme.py::test_unique_set_size (call) peak unique set size: 25.7MB, running time: 0.047 seconds
tests/test_readme.py::test_no_overhead (call) running time: 0.020 seconds
=========================================================== 5 passed in 100.13s (0:01:40) ============================================================

Metadata

Release files for pytest-resource-usage 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-resource-usage 1.0.0
File Size Uploaded
pytest-resource-usage-1.0.0.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-resource-usage 1.0.0
File Interpreter ABI Platform
pytest_resource_usage-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.1 kB

Release files / pytest-resource-usage-1.0.0.tar.gz

Download URL pytest-resource-usage-1.0.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3a200cee007fc3e7496ff34620b2841494356c92063aa6fa79e9201fd1e583cc
BLAKE2b-256 checksum
How to use checksums
0645596537d11af41c8e11662b9c237cbdebcca30fb4d7fd189bfd772ac1835c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release files / pytest_resource_usage-1.0.0-py3-none-any.whl

Download URL pytest_resource_usage-1.0.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
94534f378c13ccdf1413d461f0d780ff2cebf45f07de1622d6e1c07f0a86154b
BLAKE2b-256 checksum
How to use checksums
ee0c59bd48065714caa13caaeea82a458934b260c833e90057dfa93d5c8ffabc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page