File: utils.py

package info (click to toggle)
lxml-html-clean 0.4.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 228 kB
  • sloc: python: 865; makefile: 12
file content (18 lines) | stat: -rw-r--r-- 548 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import unittest


def peak_memory_usage(func, *args, **kwargs):
    """
    Monitor the memory usage of a function and return the peak memory used, in MiB.
    """
    try:
        from memory_profiler import memory_usage  # type: ignore
    except ImportError:
        raise unittest.SkipTest("memory-profiler is not available")

    try:
        mem_usage = memory_usage((func, args, kwargs), interval=0.1, timeout=None)
    except MemoryError:
        return float("inf")
    peak_memory = max(mem_usage) - min(mem_usage)
    return peak_memory