-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_memory_counter.py
More file actions
78 lines (59 loc) · 3.61 KB
/
test_memory_counter.py
File metadata and controls
78 lines (59 loc) · 3.61 KB
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
import sys
from pympler import asizeof
from codeguru_profiler_agent.model.call_graph_node import CallGraphNode
from codeguru_profiler_agent.model.frame import Frame
from codeguru_profiler_agent.model.memory_counter import MemoryCounter
# These tests validate that our simple memory tracker's math matches with what we can observe in the Pympler memory
# sizing library
class TestMemoryCounter:
class TestEmptyNodeSize:
def test_it_matches_the_expected_size(self):
dummy_frame_name = None
node = CallGraphNode(frame_name=dummy_frame_name, class_name=None, file_path=None, line_no=None)
node.increase_runnable_count()
full_recursive_size_of_node = asizeof.asizeof(node)
frame_name_size = sys.getsizeof(dummy_frame_name)
empty_children_tuple_size = sys.getsizeof(())
assert (MemoryCounter.empty_node_size_bytes == \
(full_recursive_size_of_node \
# The empty size should not include the frame name, so we subtract it
- frame_name_size
# The empty tuple is always reused by Python, so we also subtract it
- empty_children_tuple_size))
def test_sanity_check_it_is_smaller_than_512_bytes(self):
assert (MemoryCounter.empty_node_size_bytes <= 256)
class TestBaseStorageSize:
def test_it_matches_the_expected_size(self):
node = CallGraphNode(frame_name=None, class_name=None, file_path=None, line_no=None)
node.update_current_node_and_get_child(frame=Frame(None))
recursive_node_storage_size = asizeof.asizeof(node.children)
child_node_size = asizeof.asizeof(
CallGraphNode(frame_name=None, class_name=None, file_path=None, line_no=None))
assert (MemoryCounter.base_storage_size_bytes == (
recursive_node_storage_size - child_node_size))
def test_sanity_check_it_is_smaller_than_100_bytes(self):
assert (MemoryCounter.base_storage_size_bytes <= 100)
class TestStorageIncrementSize:
def test_it_matches_the_expected_size(self):
node = CallGraphNode(frame_name=None, class_name=None, file_path=None, line_no=None)
node.update_current_node_and_get_child(Frame("child1"))
one_child_storage_size = asizeof.asizeof(node.children, limit=0)
node.update_current_node_and_get_child(Frame("child2"))
two_children_storage_size = asizeof.asizeof(node.children, limit=0)
assert (MemoryCounter.storage_increment_size_bytes == (
two_children_storage_size - one_child_storage_size))
def test_sanity_check_it_is_smaller_than_16_bytes(self):
assert (MemoryCounter.storage_increment_size_bytes <= 16)
class TestCountCreateNode:
def test_sanity_check_it_counts_frame_file_path_line_no_class_name_size(self):
subject = MemoryCounter()
subject.count_create_node(frame="test/frame", file_path="test/file/path", class_name="TestClass")
# [Oct-2020 Python-3.7.7] "test/frame" size: 59 bytes; "test/file/path" size: 63 bytes; "TestClass" size:
# 58 bytes; fixed line_no size: 2 * 32 = 64; sum = 244
# [Dec-2025 Python-3.12+] Internal string memory optimization reduced size by 24 bytes; sum = 220
import sys
if sys.version_info >= (3, 12):
expected_size = subject.empty_node_size_bytes + 220
else:
expected_size = subject.empty_node_size_bytes + 244
assert (subject.get_memory_usage_bytes() == expected_size)