| 1 | """1 GiB 입력의 7z 작성 메모리와 게시 결과를 검증함.""" |
| 2 | |
| 3 | import json |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import struct |
| 7 | import subprocess |
| 8 | import sys |
| 9 | import tempfile |
| 10 | import time |
| 11 | |
| 12 | |
| 13 | def native(path): |
| 14 | value = os.fsencode(path) |
| 15 | return b"\x01" + struct.pack("<I", len(value)) + value |
| 16 | |
| 17 | |
| 18 | with tempfile.TemporaryDirectory(prefix="7zc-memory-", dir=os.environ["TMPDIR"]) as temporary: |
| 19 | root = Path(temporary) |
| 20 | source = root / "source" |
| 21 | with source.open("xb") as output: |
| 22 | output.truncate(1024 * 1024 * 1024) |
| 23 | archive = root / "result.7z" |
| 24 | body = b"\x04" + struct.pack("<I", 1) + native(source) |
| 25 | body += struct.pack("<I", 11) + b"payload.bin" + native(archive) |
| 26 | body += b"\x00\x00\x00" |
| 27 | body += b"\x00" # volume_size=None으로 분할하지 않음. |
| 28 | body += struct.pack("<QQI", 2**64 - 1, 16 * 1024**3, 10000) |
| 29 | frame = b"ZAW1" + struct.pack("<HHIIQQ", 1, 1, len(body), 0, 17, 0) + body |
| 30 | started = time.monotonic() |
| 31 | result = subprocess.run( |
| 32 | ["/usr/bin/time", "-f", "%M", "-o", str(root / "rss"), sys.argv[1]], |
| 33 | input=frame, capture_output=True, timeout=180, |
| 34 | env={**os.environ, "TMPDIR": str(root), "TMP": str(root), "TEMP": str(root)}, |
| 35 | ) |
| 36 | assert result.returncode == 0, result.stderr |
| 37 | assert not result.stderr, result.stderr |
| 38 | at = 0 |
| 39 | terminal = None |
| 40 | outputs = 0 |
| 41 | while at < len(result.stdout): |
| 42 | magic, version, kind, size, reserved, job, sequence = struct.unpack_from("<4sHHIIQQ", result.stdout, at) |
| 43 | assert (magic, version, reserved, job) == (b"ZAW1", 1, 0, 17) |
| 44 | at += 32 |
| 45 | payload = result.stdout[at:at + size] |
| 46 | assert len(payload) == size |
| 47 | at += size |
| 48 | if kind == 105: |
| 49 | outputs += 1 |
| 50 | if kind == 106: |
| 51 | assert at == len(result.stdout) |
| 52 | terminal = payload |
| 53 | assert terminal[:3] == b"\x00\x00\x02", terminal |
| 54 | assert struct.unpack_from("<QQ", terminal, 3) == (1, 0) |
| 55 | assert outputs == 1 and archive.is_file() |
| 56 | rss = int((root / "rss").read_text()) |
| 57 | assert rss <= 64 * 1024, rss |
| 58 | print(json.dumps({ |
| 59 | "seven_zip_creation_input_bytes": source.stat().st_size, |
| 60 | "archive_bytes": archive.stat().st_size, |
| 61 | "peak_rss_kib": rss, |
| 62 | "elapsed_seconds": round(time.monotonic() - started, 3), |
| 63 | })) |