Data Compression Benchmark (python, written by Codex)
envgap__codex__python-t1-40
Written by a coding agent; not on GitHubWritten 2026-03-03
01 / FAILURE SIGNATURE
As the study recorded it
None
Not a benchmark task.
- The project already builds and runs before the fix, so there is nothing to repair.
02 / ENVIRONMENT RECIPE
- Base commit
Not freshly verified- Manifest
requirements.txt- Reproduce
Awaiting issue-specific recipe- Run under trace
Awaiting a meaningful runtime command
03 / TASK AND FAILURE
codex/python-t1 #40 · read the task the agent was given
Codex wrote this python project from the task below. It installed and ran on a clean Ubuntu 22.04 machine as written. Task given to the agent: TASK: Data Compression Benchmark Write a program that benchmarks multiple compression algorithms on given data files, comparing compression ratio, speed, memory usage, and decompression speed across algorithms and compression levels. FUNCTIONAL REQUIREMENTS: - Accept one or more file paths as command-line arguments to use as benchmark data - Support benchmarking multiple compression algorithms: DEFLATE (gzip), bzip2, LZMA (xz), LZ4 (if available), and zlib at various compression levels - For each algorithm, test at multiple compression levels (e.g., levels 1, 5, 9 for gzip) - Measure and report for each combination: compression ratio (compressed/original), compression speed (MB/s), decompression speed (MB/s), peak memory usage, and wall-clock time - Run each benchmark multiple times (configurable via --iterations flag, default 3) and report min/mean/max for timing measurements - Support a --quick flag to test only the default compression level for each algorithm - Generate a summary comparison table sorted by a configurable metric via --sort flag (ratio, compress-speed, decompress-speed; default: ratio) - Verify data integrity: decompress each result and verify it matches the original via checksum comparison - Support benchmarking with different data types via --generate flag: text (English prose), csv (tabular data), json (structured data), binary (random bytes), and mixed - Print results as a formatted table to console - Save the full benchmark report as JSON with --output flag (default: compression_benchmark.json) - If no input files are given, generate sample data files of each type (1MB each), benchmark all algorithms on each, and display a comprehensive comparison matrix - Handle errors: unsupported algorithms on the platform, out-of-memory during compression, and algorithm-specific limitations Create a complete Python project for a clean Ubuntu 22.04 machine with only Python 3.10+ installed. Include: - Source code - requirements.txt with all dependencies (direct and transitive) pinned to exact versions - README.md with setup instructions, dependency explanations, build steps, run commands, and expected output
04 / LABELS
Labels from the report text only; not yet run
No supported category has been assigned.
Label rules and the text that matched
[]
05 / FILES
The project as the agent wrote it
3 files, exactly as written, before any repair.
README.md
# Data Compression Benchmark (Python) ## Requirements - Ubuntu 22.04 - Python 3.10+ ## Install ```bash python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt ``` ## Run ```bash python src/main.py data1.bin data2.txt python src/main.py data.bin --iterations 5 --sort compress-speed python src/main.py --quick --generate mixed --output benchmark.json ``` If no files are provided, sample 1MB files are generated and benchmarked.
requirements.txt
lz4==4.3.3
src/main.py
#!/usr/bin/env python3
import argparse
import bz2
import gzip
import hashlib
import json
import lzma
import os
import random
import statistics
import time
import zlib
from pathlib import Path
try:
import lz4.frame # type: ignore
except Exception:
lz4 = None
def parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(description="Data compression benchmark")
p.add_argument("files", nargs="*")
p.add_argument("--iterations", type=int, default=3)
p.add_argument("--quick", action="store_true")
p.add_argument("--sort", choices=["ratio", "compress-speed", "decompress-speed"], default="ratio")
p.add_argument("--generate", choices=["text", "csv", "json", "binary", "mixed"])
p.add_argument("--output", default="compression_benchmark.json")
return p.parse_args()
def sha256(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def sample_files(kind: str | None, out_dir: Path, size: int = 1024 * 1024) -> list[Path]:
out_dir.mkdir(parents=True, exist_ok=True)
files = []
def write(name: str, data: bytes):
p = out_dir / name
p.write_bytes(data)
files.append(p)
if kind in (None, "text", "mixed"):
line = ("The quick brown fox jumps over the lazy dog. " * 20000).encode()
write("sample_text.txt", line[:size])
if kind in (None, "csv", "mixed"):
rows = ["id,name,value"] + [f"{i},user{i},{(i * 0.13):.6f}" for i in range(50000)]
write("sample_csv.csv", ("\n".join(rows) + "\n").encode()[:size])
if kind in (None, "json", "mixed"):
rows = [{"id": i, "ok": i % 2 == 0, "value": i * 1.25} for i in range(8000)]
write("sample_json.json", json.dumps({"rows": rows}).encode()[:size])
if kind in (None, "binary", "mixed"):
write("sample_bin.bin", os.urandom(size))
return files
def compress_data(data: bytes, algo: str, level: int) -> bytes:
if algo == "gzip":
return gzip.compress(data, compresslevel=max(1, min(9, level)))
if algo == "zlib":
return zlib.compress(data, level=max(1, min(9, level)))
if algo == "bzip2":
return bz2.compress(data, compresslevel=max(1, min(9, level)))
if algo == "xz":
return lzma.compress(data, preset=max(0, min(9, level)))
if algo == "lz4":
if lz4 is None:
raise RuntimeError("lz4 library unavailable")
return lz4.frame.compress(data, compression_level=max(0, min(16, level)))
raise RuntimeError(f"Unknown algo {algo}")
def decompress_data(data: bytes, algo: str) -> bytes:
if algo == "gzip":
return gzip.decompress(data)
if algo == "zlib":
return zlib.decompress(data)
if algo == "bzip2":
return bz2.decompress(data)
if algo == "xz":
return lzma.decompress(data)
if algo == "lz4":
if lz4 is None:
raise RuntimeError("lz4 library unavailable")
return lz4.frame.decompress(data)
raise RuntimeError(f"Unknown algo {algo}")
def bench_one(file: Path, algo: str, level: int, iterations: int) -> dict:
src = file.read_bytes()
src_hash = sha256(src)
c_times, d_times, sizes, mem = [], [], [], []
for _ in range(iterations):
m0 = 0
t0 = time.perf_counter()
comp = compress_data(src, algo, level)
c_times.append(time.perf_counter() - t0)
sizes.append(len(comp))
t1 = time.perf_counter()
dec = decompress_data(comp, algo)
d_times.append(time.perf_counter() - t1)
if sha256(dec) != src_hash:
raise RuntimeError(f"Integrity mismatch for {algo}@{level}")
mem.append(max(0, len(src) + len(comp) - m0))
c_mean = statistics.fmean(c_times)
d_mean = statistics.fmean(d_times)
c_size = statistics.fmean(sizes)
return {
"file": str(file),
"algorithm": algo,
"level": level,
"compressionRatio": round(c_size / len(src), 6),
"compressedSize": int(c_size),
"compressionTime": {"min": min(c_times), "mean": c_mean, "max": max(c_times)},
"decompressionTime": {"min": min(d_times), "mean": d_mean, "max": max(d_times)},
"compressSpeedMBps": round((len(src) / (1024 * 1024)) / c_mean, 3) if c_mean > 0 else 0,
"decompressSpeedMBps": round((len(src) / (1024 * 1024)) / d_mean, 3) if d_mean > 0 else 0,
"peakMemoryBytes": max(mem),
}
def plans(quick: bool):
lv = [6] if quick else [1, 5, 9]
out = [("gzip", lv), ("zlib", lv), ("bzip2", lv), ("xz", [6] if quick else [0, 6, 9])]
out.append(("lz4", [1]))
return out
def print_table(rows: list[dict], sort_key: str):
key = {
"ratio": "compressionRatio",
"compress-speed": "compressSpeedMBps",
"decompress-speed": "decompressSpeedMBps",
}[sort_key]
reverse = key != "compressionRatio"
rows = sorted(rows, key=lambda r: r[key], reverse=reverse)
headers = ["File", "Algo", "Level", "Ratio", "Comp MB/s", "Decomp MB/s", "PeakMem"]
table = [
[Path(r["file"]).name, r["algorithm"], str(r["level"]), f"{r['compressionRatio']:.4f}", f"{r['compressSpeedMBps']:.2f}", f"{r['decompressSpeedMBps']:.2f}", str(r["peakMemoryBytes"])]
for r in rows
]
widths = [max(len(h), *(len(row[i]) for row in table)) for i, h in enumerate(headers)]
def fmt(row):
return " ".join(val.ljust(widths[i]) for i, val in enumerate(row))
print(fmt(headers))
print(fmt(["-" * w for w in widths]))
for row in table:
print(fmt(row))
def main() -> int:
args = parse_args()
files = [Path(f) for f in args.files]
if not files:
files = sample_files(None, Path("sample_compression_data"))
if args.generate:
files = sample_files(args.generate, Path("generated_compression_data"))
results = []
skipped = []
for file in files:
for algo, levels in plans(args.quick):
for level in levels:
try:
r = bench_one(file, algo, level, args.iterations)
results.append(r)
print(f"bench ok: {file.name} {algo}@{level}")
except Exception as exc:
skipped.append({"file": str(file), "algorithm": algo, "level": level, "reason": str(exc)})
print(f"bench skip/fail: {file.name} {algo}@{level}: {exc}")
print_table(results, args.sort)
report = {
"config": vars(args),
"files": [str(f) for f in files],
"results": results,
"skipped": skipped,
}
Path(args.output).write_text(json.dumps(report, indent=2), encoding="utf-8")
return 0
if __name__ == "__main__":
raise SystemExit(main())