server
This commit is contained in:
parent
2501a4c975
commit
67a7477242
9 changed files with 1116 additions and 139 deletions
42
server/compression_lab.py
Normal file
42
server/compression_lab.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#!/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())
|
||||
Loading…
Add table
Add a link
Reference in a new issue