Skip to main content

Pytest plugin for reporting running time and peak memory usage

Project description


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

def test_sleep():

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

def test_unique_set_size():
    _ = list(range(1_000_000))

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/ .....                                                                                                                     [100%]

=================================================================== resource usage ===================================================================
tests/ (call) running time: 0:01:39
tests/[2000000] (call) peak allocated memory: 72MB, peak unique set size: 243MB, running time: 0.627 seconds
tests/[1000000] (call) peak allocated memory: 36MB, peak unique set size: 115MB, running time: 0.329 seconds
tests/ (call) peak unique set size: 25.7MB, running time: 0.047 seconds
tests/ (call) running time: 0.020 seconds
=========================================================== 5 passed in 100.13s (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-1.0.0.tar.gz (5.6 kB view hashes)

Uploaded source

Built Distribution

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