Files

119 lines
4.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""aggregate.py — RSI round harness: parse bench/<tag>-*.txt logs into
results/bench-<tag>.tsv and print overall + per-type summaries.
Usage: aggregate.py <tag>
Reads files.tsv (name, rel path, type) and reference.tsv + reference2.tsv
(xz -6 ratios) from the repo dir (copies of /root/fftdict-shared/results/).
Ratios and speeds are size-weighted (bytes), matching the fftdict reports.
"""
import os, sys, csv
from collections import defaultdict
TAG = sys.argv[1]
HERE = os.path.dirname(os.path.abspath(__file__))
def parse_log(path):
d = {}
try:
with open(path) as f:
for line in f:
if ':' in line:
k, v = line.split(':', 1)
d[k.strip()] = v.strip()
except FileNotFoundError:
return None
if d.get('roundtrip') != 'PASS':
return None
def num(k, default=0.0):
try: return float(d.get(k, default))
except ValueError: return default
return {
'ratio': num('ratio'), 'enc': num('encode_MBps'), 'dec': num('decode_MBps'),
'in': int(float(d.get('input_bytes', 0))), 'out': int(float(d.get('output_bytes', 0))),
'tokens': int(float(d.get('tokens', 0))),
'dict_entries': int(float(d.get('dict_entries', 0))),
'table_bytes': int(float(d.get('table_bytes', 0))),
'distinct': int(float(d.get('distinct_tokens', 0))),
'lit_runs': int(float(d.get('lit_runs', 0))),
'lit_bytes': int(float(d.get('lit_bytes', 0))),
}
files = []
with open(os.path.join(HERE, 'files.tsv')) as f:
for line in f:
line = line.rstrip('\n')
if not line or line.startswith('#'):
continue
name, rel, typ = line.split('\t')
files.append((name, rel, typ))
xz = {}
for ref in ('reference.tsv', 'reference2.tsv'):
p = os.path.join(HERE, ref)
if not os.path.exists(p):
continue
with open(p) as f:
r = csv.DictReader(f, delimiter='\t')
for row in r:
if row.get('compressor') == 'xz -6':
xz[row['file']] = float(row['ratio'])
rows = []
missing = []
for name, rel, typ in files:
r = parse_log(os.path.join(HERE, 'bench', f'{TAG}-{name}.txt'))
if r is None:
missing.append(name)
continue
r['name'] = name; r['type'] = typ; r['xz'] = xz.get(name)
rows.append(r)
if missing:
print(f'MISSING/FAIL: {len(missing)}: {", ".join(missing)}', file=sys.stderr)
out_tsv = os.path.join(HERE, 'results', f'bench-{TAG}.tsv')
os.makedirs(os.path.join(HERE, 'results'), exist_ok=True)
with open(out_tsv, 'w') as f:
f.write('file\ttype\tinput_bytes\toutput_bytes\tratio\txz_ratio\tenc_MBps\tdec_MBps\ttokens\tdict_entries\ttable_bytes\tdistinct_tokens\tlit_runs\tlit_bytes\n')
for r in rows:
xz_s = f"{r['xz']:.4f}" if r['xz'] is not None else ''
f.write(f"{r['name']}\t{r['type']}\t{r['in']}\t{r['out']}\t{r['ratio']:.4f}\t{xz_s}\t"
f"{r['enc']:.2f}\t{r['dec']:.2f}\t{r['tokens']}\t{r['dict_entries']}\t"
f"{r['table_bytes']}\t{r['distinct']}\t{r['lit_runs']}\t{r['lit_bytes']}\n")
def summary(rs):
tin = sum(r['in'] for r in rs)
tout = sum(r['out'] for r in rs)
ratio = tout / tin if tin else 0.0
# size-weighted speeds: sum(bytes)/sum(bytes/rate)
enc_t = sum(r['in'] / r['enc'] for r in rs if r['enc'] > 0)
dec_t = sum(r['in'] / r['dec'] for r in rs if r['dec'] > 0)
enc = tin / enc_t if enc_t else 0.0
dec = tin / dec_t if dec_t else 0.0
xzr = None
xzp = [(r['in'], r['xz']) for r in rs if r['xz'] is not None]
if xzp:
xzr = sum(n * x for n, x in xzp) / sum(n for n, _ in xzp)
return len(rs), tin, ratio, xzr, enc, dec
n, tin, ratio, xzr, enc, dec = summary(rows)
print(f'tag: {TAG}')
print(f'files: {n}/44 input_bytes: {tin}')
print(f'OVERALL ratio: {ratio:.4f}' + (f' xz -6: {xzr:.4f} gap: {(ratio / xzr - 1) * 100:+.1f}%' if xzr else ''))
print(f'OVERALL enc: {enc:.1f} MB/s dec: {dec:.1f} MB/s (size-weighted)')
print(f'total lit_bytes: {sum(r["lit_bytes"] for r in rows)} lit_runs: {sum(r["lit_runs"] for r in rows)}')
print()
print(f'{"type":<16} {"n":>2} {"ratio":>7} {"xz":>7} {"gap%":>7} {"enc":>6} {"dec":>6}')
by_type = defaultdict(list)
for r in rows:
by_type[r['type']].append(r)
for typ in sorted(by_type):
rs = by_type[typ]
nn, _, rr, xx, ee, dd = summary(rs)
gap = f'{(rr / xx - 1) * 100:+.0f}%' if xx else ''
xzs = f'{xx:.4f}' if xx else ''
print(f'{typ:<16} {nn:>2} {rr:>7.4f} {xzs:>7} {gap:>7} {ee:>6.1f} {dd:>6.1f}')
print()
print(f'per-file TSV: results/bench-{TAG}.tsv')