fix(lab3): verify fixed-resource scaling and prepare submission

This commit is contained in:
kolya5544
2026-11-11 17:56:40 +03:00
parent 7e6f66c3db
commit d1352c12f0
10 changed files with 1351 additions and 13 deletions
+2 -2
View File
@@ -22,7 +22,7 @@ def main():
fig, ax = plt.subplots(1, 3, figsize=(17, 5))
# A: фиксированный суммарный CPU
for tag, color, shift in (("base", "#1565c0", -0.2), ("improved", "#2e7d32", 0.2)):
d = load(out, "A", tag)
d = load(out, "A", tag + "_fixed_resources")
if not d:
continue
names = [r["config"] for r in d["results"]]
@@ -30,7 +30,7 @@ def main():
label={"base": "до (несколько запросов)", "improved": "после (функция БД)"}[tag])
ax[0].set_xticks(range(len(names)))
ax[0].set_xticklabels(["%s" % n.replace("x", " × ") + " CPU" for n in names])
ax[0].set_title("A: суммарно 2.0 CPU на API (число реплик × CPU)"); ax[0].set_ylabel("успешных запросов/с"); ax[0].legend()
ax[0].set_title("A: суммарно 2 CPU / 1024 MiB RAM на API"); ax[0].set_ylabel("успешных запросов/с"); ax[0].legend()
# B: рост ресурсов
for tag, color, label in (("base", "#1565c0", "БД 2 CPU, до"), ("base_pg4", "#ef6c00", "БД 4 CPU, до"), ("improved", "#2e7d32", "БД 2 CPU, после")):
d = load(out, "B", tag)
+2 -2
View File
@@ -9,10 +9,10 @@ set_strategy() {
sleep 12
}
set_strategy multi
$PY research/scaling.py --series A --concurrency 64 --tag base --out evidence
$PY research/scaling.py --series A --concurrency 64 --tag base_fixed_resources --out evidence
$PY research/scaling.py --series B --concurrency 64 --tag base --out evidence
$PY research/scaling.py --series B --concurrency 64 --pg-cpus 4 --tag base_pg4 --out evidence
set_strategy function
$PY research/scaling.py --series A --concurrency 64 --tag improved --out evidence
$PY research/scaling.py --series A --concurrency 64 --tag improved_fixed_resources --out evidence
$PY research/scaling.py --series B --concurrency 64 --tag improved --out evidence
echo ALL-DONE
+11 -5
View File
@@ -29,15 +29,15 @@ BASE = "http://127.0.0.1:8080"
FILTER = ["--filter", "label=com.docker.compose.project=mtusi-lab3"]
def sh(*args, check=False):
return subprocess.run(args, cwd=ROOT, capture_output=True, text=True, env=dict(os.environ, **{"PYTHONUTF8": "1"})).stdout.strip()
def sh(*args, check=True):
return subprocess.run(args, cwd=ROOT, capture_output=True, text=True, check=check, env=dict(os.environ, **{"PYTHONUTF8": "1"})).stdout.strip()
def containers(service):
return sh("docker", "ps", "-q", *FILTER, "--filter", "label=com.docker.compose.service=" + service).split()
def configure(replicas, api_cpus, pg_cpus, lb_cpus):
def configure(replicas, api_cpus, pg_cpus, lb_cpus, api_memory_mib=256):
sh("docker", "compose", "--env-file", ".env", "up", "-d", "--no-deps", "--no-recreate", "--scale", "api=%d" % replicas, "api")
# лишние реплики (если было больше) удаляем
ids = containers("api")
@@ -45,7 +45,11 @@ def configure(replicas, api_cpus, pg_cpus, lb_cpus):
sh("docker", "stop", "--time", "5", extra)
sh("docker", "rm", extra)
for cid in containers("api"):
sh("docker", "update", "--cpus", str(api_cpus), "--memory", "256m", "--memory-swap", "256m", cid)
memory = "%dm" % api_memory_mib
sh("docker", "update", "--cpus", str(api_cpus), "--memory", memory, "--memory-swap", memory, cid)
limits = json.loads(sh("docker", "inspect", "-f", "{{json .HostConfig}}", cid))
assert limits["Memory"] == api_memory_mib * 1024 * 1024
assert limits["NanoCpus"] == int(api_cpus * 1_000_000_000)
for cid in containers("postgres"):
sh("docker", "update", "--cpus", str(pg_cpus), "--memory", "512m", "--memory-swap", "512m", cid)
for cid in containers("lb"):
@@ -116,7 +120,8 @@ class Sampler(threading.Thread):
def run_config(name, replicas, api_cpus, pg_cpus, lb_cpus, args, stand_data):
configure(replicas, api_cpus, pg_cpus, lb_cpus)
api_memory_mib = 1024 // replicas if args.series == "A" else 256
configure(replicas, api_cpus, pg_cpus, lb_cpus, api_memory_mib)
# прогрев
t0 = time.time()
with mp.get_context("spawn").Pool(args.procs) as pool:
@@ -139,6 +144,7 @@ def run_config(name, replicas, api_cpus, pg_cpus, lb_cpus, args, stand_data):
if e[1] == "POST" and e[4]:
instances[e[4]] = instances.get(e[4], 0) + 1
out = {"config": name, "replicas": replicas, "api_cpus_each": api_cpus, "api_cpus_total": round(replicas * api_cpus, 2),
"api_memory_mib_each": api_memory_mib, "api_memory_mib_total": api_memory_mib * replicas,
"postgres_cpus": pg_cpus, "lb_cpus": lb_cpus, "concurrency": args.concurrency, "client_processes": args.procs,
"post": post, "get": get, "total_rps": round((post.get("ok", 0) + get.get("ok", 0)) / args.seconds, 1),
"replica_share": sorted(instances.values()), "docker_stats": sampler.summary(t_start + 2, t_end)}