File: test_tracemalloc.py

package info (click to toggle)
python-memory-profiler 0.61-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 540 kB
  • sloc: python: 2,220; makefile: 33
file content (54 lines) | stat: -rw-r--r-- 1,264 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from io import StringIO
from time import sleep

from memory_profiler import profile

try:
    import tracemalloc
    has_tracemalloc = True
except ImportError:
    has_tracemalloc = False

output = StringIO()

# allowable error in MB
EPSILON = 0.0001


def test_memory_profiler(test_input, expected):
    mem_prof(test_input)
    inc, dec = parse_mem_prof()
    assert abs(inc - expected) <= EPSILON, \
        'inc = {}, size = {}, err = {}'.format(
            inc, expected, abs(inc - expected)
        )


@profile(stream=output, precision=6, backend='tracemalloc')
def mem_prof(n):
    a = bytearray(n)
    del a
    sleep(1)


def parse_mem_prof():
    text = output.getvalue().split('\n')

    def f(s):
        return float(s.split()[3])

    return f(text[-6]), -f(text[-5])

if __name__ == '__main__':
    if has_tracemalloc:
        tests = [
            (100, 0.00012302398681640625),
            (1000, 0.0009813308715820312),
            (10000, 0.009564399719238281),
            (100000, 0.09539508819580078),
            (1000000, 0.9537019729614258),
            (10000000, 9.536770820617676),
            (100000000, 95.36745929718018),
        ]
        for test_input, expected in tests:
            test_memory_profiler(test_input, expected)