#!/usr/bin/env python3 """Собирает сводную таблицу из evidence/track1/*.json (open-loop).""" import csv import json import re from pathlib import Path ROOT = Path(__file__).resolve().parents[1] EVID = ROOT / 'evidence' / 'track1' def parse_stats(path): """Возвращает {'api': {...sum...}, 'postgres': {...}, 'lb': ..., 'redis': ...}.""" agg = {} for line in path.read_text().splitlines(): # формат: % / m = re.match( r'^(\S+)\s+([0-9.]+)%\s+([0-9.]+)\s*(B|KiB|MiB|GiB)\s*/', line, ) if not m: continue name, cpup, memv, memunit = m.groups() role = None if '-api-' in name or name.endswith('-api-1') or re.search(r'-api-\d+$', name): role = 'api' elif '-postgres-' in name: role = 'postgres' elif '-redis-' in name: role = 'redis' elif '-lb-' in name: role = 'lb' if role is None: continue mult = {'B': 1/1024/1024, 'KiB': 1/1024, 'MiB': 1, 'GiB': 1024}[memunit] mem_mib = float(memv) * mult slot = agg.setdefault(role, {'cpu_max': 0.0, 'cpu_sum': 0.0, 'mem_mib': 0.0}) slot['cpu_max'] = max(slot['cpu_max'], float(cpup)) slot['cpu_sum'] += float(cpup) slot['mem_mib'] = max(slot['mem_mib'], mem_mib) return agg def main(): rows = [] for path in sorted(EVID.glob('*-rps*.json')): # имя: A1-rps100.json stem = path.stem profile, _, rps_part = stem.rpartition('-rps') rps = int(rps_part) r = json.loads(path.read_text()) # stats.txt и pg.txt могут отсутствовать (если снимали отдельно). stats = path.with_name(stem + '-stats.txt') agg = parse_stats(stats) if stats.exists() else {} api = agg.get('api', {'cpu_sum': 0.0, 'cpu_max': 0.0, 'mem_mib': 0.0}) pg = agg.get('postgres', {'cpu_max': 0.0, 'mem_mib': 0.0}) lb = agg.get('lb', {'cpu_max': 0.0}) redis = agg.get('redis', {'cpu_max': 0.0}) rows.append({ 'profile': profile, 'offered_rps': rps, 'successful_rps': r.get('successful_rps'), 'error_ratio_pct': round((r.get('error_ratio') or 0) * 100, 3), 'p50_ms': r['successful_latency_ms']['p50'], 'p95_ms': r['successful_latency_ms']['p95'], 'p99_ms': r['successful_latency_ms']['p99'], 'api_cpu_sum_max': round(api['cpu_sum'], 2), 'api_cpu_peak': round(api['cpu_max'], 2), 'api_mem_mib': round(api['mem_mib'], 1), 'pg_cpu_max': round(pg['cpu_max'], 2), 'pg_mem_mib': round(pg['mem_mib'], 1), 'lb_cpu_max': round(lb['cpu_max'], 2), 'redis_cpu_max': round(redis['cpu_max'], 2), }) if not rows: print('no json files in', EVID) return out_csv = ROOT / 'evidence' / 'track1-summary.csv' with out_csv.open('w', newline='') as f: w = csv.DictWriter(f, fieldnames=list(rows[0].keys())) w.writeheader() w.writerows(rows) print(out_csv.read_text()) print('written:', out_csv) if __name__ == '__main__': main()