42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Compare stronger compression candidates for Pixel Island asset JSON.
|
|
|
|
Usage:
|
|
python server/compression_lab.py exported_world.json
|
|
|
|
This does not change production data. It reports approximate sizes for raw JSON,
|
|
minified JSON, gzip, zlib, and brotli if the optional `brotli` module is installed.
|
|
For browser/server interchange, prefer: compact JSON -> gzip/brotli at HTTP layer.
|
|
"""
|
|
from __future__ import annotations
|
|
import argparse, gzip, json, zlib
|
|
from pathlib import Path
|
|
|
|
try:
|
|
import brotli # type: ignore
|
|
except Exception: # pragma: no cover
|
|
brotli = None
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument('json_file', type=Path)
|
|
args = ap.parse_args()
|
|
data = json.loads(args.json_file.read_text(encoding='utf-8'))
|
|
pretty = json.dumps(data, ensure_ascii=False, indent=2).encode('utf-8')
|
|
mini = json.dumps(data, ensure_ascii=False, separators=(',', ':')).encode('utf-8')
|
|
rows = [
|
|
('pretty_json', len(pretty)),
|
|
('minified_json', len(mini)),
|
|
('gzip_9', len(gzip.compress(mini, compresslevel=9))),
|
|
('zlib_9', len(zlib.compress(mini, level=9))),
|
|
]
|
|
if brotli:
|
|
rows.append(('brotli_11', len(brotli.compress(mini, quality=11))))
|
|
base = len(pretty) or 1
|
|
for name, size in rows:
|
|
print(f'{name:14} {size:10d} bytes {size/base:6.1%} of pretty JSON')
|
|
return 0
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|