File: test_table_reporter.py

package info (click to toggle)
python-memray 1.17.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,396 kB
  • sloc: python: 28,451; ansic: 16,507; sh: 10,586; cpp: 8,494; javascript: 1,474; makefile: 822; awk: 12
file content (156 lines) | stat: -rw-r--r-- 4,169 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from memray import AllocatorType
from memray.reporters.table import TableReporter
from tests.utils import MockAllocationRecord


class TestTableReporter:
    def test_empty_report(self):
        # GIVEN / WHEN
        table = TableReporter.from_snapshot([], memory_records=[], native_traces=False)

        # THEN
        assert table.data == []

    def test_single_allocation(self):
        # GIVEN
        peak_allocations = [
            MockAllocationRecord(
                tid=1,
                address=0x1000000,
                size=1024,
                allocator=AllocatorType.MALLOC,
                stack_id=1,
                n_allocations=1,
                _stack=[
                    ("me", "fun.py", 12),
                ],
            ),
        ]

        # WHEN
        table = TableReporter.from_snapshot(
            peak_allocations, memory_records=[], native_traces=False
        )

        # THEN
        assert table.data == [
            {
                "tid": "0x1",
                "size": 1024,
                "allocator": "malloc",
                "n_allocations": 1,
                "stack_trace": "me at fun.py:12",
            }
        ]

    def test_single_native_allocation(self):
        # GIVEN
        peak_allocations = [
            MockAllocationRecord(
                tid=1,
                address=0x1000000,
                size=1024,
                allocator=AllocatorType.MALLOC,
                stack_id=1,
                n_allocations=1,
                _hybrid_stack=[
                    ("me", "fun.c", 12),
                ],
            ),
        ]

        # WHEN
        table = TableReporter.from_snapshot(
            peak_allocations, memory_records=[], native_traces=True
        )

        # THEN
        assert table.data == [
            {
                "tid": "0x1",
                "size": 1024,
                "allocator": "malloc",
                "n_allocations": 1,
                "stack_trace": "me at fun.c:12",
            }
        ]

    def test_multiple_allocations(self):
        # GIVEN
        peak_allocations = [
            MockAllocationRecord(
                tid=1,
                address=0x1000000,
                size=1024,
                allocator=AllocatorType.MALLOC,
                stack_id=1,
                n_allocations=1,
                _stack=[
                    ("me", "foo.py", 12),
                ],
            ),
            MockAllocationRecord(
                tid=1,
                address=0x1100000,
                size=2048,
                allocator=AllocatorType.VALLOC,
                stack_id=2,
                n_allocations=10,
                _stack=[
                    ("you", "bar.py", 21),
                ],
            ),
        ]

        # WHEN
        table = TableReporter.from_snapshot(
            peak_allocations, memory_records=[], native_traces=False
        )

        # THEN
        assert table.data == [
            {
                "tid": "0x1",
                "size": 1024,
                "allocator": "malloc",
                "n_allocations": 1,
                "stack_trace": "me at foo.py:12",
            },
            {
                "tid": "0x1",
                "size": 2048,
                "allocator": "valloc",
                "n_allocations": 10,
                "stack_trace": "you at bar.py:21",
            },
        ]

    def test_empty_stack_trace(self):
        # GIVEN
        peak_allocations = [
            MockAllocationRecord(
                tid=1,
                address=0x1000000,
                size=1024,
                allocator=AllocatorType.MALLOC,
                stack_id=1,
                n_allocations=1,
                _stack=[],
            ),
        ]

        # WHEN
        table = TableReporter.from_snapshot(
            peak_allocations, memory_records=[], native_traces=False
        )

        # THEN
        assert table.data == [
            {
                "tid": "0x1",
                "size": 1024,
                "allocator": "malloc",
                "n_allocations": 1,
                "stack_trace": "???",
            }
        ]