Files
mtusi2026lab4/scripts/track1_report.py
T
2026-10-21 21:22:42 +03:00

210 lines
8.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""Сводка трека 1: артефакты `evidence/track1/*` -> таблица и графики.
Читает `load-<label>.json` (tests/load.py) и `stats-<label>.csv` (docker stats)
и печатает метрики профиля: успешный RPS, p50/p95/p99 успешных запросов, доля
ошибок, суммарный CPU API, максимум памяти API, CPU PostgreSQL и LB.
.venv/bin/python scripts/track1_report.py
Графики (PNG) требуют matplotlib; без него печатается только таблица.
"""
import argparse
import csv
import json
import re
from collections import defaultdict
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_DIR = ROOT / 'evidence/track1'
# Стенд трека 1 — только контейнеры своего проекта: параллельно поднятый стенд
# другой лаборатории (тот же Docker-host) иначе попадал бы в те же колонки
# (mtusi-messenger-lab2-postgres-1 -> postgres) и искажал бюджет и CPU.
DEFAULT_PREFIX = 'mtusi-messenger-lab3-'
MEMORY_UNITS = {'B': 1 / 1024 / 1024, 'KiB': 1 / 1024, 'MiB': 1, 'GiB': 1024}
def parse_cpu(value):
return float(value.rstrip('%')) if value else 0.0
def parse_memory(value):
"""'123.4MiB / 256MiB' -> 123.4 MiB."""
match = re.match(r'([\d.]+)\s*([KMG]?i?B)', value.strip())
if not match:
return 0.0
return float(match.group(1)) * MEMORY_UNITS.get(match.group(2), 1)
def service_of(container):
"""mtusi-messenger-lab3-api-12 -> api"""
return container.rsplit('-', 1)[0].split('-')[-1] if '-' in container else container
def read_stats(path, prefix=''):
"""Профиль -> агрегаты по сервисам.
Внутри одного timestamp значения всех контейнеров сервиса складываются: две-три
реплики — это общий ресурс профиля, а не выборка. Затем CPU усредняется по
timestamp'ам, а память берётся пиковой (сумма по репликам).
"""
samples = defaultdict(lambda: defaultdict(lambda: {'cpu': 0.0, 'mem': 0.0}))
with path.open() as handle:
for row in csv.DictReader(handle):
if prefix and not row['container'].startswith(prefix):
continue
service = service_of(row['container'])
bucket = samples[service][row['timestamp']]
bucket['cpu'] += parse_cpu(row['cpu_percent'])
bucket['mem'] += parse_memory(row['mem_usage'])
cpu = {service: [values['cpu'] for values in by_timestamp.values()] for service, by_timestamp in samples.items()}
memory = {service: max(values['mem'] for values in by_timestamp.values()) for service, by_timestamp in samples.items()}
return cpu, memory
def aggregate(label, directory, prefix=''):
load_path = directory / f'load-{label}.json'
stats_path = directory / f'stats-{label}.csv'
profile_path = directory / f'profile-{label}.json'
if not load_path.is_file() or not stats_path.is_file():
return None
report = json.loads(load_path.read_text())
profile = json.loads(profile_path.read_text()) if profile_path.is_file() else {}
cpu, memory = read_stats(stats_path, prefix)
api_cpu = cpu.get('api', [])
api_memory = memory.get('api', 0.0)
def average(values):
return round(sum(values) / len(values), 2) if values else None
sampled = max((len(values) for values in cpu.values()), default=0)
return {
'label': label,
'replicas': profile.get('replicas', 0),
'total_cpu': profile.get('total_cpu'),
'postgres_budget': '{}/{}'.format(
profile.get('postgres_cpu_limit', '?'), profile.get('postgres_mem_limit', '?')
),
'postgres_max_conns': profile.get('postgres_max_conns', '?'),
'lb_cpu_limit': profile.get('lb_cpu_limit', '?'),
'cpu_per_replica': round(profile['total_cpu'] / max(1, profile['replicas']), 2)
if profile.get('total_cpu') else None,
'successful_rps': report['successful_rps'],
'p50_ms': report['successful_latency_ms']['p50'],
'p95_ms': report['successful_latency_ms']['p95'],
'p99_ms': report['successful_latency_ms']['p99'],
'errors': report['failed_requests'],
'error_ratio': report['error_ratio'],
'api_cpu_sum': average(api_cpu),
'api_cpu_per_replica': round(average(api_cpu) / max(1, profile.get('replicas') or 1), 2)
if api_cpu else None,
'api_memory_max_sum_mib': round(api_memory, 1) if api_memory else None,
'postgres_cpu': average(cpu.get('postgres', [])),
'lb_cpu': average(cpu.get('lb', [])),
'stats_samples': sampled,
'load_seconds': report['elapsed_seconds'],
}
def markdown(rows):
header = (
'| профиль | реплик | бюджет CPU API | CPU/реплика | бюджет PG CPU/RAM | пул | LB CPU (лимит) | успешный RPS | '
'p50 мс | p95 мс | p99 мс | ошибки | API CPU (сумма) | API RAM max (сумма, MiB) | PG CPU | LB CPU |\n'
'| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n'
)
lines = [
'| {label} | {replicas} | {total_cpu} | {cpu_per_replica} | {postgres_budget} | {postgres_max_conns} | '
'{lb_cpu_limit} | {successful_rps} | {p50_ms} | {p95_ms} | {p99_ms} | '
'{errors} | {api_cpu_sum} | {api_memory_max_sum_mib} | {postgres_cpu} | {lb_cpu} |'.format(**row)
for row in rows
]
return header + '\n'.join(lines) + '\n'
def plot(rows, directory):
try:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
except ImportError:
print('matplotlib недоступен: графики не построены (pip install matplotlib)')
return
series = {
'fixed-budget': [row for row in rows if row['label'].startswith('r') and row['total_cpu']],
'growth': [row for row in rows if row['label'].startswith('g') and row['total_cpu']],
}
for name, points in series.items():
if len(points) < 2:
continue
axis = 'replicas' if name == 'fixed-budget' else 'total_cpu'
points = sorted(points, key=lambda row: row[axis])
ticks = [str(row[axis]) for row in points]
figure, (left, right) = plt.subplots(1, 2, figsize=(10, 4))
left.plot(ticks, [row['successful_rps'] for row in points], marker='o')
left.set_xlabel('API реплик' if axis == 'replicas' else 'суммарный бюджет CPU')
left.set_ylabel('успешный RPS')
left.grid(True)
left.set_title('Пропускная способность')
right.plot(ticks, [row['p95_ms'] for row in points], marker='o', color='tab:red')
right.set_xlabel('API реплик' if axis == 'replicas' else 'суммарный бюджет CPU')
right.set_ylabel('p95 успешных, мс')
right.grid(True)
right.set_title('Задержка p95')
figure.suptitle(('Фиксированный бюджет' if name == 'fixed-budget' else 'Рост ресурсов') + ' (трек 1)')
figure.tight_layout()
target = directory / ('scaling-' + name + '.png')
figure.savefig(target, dpi=120)
print('график:', target)
def main():
parser = argparse.ArgumentParser(description='Сводка трека 1 по артефактам стенда')
parser.add_argument('--dir', type=Path, default=DEFAULT_DIR)
parser.add_argument('--prefix', default=DEFAULT_PREFIX, help='префикс контейнеров своего стенда')
args = parser.parse_args()
labels = sorted(
path.stem.removeprefix('load-') for path in args.dir.glob('load-*.json')
)
rows = []
for label in labels:
row = aggregate(label, args.dir, args.prefix)
if row is None:
print('внимание: профиль ' + label + ' пропущен — нет load-*.json или stats-*.csv')
continue
rows.append(row)
if not rows:
raise SystemExit('нет артефактов в ' + str(args.dir))
table = markdown(rows)
print(table)
(args.dir / 'summary.md').write_text('# Трек 1: цена масштабирования\n\n' + table)
plot(rows, args.dir)
if __name__ == '__main__':
main()