Debian Codegraph @

tests/stream/test_stream_3.py

raw symbols SCIP
1 import lz4.stream
2 import pytest
3 import sys
4
5
6 _1KB = 1024
7 _1MB = _1KB * 1024
8 _1GB = _1MB * 1024
9
10
11 def compress(x, c_kwargs):
12 c = []
13 with lz4.stream.LZ4StreamCompressor(**c_kwargs) as proc:
14 for start in range(0, len(x), c_kwargs['buffer_size']):
15 chunk = x[start:start + c_kwargs['buffer_size']]
16 block = proc.compress(chunk)
17 c.append(block)
18 if c_kwargs.get('return_bytearray', False):
19 return bytearray().join(c)
20 else:
21 return bytes().join(c)
22
23
24 def decompress(x, d_kwargs):
25 d = []
26 with lz4.stream.LZ4StreamDecompressor(**d_kwargs) as proc:
27 start = 0
28 while start < len(x):
29 block = proc.get_block(x[start:])
30 chunk = proc.decompress(block)
31 d.append(chunk)
32 start += d_kwargs['store_comp_size'] + len(block)
33 if d_kwargs.get('return_bytearray', False):
34 return bytearray().join(d)
35 else:
36 return bytes().join(d)
37
38
39 test_buffer_size = sorted(
40 [256,
41 1 * _1KB,
42 64 * _1KB,
43 1 * _1MB,
44 1 * _1GB,
45 lz4.stream.LZ4_MAX_INPUT_SIZE]
46 )
47
48
49 @pytest.fixture(
50 params=test_buffer_size,
51 ids=[
52 'buffer_size' + str(i) for i in range(len(test_buffer_size))
53 ]
54 )
55 def buffer_size(request):
56 return request.param
57
58
59 test_data = [
60 (b'a' * _1MB),
61 ]
62
63
64 @pytest.fixture(
65 params=test_data,
66 ids=[
67 'data' + str(i) for i in range(len(test_data))
68 ]
69 )
70 def data(request):
71 return request.param
72
73
74 @pytest.mark.thread_unsafe
75 def test_block_decompress_mem_usage(data, buffer_size):
76 kwargs = {
77 'strategy': "double_buffer",
78 'buffer_size': buffer_size,
79 'store_comp_size': 4,
80 }
81
82 if sys.maxsize < 0xffffffff:
83 pytest.skip('Py_ssize_t too small for this test')
84
85 tracemalloc = pytest.importorskip('tracemalloc')
86
87 # Trace memory usage on compression
88 tracemalloc.start()
89 prev_snapshot = None
90
91 for i in range(1000):
92 compressed = compress(data, kwargs)
93
94 if i % 100 == 0:
95 snapshot = tracemalloc.take_snapshot()
96
97 if prev_snapshot:
98 # Filter on lz4.stream module'a allocations
99 stats = [x for x in snapshot.compare_to(prev_snapshot, 'lineno')
100 if lz4.stream.__file__ in x.traceback._frames[0][0]]
101 assert sum(map(lambda x: x.size_diff, stats)) < (1024 * 4)
102
103 prev_snapshot = snapshot
104
105 tracemalloc.stop()
106
107 tracemalloc.start()
108 prev_snapshot = None
109
110 for i in range(1000):
111 decompressed = decompress(compressed, kwargs) # noqa: F841
112
113 if i % 100 == 0:
114 snapshot = tracemalloc.take_snapshot()
115
116 if prev_snapshot:
117 # Filter on lz4.stream module'a allocations
118 stats = [x for x in snapshot.compare_to(prev_snapshot, 'lineno')
119 if lz4.stream.__file__ in x.traceback._frames[0][0]]
120 assert sum(map(lambda x: x.size_diff, stats)) < (1024 * 4)
121
122 prev_snapshot = snapshot
123
124 tracemalloc.stop()