Debian Codegraph @

tests/test_benchmark.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import io
2 import os
3 import pathlib
4
5 import pytest
6
7 import pyppmd
8
9 testdata_path = pathlib.Path(os.path.join(os.path.dirname(__file__), "data"))
10 testdata = testdata_path.joinpath("10000SalesRecords.csv")
11 src_size = testdata.stat().st_size
12 READ_BLOCKSIZE = 1048576
13
14 targets = [("PPMd H", 7, 6, 16 << 20), ("PPMd I", 8, 8, 8 << 20)]
15
16
17 @pytest.mark.benchmark(group="compress")
18 @pytest.mark.parametrize("name, var, max_order, mem_size", targets)
19 def test_benchmark_text_compress(tmp_path, benchmark, name, var, max_order, mem_size):
20 cpuinfo = pytest.importorskip("cpuinfo")
21
22 def encode(var, max_order, mem_size):
23 if var == 7:
24 encoder = pyppmd.Ppmd7Encoder(max_order=max_order, mem_size=mem_size)
25 else:
26 encoder = pyppmd.Ppmd8Encoder(max_order=max_order, mem_size=mem_size)
27 with io.BytesIO() as target:
28 with testdata.open("rb") as src:
29 data = src.read(READ_BLOCKSIZE)
30 while len(data) > 0:
31 target.write(encoder.encode(data))
32 data = src.read(READ_BLOCKSIZE)
33 target.write(encoder.flush())
34
35 benchmark.extra_info["data_size"] = src_size
36 benchmark(encode, var, max_order, mem_size)
37
38
39 @pytest.mark.benchmark(group="decompress")
40 @pytest.mark.parametrize("name, var, max_order, mem_size", targets)
41 def test_benchmark_text_decompress(tmp_path, benchmark, name, var, max_order, mem_size):
42 cpuinfo = pytest.importorskip("cpuinfo")
43
44 def decode(var, max_order, mem_size):
45 if var == 7:
46 decoder = pyppmd.Ppmd7Decoder(max_order=max_order, mem_size=mem_size)
47 else:
48 decoder = pyppmd.Ppmd8Decoder(max_order=max_order, mem_size=mem_size)
49 with tmp_path.joinpath("target.ppmd").open("rb") as src:
50 with io.BytesIO() as target:
51 remaining = src_size
52 data = src.read(READ_BLOCKSIZE)
53 while remaining > 0:
54 out = decoder.decode(data, remaining)
55 if len(out) == 0:
56 break
57 target.write(out)
58 remaining = remaining - len(out)
59 data = src.read(READ_BLOCKSIZE)
60 assert remaining == 0
61
62 # prepare compressed data
63 if var == 7:
64 encoder = pyppmd.Ppmd7Encoder(max_order=max_order, mem_size=mem_size)
65 else:
66 encoder = pyppmd.Ppmd8Encoder(max_order=max_order, mem_size=mem_size)
67 with tmp_path.joinpath("target.ppmd").open("wb") as target:
68 with testdata.open("rb") as src:
69 data = src.read(READ_BLOCKSIZE)
70 while len(data) > 0:
71 target.write(encoder.encode(data))
72 data = src.read(READ_BLOCKSIZE)
73 target.write(encoder.flush())
74
75 benchmark.extra_info["data_size"] = src_size
76 benchmark(decode, var, max_order, mem_size)