53 lines
5.5 KiB
Python
53 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Render source JSON as a reproducible scientific figure and a compact table."""
|
|
import csv,json,statistics
|
|
from collections import defaultdict
|
|
from pathlib import Path
|
|
import matplotlib
|
|
matplotlib.use('Agg')
|
|
import matplotlib.pyplot as plt
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
report=json.loads((ROOT/'evidence/scaling.json').read_text());groups=defaultdict(list)
|
|
for run in report['runs']:groups[(run['series'],run['replicas'],run['concurrency'],run.get('pool_per_api',10))].append(run)
|
|
summary=[]
|
|
for (series,n,clients,pool),rows in sorted(groups.items()):
|
|
result={'series':series,'replicas':n,'clients':clients,'pool_per_api':pool,'repeats':len(rows)}
|
|
for field in ['successful_rps','error_fraction','p50_ms','p95_ms','p99_ms']:result[field]=statistics.median(r[field] for r in rows)
|
|
summary.append(result)
|
|
with (ROOT/'evidence/scaling-summary.csv').open('w') as f:
|
|
w=csv.DictWriter(f,fieldnames=list(summary[0]));w.writeheader();w.writerows(summary)
|
|
plt.rcParams.update({'font.family':'DejaVu Sans','font.size':10,'axes.spines.top':False,'axes.spines.right':False})
|
|
fig,axes=plt.subplots(2,2,figsize=(11,7),layout='constrained')
|
|
colors={1:'#147d92',2:'#d27925',4:'#814eac'}
|
|
for col,series in enumerate(['fixed','growing']):
|
|
for n in [1,2,4]:
|
|
rows=[r for r in summary if r['series']==series and r['replicas']==n]
|
|
axes[0,col].plot([r['clients'] for r in rows],[r['successful_rps'] for r in rows],marker='o',color=colors[n],label=f'{n} API')
|
|
axes[1,col].plot([r['clients'] for r in rows],[r['p99_ms'] for r in rows],marker='o',color=colors[n])
|
|
axes[0,col].set_title('Fixed: total API 2 CPU / 1024 MiB' if series=='fixed' else 'Growing: 1 CPU / 512 MiB per API')
|
|
axes[0,col].set_ylabel('Successful responses / s');axes[0,col].legend(frameon=False)
|
|
axes[1,col].set_ylabel('p99 of successful responses, ms');axes[1,col].set_xlabel('Concurrent clients')
|
|
for ax in axes[:,col]:ax.set_xticks([4,16,64]);ax.grid(alpha=.2)
|
|
fig.suptitle('Messenger POST scaling — medians of two 6 s runs, 16 chats',fontsize=14)
|
|
fig.savefig(ROOT/'evidence/scaling.png',dpi=180)
|
|
fig.savefig(ROOT/'evidence/scaling.pdf')
|
|
lines=['# Масштабирование: измеренные результаты','', 'Raw: `scaling.json`. График: `scaling.png`; таблица: `scaling-summary.csv`.', '', 'Медианы двух 6-секундных прогонов после 2 s прогрева на каждую конкуренцию. PostgreSQL2 CPU/512MiB, LB1 CPU/128MiB. 16 чатов, 4/16/64 closed-loop клиента, reusable HTTP connections; без retry. Процентили только успешных HTTP201; ошибки отдельно. Каждый point содержит raw cgroups и samples PostgreSQL.','', '| Серия | API | Клиенты | RPS | Ошибки % | p50 ms | p95 ms | p99 ms |','|---|---:|---:|---:|---:|---:|---:|---:|']
|
|
for r in summary:
|
|
lines.append(f"| {r['series']}"+(f" pool={r['pool_per_api']}" if r['series']=='pool_improvement' else '')+f" | {r['replicas']} | {r['clients']} | {r['successful_rps']:.1f} | {100*r['error_fraction']:.3f} | {r['p50_ms']:.3f} | {r['p95_ms']:.3f} | {r['p99_ms']:.3f} |")
|
|
lines += ['','## Ресурсы и ожидания','', '| Серия | API | Клиенты | CPU API sum % | CPU PostgreSQL % | CPU LB % | API max cgroup peak MiB |','|---|---:|---:|---:|---:|---:|---:|']
|
|
for (series,n,clients,pool),runs in sorted(groups.items()):
|
|
if series=='pool_improvement':continue
|
|
def cpu(service):return statistics.median(sum(v['cpu_percent'] for v in r['cgroup_after'].values() if v['service']==service) for r in runs)
|
|
peak=max(v['memory_peak'] for r in runs for v in r['cgroup_after'].values() if v['service']=='api')/2**20
|
|
lines.append(f'| {series} | {n} | {clients} | {cpu("api"):.1f} | {cpu("postgres"):.1f} | {cpu("lb"):.1f} | {peak:.1f} |')
|
|
lines += ['','CPU100%=один core. CPU измерен за окно snapshots, включающее небольшую инструментальную накладную стоимость, не только ровно6s. memory.peak накоплен с запуска контейнера, current до/после тоже сохранён. Pg_stat_activity samples позволяют наблюдать WAL/locks/CPU, но не являются непрерывным профилем.','']
|
|
for pool,rows in report.get('pool_diagnostics',{}).items():
|
|
# Each instance emits cumulative counters; retain the final sample per instance
|
|
# when available; otherwise show conservative maxima rather than sum duplicates.
|
|
last={}
|
|
for row in rows:last[row.get('instance','unknown')]=row
|
|
for instance,row in last.items():lines.append(f"- Pool{pool}, instance{instance}: acquires={row['acquires']}, empty_acquires={row['empty_acquires']}, accumulated empty wait={row['wait_ns']/1e9:.3f}s (process lifetime counters).")
|
|
lines += ['','## Границы измерения','', 'Короткие прогоны на одном host, warm caches, fixed ordering, растущие tables и coordinated omission. Предельные задержки во время перегрузки могут быть недооценены closed-loop моделью. Токены только в памяти и stdin генератора, в evidence не сохраняются. SQL/RAM/WAL и работа нескольких процессов ограничивают масштабирование; выводы относятся к измеренному POST-профилю.']
|
|
(ROOT/'evidence/scaling-summary.md').write_text('\n'.join(lines)+'\n')
|
|
print('Wrote PNG/PDF/CSV/Markdown from raw evidence')
|