|
| 1 | +""" |
| 2 | +This package is dedicated to all the memory aspects testing. |
| 3 | +""" |
| 4 | +import tracemalloc |
| 5 | +from tracemalloc import Snapshot, Statistic, StatisticDiff |
| 6 | +from copy import deepcopy |
| 7 | +from functools import wraps |
| 8 | +from typing import Callable, Iterable, Optional, List |
| 9 | +import collections.abc as abc |
| 10 | + |
| 11 | +ExcludeType = Optional[Iterable[str]] |
| 12 | + |
| 13 | +DEFAULT_FILTERS = { |
| 14 | + tracemalloc.Filter(False, "<frozen importlib._bootstrap>"), |
| 15 | + tracemalloc.Filter(False, "<unknown>"), |
| 16 | + tracemalloc.Filter(False, tracemalloc.__file__), |
| 17 | + tracemalloc.Filter(False, __file__) |
| 18 | +} |
| 19 | + |
| 20 | +def _filter_snapshot(snapshot: Snapshot, exclude: ExcludeType = None) -> tracemalloc.Snapshot: |
| 21 | + """ |
| 22 | + Filters the given snapshot using the exclude value and the DEFAULT_FILTERS. |
| 23 | + :param snapshot: snapshot to be filtered |
| 24 | + :param exclude: element(s) to be excluded from the snapshot |
| 25 | + :return: the filtered snapshot |
| 26 | + """ |
| 27 | + filters = deepcopy(DEFAULT_FILTERS) |
| 28 | + if isinstance(exclude, str): |
| 29 | + filters.add(tracemalloc.Filter(False, exclude)) |
| 30 | + elif isinstance(exclude, abc.Iterable): |
| 31 | + filters.update( |
| 32 | + {tracemalloc.Filter(False, e) for e in exclude if isinstance(e, str)} |
| 33 | + ) |
| 34 | + return snapshot.filter_traces(tuple(filters)) |
| 35 | + |
| 36 | + |
| 37 | +def _get_overload(snapshot: Snapshot, threshold: float, key_type: str = "lineno") -> \ |
| 38 | + List[Statistic]: |
| 39 | + """ |
| 40 | + Returns a list of statistics that exceed the given threshold in KiB. |
| 41 | + :param snapshot: snapshot to analyze |
| 42 | + :param threshold: threshold not to exceed (in KiB) |
| 43 | + :param key_type: key used to order the snapshot's statistics |
| 44 | +
|
| 45 | + :return: a list of statistics that exceed the given threshold |
| 46 | + """ |
| 47 | + stats = snapshot.statistics(key_type) |
| 48 | + return [stat for stat in stats if stat.size / 1024 > threshold] |
| 49 | + |
| 50 | + |
| 51 | +def _get_leaks(snapshot1: Snapshot, snapshot2: Snapshot, threshold: float, |
| 52 | + key_type: str = "lineno") -> List[StatisticDiff]: |
| 53 | + """ |
| 54 | + Returns a list of statistics that contains detected memory leaks above |
| 55 | + the given threshold in bytes. |
| 56 | + :param snapshot1: first snapshot (before the tested function execution) |
| 57 | + :param snapshot2: second snapshot (after the tested function execution) |
| 58 | + :param threshold: threshold to be exceeded to retain the leaks (in B) |
| 59 | + :param key_type: key used to order the snapshot's statistics |
| 60 | + :return: a list of statistics that contains detected memory leaks |
| 61 | + """ |
| 62 | + return [s for s in snapshot2.compare_to(snapshot1, key_type) if s.size_diff / 1024 >= threshold] |
| 63 | + |
| 64 | + |
| 65 | +def _get_failure_details(stats: List[Statistic]) -> str: |
| 66 | + """ |
| 67 | + Returns a string containing details about the given stats which led to a failure. |
| 68 | + :param stats: statistics to describe |
| 69 | + :return: a string containing details about the given stats |
| 70 | + which led to a failure |
| 71 | + """ |
| 72 | + details = "\n" |
| 73 | + for stat in stats: |
| 74 | + formatted_traceback = "\n".join(stat.traceback.format()) |
| 75 | + details += "{} failed with size : [{} KiB]\n".format(formatted_traceback, stat.size / 1024) |
| 76 | + return details |
| 77 | + |
| 78 | + |
| 79 | +def _get_leaks_details(stats: List[StatisticDiff]) -> str: |
| 80 | + """ |
| 81 | + Returns a string containing details about the given stats which led to a failure. |
| 82 | + :param stats: statistics to describe |
| 83 | + :return: a string containing details about the given stats |
| 84 | + which led to a failure |
| 85 | + """ |
| 86 | + failed_sentence = "{} failed with diff : [{} B]\n" |
| 87 | + details = "\n" |
| 88 | + for stat in stats: |
| 89 | + formatted_traceback = "\n".join(stat.traceback.format()) |
| 90 | + details += failed_sentence.format(formatted_traceback, stat.size_diff / 1024) |
| 91 | + return details |
| 92 | + |
| 93 | + |
| 94 | +def memory_not_exceed(threshold: float, exclude: ExcludeType = None) -> Callable: |
| 95 | + """ |
| 96 | + Tests that the memory taken up by the given function |
| 97 | + doesn't exceed the given threshold in KiB. |
| 98 | + :param threshold: threshold in KiB |
| 99 | + :param exclude: element(s) to be excluded from the snapshot |
| 100 | + :return: the memeory_not_exceed's decorator |
| 101 | + """ |
| 102 | + |
| 103 | + def memory_not_exceed_decorator(func: Callable) -> Callable: |
| 104 | + """ |
| 105 | + memory_not_exceed's decorator. |
| 106 | + :param func: function to call |
| 107 | + :return: the memory_not_exceed's wrapper |
| 108 | + """ |
| 109 | + |
| 110 | + @wraps(func) |
| 111 | + def memory_not_exceed_wrapper(*args) -> None: |
| 112 | + """ |
| 113 | + memory_not_exceed's wrapper. |
| 114 | + :param args: wrapper's arguments |
| 115 | + """ |
| 116 | + tracemalloc.start() |
| 117 | + func(*args) |
| 118 | + snapshot = tracemalloc.take_snapshot() |
| 119 | + tracemalloc.stop() |
| 120 | + snapshot = _filter_snapshot(snapshot, exclude=exclude) |
| 121 | + overload = _get_overload(snapshot, threshold=threshold) |
| 122 | + assert not overload, _get_failure_details(overload) |
| 123 | + |
| 124 | + return memory_not_exceed_wrapper |
| 125 | + |
| 126 | + return memory_not_exceed_decorator |
| 127 | + |
| 128 | + |
| 129 | +def memory_not_leak(threshold: float = 0, exclude: ExcludeType = None) -> Callable: |
| 130 | + """ |
| 131 | + Tests that the memory taken up by the given function |
| 132 | + doesn't leak. |
| 133 | + :param threshold: threshold in B |
| 134 | + :param exclude: element(s) to be excluded from the snapshot |
| 135 | + :return: the memory_not_leak's decorator |
| 136 | + """ |
| 137 | + |
| 138 | + def memory_not_leak_decorator(func: Callable) -> Callable: |
| 139 | + """ |
| 140 | + memory_not_leak's decorator. |
| 141 | + :param func: function to call |
| 142 | + :return: the memory_not_leak's wrapper |
| 143 | + """ |
| 144 | + @wraps(func) |
| 145 | + def memory_not_leak_wrapper(*args) -> None: |
| 146 | + """ |
| 147 | + memory_not_leak's wrapper. |
| 148 | + :param args: wrapper's arguments |
| 149 | + """ |
| 150 | + tracemalloc.start() |
| 151 | + snapshot1 = tracemalloc.take_snapshot() |
| 152 | + func(*args) |
| 153 | + snapshot2 = tracemalloc.take_snapshot() |
| 154 | + tracemalloc.stop() |
| 155 | + snapshot1 = _filter_snapshot(snapshot1, exclude=exclude) |
| 156 | + snapshot2 = _filter_snapshot(snapshot2, exclude=exclude) |
| 157 | + leaks = _get_leaks(snapshot1, snapshot2, threshold) |
| 158 | + assert not leaks, _get_leaks_details(leaks) |
| 159 | + |
| 160 | + return memory_not_leak_wrapper |
| 161 | + |
| 162 | + return memory_not_leak_decorator |
0 commit comments