106 lines
6.1 KiB
Python
106 lines
6.1 KiB
Python
# LZMA / LZMA2 / xz fixtures (tools/fixtures.py): deterministic corpora compressed by liblzma (Python's lzma module and
|
|
# the xz CLI): every preset and -e, lc / lp / pb variants, raw LZMA1 / LZMA2, .lzma files with and without the size
|
|
# and end marker, xz with every check type, BCJ (x86, PowerPC, ARM, ARM-Thumb, SPARC, ARM64) and delta filters,
|
|
# multi-block (--block-size), multi-stream and stream padding.
|
|
# tests/gen/lzma.luau { corpora = { [name] = bytes }, cases = { { name, kind, corpus, data, props?, dict?,
|
|
# size? } } } kind: "raw" (LZMA1), "raw2" (LZMA2), "alone", "xz"
|
|
# tests/gen/lzma_bench.luau { size, crc, xz, alone } (a ~4 MB corpus at -6)
|
|
import lzma, os, random, subprocess, sys, zlib
|
|
|
|
|
|
def text_corpus(limit):
|
|
base = os.path.dirname(os.__file__)
|
|
out = bytearray()
|
|
for name in sorted(os.listdir(base)):
|
|
if name.endswith(".py"):
|
|
with open(os.path.join(base, name), "rb") as f:
|
|
out += f.read()
|
|
if len(out) >= limit:
|
|
break
|
|
return bytes(out[:limit])
|
|
|
|
|
|
def binary_corpus(limit):
|
|
# machine code: a slice of the Python interpreter (x86-64 here; exercises the x86 BCJ filter)
|
|
with open(os.path.realpath(sys.executable), "rb") as f:
|
|
data = f.read()
|
|
if len(data) < limit:
|
|
lib = os.path.join(os.path.dirname(os.path.realpath(sys.executable)), "..", "lib")
|
|
for name in sorted(os.listdir(lib)):
|
|
p = os.path.join(lib, name)
|
|
if name.startswith("libpython") and os.path.isfile(p):
|
|
with open(p, "rb") as f:
|
|
data += f.read(limit)
|
|
break
|
|
return data[:limit]
|
|
|
|
|
|
def xz_cli(args, data):
|
|
return subprocess.run(["xz"] + args + ["-c"], input=data, capture_output=True, check=True).stdout
|
|
|
|
|
|
def generate(ctx):
|
|
rng = random.Random(7)
|
|
corpora = {
|
|
"empty": b"",
|
|
"one": b"a",
|
|
"text": text_corpus(150000),
|
|
"binary": binary_corpus(120000),
|
|
"random": rng.randbytes(20000),
|
|
"runs": b"abc" * 3000 + b"\0" * 30000 + bytes(range(256)) * 30 + b"z" * 300,
|
|
"words": b" ".join(rng.choice([b"lorem", b"ipsum", b"dolor", b"sit", b"amet", b"\xff\xfe", b"0123"]) for _ in range(20000)),
|
|
}
|
|
cases = []
|
|
|
|
def add(name, kind, corpus, data, **extra):
|
|
c = {"name": name, "kind": kind, "corpus": corpus, "data": data}
|
|
c.update(extra)
|
|
cases.append(c)
|
|
|
|
for cname, data in corpora.items():
|
|
small = cname in ("empty", "one", "random")
|
|
presets = [0, 6] if small else [0, 1, 3, 6, 9, 9 | lzma.PRESET_EXTREME]
|
|
for p in presets:
|
|
add("%s/xz-%d%s" % (cname, p & 15, "e" if p & lzma.PRESET_EXTREME else ""), "xz", cname,
|
|
lzma.compress(data, lzma.FORMAT_XZ, check=lzma.CHECK_CRC64, preset=p))
|
|
for chk, cn in ((lzma.CHECK_NONE, "none"), (lzma.CHECK_CRC32, "crc32"), (lzma.CHECK_SHA256, "sha256")):
|
|
add("%s/xz-check-%s" % (cname, cn), "xz", cname, lzma.compress(data, lzma.FORMAT_XZ, check=chk, preset=2))
|
|
# .lzma (alone): with a size (Python writes an end marker and no size; the CLI writes the same); raw streams
|
|
add("%s/alone" % cname, "alone", cname, lzma.compress(data, lzma.FORMAT_ALONE, preset=6))
|
|
for lc, lp, pb in ((3, 0, 2), (0, 2, 0), (4, 0, 4), (1, 3, 1), (0, 4, 3)):
|
|
f = [{"id": lzma.FILTER_LZMA1, "preset": 6, "lc": lc, "lp": lp, "pb": pb, "dict_size": 1 << 20}]
|
|
raw = lzma.compress(data, lzma.FORMAT_RAW, filters=f)
|
|
add("%s/raw-lc%d-lp%d-pb%d" % (cname, lc, lp, pb), "raw", cname, raw, props=bytes([(pb * 5 + lp) * 9 + lc]), size=len(data))
|
|
for lc, lp, pb in ((3, 0, 2), (0, 4, 4), (2, 2, 1)):
|
|
f = [{"id": lzma.FILTER_LZMA2, "preset": 6, "lc": lc, "lp": lp, "pb": pb}]
|
|
add("%s/raw2-lc%d-lp%d-pb%d" % (cname, lc, lp, pb), "raw2", cname, lzma.compress(data, lzma.FORMAT_RAW, filters=f))
|
|
if not small:
|
|
for fid, fname in ((lzma.FILTER_X86, "x86"), (lzma.FILTER_POWERPC, "ppc"), (lzma.FILTER_ARM, "arm"),
|
|
(lzma.FILTER_ARMTHUMB, "armthumb"), (lzma.FILTER_SPARC, "sparc")):
|
|
f = [{"id": fid}, {"id": lzma.FILTER_LZMA2, "preset": 3}]
|
|
add("%s/xz-%s" % (cname, fname), "xz", cname, lzma.compress(data, lzma.FORMAT_XZ, filters=f))
|
|
f = [{"id": lzma.FILTER_X86, "start_offset": 0x1000}, {"id": lzma.FILTER_LZMA2, "preset": 3}]
|
|
add("%s/xz-x86-start" % cname, "xz", cname, lzma.compress(data, lzma.FORMAT_XZ, filters=f))
|
|
for dist in (1, 4, 256):
|
|
f = [{"id": lzma.FILTER_DELTA, "dist": dist}, {"id": lzma.FILTER_LZMA2, "preset": 3}]
|
|
add("%s/xz-delta%d" % (cname, dist), "xz", cname, lzma.compress(data, lzma.FORMAT_XZ, filters=f))
|
|
add("%s/xz-cli-arm64" % cname, "xz", cname, xz_cli(["--arm64", "--lzma2=preset=3"], data))
|
|
add("%s/xz-cli-blocks" % cname, "xz", cname, xz_cli(["-6", "--block-size=20000"], data))
|
|
add("%s/xz-cli-x86-delta" % cname, "xz", cname, xz_cli(["--x86", "--delta=dist=2", "--lzma2=preset=1"], data))
|
|
add("%s/alone-cli" % cname, "alone", cname, xz_cli(["--format=lzma", "-3"], data))
|
|
# multi-stream + stream padding
|
|
corpora["multi"] = corpora["text"][:40000] + corpora["runs"] + b"" + corpora["one"]
|
|
multi = (lzma.compress(corpora["text"][:40000], lzma.FORMAT_XZ, preset=1) + b"\0" * 8 +
|
|
lzma.compress(corpora["runs"], lzma.FORMAT_XZ, check=lzma.CHECK_CRC32) +
|
|
lzma.compress(b"", lzma.FORMAT_XZ) + lzma.compress(b"a", lzma.FORMAT_XZ, check=lzma.CHECK_NONE) + b"\0" * 4)
|
|
add("multi/xz-streams", "xz", "multi", multi)
|
|
|
|
ctx.emit("lzma", {"corpora": corpora, "cases": cases})
|
|
|
|
# real text and machine code, little long-range repetition
|
|
bench = text_corpus(2_000_000) + binary_corpus(1_000_000)
|
|
bench = bench[:3_000_000]
|
|
ctx.emit("lzma_bench", {"size": len(bench), "crc": zlib.crc32(bench),
|
|
"xz": lzma.compress(bench, lzma.FORMAT_XZ, preset=6),
|
|
"alone": lzma.compress(bench, lzma.FORMAT_ALONE, preset=6)})
|