Debian Codegraph @

tests/test_benchmark.py

raw symbols ◀ ▶ SCIP
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.hookimpl(optionalhook=True)
18 @pytest.mark.benchmark(group="compress")
19 @pytest.mark.parametrize("name, var, max_order, mem_size", targets)
20 def test_benchmark_text_compress(tmp_path, benchmark, name, var, max_order, mem_size):
21 cpuinfo = pytest.importorskip("cpuinfo")
22
23 def encode(var, max_order, mem_size):
24 if var == 7:
25 encoder = pyppmd.Ppmd7Encoder(max_order=max_order, mem_size=mem_size)
26 else:
27 encoder = pyppmd.Ppmd8Encoder(max_order=max_order, mem_size=mem_size)
28 with io.BytesIO() as target:
29 with testdata.open("rb") as src:
30 data = src.read(READ_BLOCKSIZE)
31 while len(data) > 0:
32 target.write(encoder.encode(data))
33 data = src.read(READ_BLOCKSIZE)
34 target.write(encoder.flush())
35
36 benchmark.extra_info["data_size"] = src_size
37 benchmark(encode, var, max_order, mem_size)
38
39
•40 @pytest.hookimpl(optionalhook=True)
41 @pytest.mark.benchmark(group="decompress")
42 @pytest.mark.parametrize("name, var, max_order, mem_size", targets)
43 def test_benchmark_text_decompress(tmp_path, benchmark, name, var, max_order, mem_size):
44 cpuinfo = pytest.importorskip("cpuinfo")
45
46 def decode(var, max_order, mem_size):
47 if var == 7:
48 decoder = pyppmd.Ppmd7Decoder(max_order=max_order, mem_size=mem_size)
49 else:
50 decoder = pyppmd.Ppmd8Decoder(max_order=max_order, mem_size=mem_size)
51 with tmp_path.joinpath("target.ppmd").open("rb") as src:
52 with io.BytesIO() as target:
53 remaining = src_size
54 data = src.read(READ_BLOCKSIZE)
55 while remaining > 0:
56 out = decoder.decode(data, remaining)
57 if len(out) == 0:
58 break
59 target.write(out)
60 remaining = remaining - len(out)
61 data = src.read(READ_BLOCKSIZE)
62 assert remaining == 0
63
64 # prepare compressed data
65 if var == 7:
66 encoder = pyppmd.Ppmd7Encoder(max_order=max_order, mem_size=mem_size)
67 else:
68 encoder = pyppmd.Ppmd8Encoder(max_order=max_order, mem_size=mem_size)
69 with tmp_path.joinpath("target.ppmd").open("wb") as target:
70 with testdata.open("rb") as src:
71 data = src.read(READ_BLOCKSIZE)
72 while len(data) > 0:
73 target.write(encoder.encode(data))
74 data = src.read(READ_BLOCKSIZE)
75 target.write(encoder.flush())
76
77 benchmark.extra_info["data_size"] = src_size
78 benchmark(decode, var, max_order, mem_size)