Реализовать лабораторную работу 5

This commit is contained in:
Никита
2026-11-10 10:57:36 +03:00
parent 276dbdad8a
commit 8c87c60621
124 changed files with 15887 additions and 27 deletions
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
root="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
exec docker compose --project-name mtusi2026lab5 --project-directory "$root" "$@"
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env python3
from pathlib import Path
import secrets,json,os
os.umask(0o077)
p=Path(__file__).resolve().parents[1]/'secrets';p.mkdir(exist_ok=True)
def secret(name,size=24):
f=p/name
if not f.exists(): f.write_text(secrets.token_hex(size))
return f.read_text().strip()
pw=secret('postgres_password')
f=p/'database_url'
if not f.exists(): f.write_text('Host=postgres;Port=5432;Database=messenger;Username=messenger;Password='+pw)
admin=secret('s3_admin_access',12); admin_secret=secret('s3_admin_secret')
access=secret('s3_access',12); key=secret('s3_secret')
config={'identities':[
{'name':'bootstrap','credentials':[{'accessKey':admin,'secretKey':admin_secret}],'actions':['Admin','Read','Write','List','Tagging']},
{'name':'messenger','credentials':[{'accessKey':access,'secretKey':key}],'actions':['Read:messenger','Write:messenger','List:messenger']}]}
(p/'s3_config').write_text(json.dumps(config))
print('Local secrets ready')
broker_password=secret('broker_password')
(p/'broker.conf').write_text('default_user = messenger\ndefault_pass = '+broker_password+'\ndefault_vhost = messenger\n')
(p.parent/'.local/faults').mkdir(parents=True,exist_ok=True)
(p.parent/'.local/faults').chmod(0o777)
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
set -eu
exec python3 "$(dirname "$0")/init-secrets.py"
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env python3
from pathlib import Path
import json,statistics
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
ROOT=Path(__file__).resolve().parents[1]
runs=json.loads((ROOT/'evidence/overload.json').read_text())['runs']
fig,axes=plt.subplots(1,3,figsize=(13,4.3),layout='constrained')
settings=[1,2,4]
for label,color in [('small','#4b6fba'),('large','#719036')]:
for ax,key,title,unit in zip(axes,['throughput','p95_ready_seconds','peak_rss_bytes'],['Скорость обработки','Время до ready: p95 batch','Память процессов worker'],['готовых файлов/с','секунды','MiB']):
groups=[[r[key]/(1048576 if key=='peak_rss_bytes' else 1) for r in runs if r['image']==label and r['concurrency']==n] for n in settings]
mid=[statistics.median(g) for g in groups]
ax.errorbar(range(3),mid,yerr=[[m-min(g) for m,g in zip(mid,groups)],[max(g)-m for m,g in zip(mid,groups)]],color=color,marker='o',capsize=4,label=label)
ax.set_xticks(range(3),['1 / 1','2 / 2','4 / 4']);ax.set_xlabel('Concurrency / prefetch');ax.set_ylabel(unit);ax.set_title(title,fontsize=11);ax.set_ylim(bottom=0);ax.grid(alpha=.2)
for ax,key in zip(axes,['throughput','p95_ready_seconds','peak_rss_bytes']):
ax.set_ylim(0,max(r[key] for r in runs)/(1048576 if key=='peak_rss_bytes' else 1)*1.12)
axes[0].legend(title='Размер PNG')
fig.suptitle('Lab5: одинаковые 1 CPU / 1024 MiB worker; медианы и диапазон 3 повторов',fontsize=12)
fig.savefig(ROOT/'evidence/overload.png',dpi=170)
fig.savefig(ROOT/'evidence/overload.svg')
print('Saved evidence/overload.png and overload.svg')
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
./scripts/compose.sh restart postgres redis s3 broker
./scripts/compose.sh up -d --wait --wait-timeout 60 postgres redis s3 broker
./scripts/compose.sh restart api lb dispatcher worker
./scripts/compose.sh up -d --wait --wait-timeout 60 api lb dispatcher worker
+13
View File
@@ -0,0 +1,13 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
test -n "${TARGET_INSTANCE_ID:-}" || { echo "TARGET_INSTANCE_ID is required" >&2; exit 1; }
for id in $(./scripts/compose.sh ps -q api); do
instance="$(docker exec "$id" curl -fsS -D - -o /dev/null http://127.0.0.1:8080/health/live | tr -d '\r' | awk 'tolower($1) == "x-instance-id:" { print $2 }')"
if [ "$instance" = "$TARGET_INSTANCE_ID" ]; then
docker stop --time 8 "$id" >/dev/null
exit 0
fi
done
echo "Requested API instance was not found" >&2
exit 1