Debian Codegraph @

tests/frame/test_frame_5.py

raw symbols SCIP
1 import lz4.frame
2 import pytest
3 import gc
4
5 MEM_INCREASE_LIMIT = (1024 * 25)
6
7 test_data = [
8 (b'a' * 1024 * 1024),
9 ]
10
11 pytestmark = pytest.mark.thread_unsafe
12
13
14 @pytest.fixture(
15 params=test_data,
16 ids=[
17 'data' + str(i) for i in range(len(test_data))
18 ]
19 )
20 def data(request):
21 return request.param
22
23
24 def test_frame_decompress_mem_usage(data):
25 tracemalloc = pytest.importorskip('tracemalloc')
26
27 tracemalloc.start()
28
29 compressed = lz4.frame.compress(data)
30 prev_snapshot = None
31
32 for i in range(1000):
33 decompressed = lz4.frame.decompress(compressed) # noqa: F841
34
35 if i % 100 == 0:
36 gc.collect()
37 snapshot = tracemalloc.take_snapshot()
38
39 if prev_snapshot:
40 stats = snapshot.compare_to(prev_snapshot, 'lineno')
41 assert stats[0].size_diff < MEM_INCREASE_LIMIT
42
43 prev_snapshot = snapshot
44
45
46 def test_frame_decompress_chunk_mem_usage(data):
47 tracemalloc = pytest.importorskip('tracemalloc')
48 tracemalloc.start()
49
50 compressed = lz4.frame.compress(data)
51
52 prev_snapshot = None
53
54 for i in range(1000):
55 context = lz4.frame.create_decompression_context()
56 decompressed = lz4.frame.decompress_chunk( # noqa: F841
57 context, compressed
58 )
59
60 if i % 100 == 0:
61 gc.collect()
62 snapshot = tracemalloc.take_snapshot()
63
64 if prev_snapshot:
65 stats = snapshot.compare_to(prev_snapshot, 'lineno')
66 assert stats[0].size_diff < MEM_INCREASE_LIMIT
67
68 prev_snapshot = snapshot
69
70
71 def test_frame_open_decompress_mem_usage(data):
72 tracemalloc = pytest.importorskip('tracemalloc')
73 tracemalloc.start()
74
75 with lz4.frame.open('test.lz4', 'w') as f:
76 f.write(data)
77
78 prev_snapshot = None
79
80 for i in range(1000):
81 with lz4.frame.open('test.lz4', 'r') as f:
82 decompressed = f.read() # noqa: F841
83
84 if i % 100 == 0:
85 gc.collect()
86 snapshot = tracemalloc.take_snapshot()
87
88 if prev_snapshot:
89 stats = snapshot.compare_to(prev_snapshot, 'lineno')
90 assert stats[0].size_diff < MEM_INCREASE_LIMIT
91
92 prev_snapshot = snapshot
93
94
95 # TODO: add many more memory usage tests along the lines of this one
96 # for other funcs
97
98 def test_dummy_always_pass():
99 # If pytest finds all tests are skipped, then it exits with code 5 rather
100 # than 0, which tox sees as an error. Here we add a dummy test that always passes.
101 assert True