125 lines
6.0 KiB
Python
125 lines
6.0 KiB
Python
# Deflate fixtures (tools/fixtures.py): deterministic corpora compressed with Python's zlib at every level and
|
|
# strategy, raw / zlib / gzip framing, small windows, flushes, gzip headers and multi-member gzip; plus a 6 MB bench
|
|
# corpus. Ported from deflate-luau tools/fixtures.py. Reads Webamp skins (zips of BMPs) from $SKINS_DIR as data only,
|
|
# if present (the corpus is smaller without them; the tests do not depend on it).
|
|
# tests/gen/deflate.luau { corpora = { [name] = bytes }, cases = { { name, kind, corpus, data } } }
|
|
# tests/gen/deflate_bench.luau { size, crc, zlib, gzip }
|
|
import gzip, os, random, zipfile, zlib
|
|
|
|
SKINS = os.environ.get("SKINS_DIR", "/tmp/wa-ref/webamp/packages/webamp-demo/skins")
|
|
|
|
|
|
def text_corpus(limit):
|
|
# Python's own stdlib sources: real text, deterministic for a given Python
|
|
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 skin_bmps(limit):
|
|
out = bytearray()
|
|
if os.path.isdir(SKINS):
|
|
for name in sorted(os.listdir(SKINS)):
|
|
if name.lower().endswith(".wsz"):
|
|
with zipfile.ZipFile(os.path.join(SKINS, name)) as z:
|
|
for i in z.infolist():
|
|
if i.filename.lower().endswith(".bmp"):
|
|
out += z.read(i)
|
|
if len(out) >= limit:
|
|
return bytes(out[:limit])
|
|
return bytes(out)
|
|
|
|
|
|
def generate(ctx):
|
|
rng = random.Random(1)
|
|
def text_corpus(limit):
|
|
# Python's own stdlib sources: real text, deterministic for a given Python
|
|
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 skin_bmps(limit):
|
|
out = bytearray()
|
|
if os.path.isdir(SKINS):
|
|
for name in sorted(os.listdir(SKINS)):
|
|
if name.lower().endswith(".wsz"):
|
|
with zipfile.ZipFile(os.path.join(SKINS, name)) as z:
|
|
for i in z.infolist():
|
|
if i.filename.lower().endswith(".bmp"):
|
|
out += z.read(i)
|
|
if len(out) >= limit:
|
|
return bytes(out[:limit])
|
|
return bytes(out)
|
|
|
|
corpora = {
|
|
"empty": b"",
|
|
"one": b"a",
|
|
"text": text_corpus(160000),
|
|
"random": rng.randbytes(40000),
|
|
"runs": b"abc" * 4000 + b"\0" * 20000 + bytes(range(256)) * 40 + b"z" * 300,
|
|
"far": (lambda r: r + rng.randbytes(20000) + r)(rng.randbytes(12000)),
|
|
"bmp": skin_bmps(120000),
|
|
"binary": bytes(rng.choice(b"\0\0\0\x01\x02\xff ABC") for _ in range(30000)),
|
|
}
|
|
|
|
cases = [] # (name, kind, corpus, compressed)
|
|
for cname, data in corpora.items():
|
|
for level in range(10):
|
|
cases.append(("%s/zlib%d" % (cname, level), "zlib", cname, zlib.compress(data, level)))
|
|
for sname, strat in (("fixed", zlib.Z_FIXED), ("huffman", zlib.Z_HUFFMAN_ONLY), ("rle", zlib.Z_RLE), ("filtered", zlib.Z_FILTERED)):
|
|
c = zlib.compressobj(6, zlib.DEFLATED, -15, 8, strat)
|
|
cases.append(("%s/raw-%s" % (cname, sname), "raw", cname, c.compress(data) + c.flush()))
|
|
for wbits, mem in ((-9, 1), (-12, 9)):
|
|
c = zlib.compressobj(9, zlib.DEFLATED, wbits, mem)
|
|
cases.append(("%s/raw-w%d-m%d" % (cname, -wbits, mem), "raw", cname, c.compress(data) + c.flush()))
|
|
# flushes in the middle: empty stored blocks, byte alignment, several block types in one stream
|
|
c = zlib.compressobj(5, zlib.DEFLATED, -15)
|
|
parts = []
|
|
step = max(1, len(data) // 5)
|
|
for i in range(0, len(data), step):
|
|
parts.append(c.compress(data[i:i + step]))
|
|
parts.append(c.flush(zlib.Z_SYNC_FLUSH if (i // step) % 2 == 0 else zlib.Z_FULL_FLUSH))
|
|
parts.append(c.flush())
|
|
cases.append(("%s/raw-flushes" % cname, "raw", cname, b"".join(parts)))
|
|
cases.append(("%s/gzip" % cname, "gzip", cname, gzip.compress(data, 6, mtime=0)))
|
|
|
|
# gzip with a file name / comment / extra / header CRC (written by hand), and multi-member
|
|
def gzip_full(data):
|
|
hdr = bytearray(b"\x1f\x8b\x08\x1e\0\0\0\0\x00\x03") # FEXTRA | FNAME | FCOMMENT | FHCRC
|
|
hdr += (4).to_bytes(2, "little") + b"ab\x01\x00"
|
|
hdr += b"name.txt\0" + b"a comment\0"
|
|
hdr += (zlib.crc32(bytes(hdr)) & 0xFFFF).to_bytes(2, "little")
|
|
c = zlib.compressobj(9, zlib.DEFLATED, -15)
|
|
body = c.compress(data) + c.flush()
|
|
return bytes(hdr) + body + zlib.crc32(data).to_bytes(4, "little") + (len(data) & 0xFFFFFFFF).to_bytes(4, "little")
|
|
corpora["multi"] = corpora["text"][:50000] + corpora["runs"] + b"" + corpora["one"]
|
|
multi = gzip.compress(corpora["text"][:50000], 9, mtime=0) + gzip_full(corpora["runs"]) + gzip.compress(b"", mtime=0) + gzip.compress(b"a", 1, mtime=0) + b"\0" * 7
|
|
cases.append(("multi/gzip-members", "gzip", "multi", multi))
|
|
cases.append(("runs/gzip-fullheader", "gzip", "runs", gzip_full(corpora["runs"])))
|
|
|
|
|
|
ctx.emit("deflate", {
|
|
"corpora": corpora,
|
|
"cases": [{"name": n, "kind": k, "corpus": c, "data": d} for n, k, c, d in cases],
|
|
})
|
|
# bench corpus: ~6 MB of text and BMPs, zlib level 6
|
|
bench = (corpora["text"] + corpora["bmp"]) * 1
|
|
while len(bench) < 6_000_000:
|
|
bench += corpora["text"] + skin_bmps(600000) + corpora["binary"]
|
|
bench = bench[:6_000_000]
|
|
|
|
ctx.emit("deflate_bench", {"size": len(bench), "crc": zlib.crc32(bench), "zlib": zlib.compress(bench, 6),
|
|
"gzip": gzip.compress(bench, 6, mtime=0)})
|