level 5 track 1

This commit is contained in:
2026-11-08 13:13:34 +03:00
parent eb4e00e412
commit 637d45c820
9 changed files with 1205 additions and 27 deletions
+511
View File
@@ -0,0 +1,511 @@
{
"hypothesis": "With a fixed total API CPU/RAM budget (2.0 CPU / 512M), two replicas outperform both one (a single uvicorn worker cannot spend 2.0 CPU) and four (each worker is capped at 0.5 CPU, p95 collapses). Growing per-replica CPU still raises RPS until that slice is no longer tiny. PostgreSQL and HAProxy stay below their limits; extra replicas past 2 only add pool connections to the same 1.0-CPU database. The confirmed improvement is 2×1.0 instead of 4×0.5 at the same budget.",
"host": {
"ncpu": 12,
"memory_bytes": 12445638656
},
"profile": {
"concurrency": 24,
"warmup_seconds": 5,
"measure_seconds": 20,
"workload": "closed-loop POST /api/v1/chats/{id}/messages through HAProxy",
"fixed_budget": "API total 2.0 CPU / 512M; postgres 1.0 CPU / 512M; pool 10 per replica"
},
"runs": [
{
"id": "fixed-1",
"series": "fixed",
"replicas": 1,
"api_cpus": "2.0",
"api_memory": "512M",
"postgres_cpus": "1.0",
"pool": 10,
"note": "1 replica takes the whole 2.0 CPU / 512M budget",
"inspected_limits": [
{
"name": "messenger-lab3-api-1",
"service": "api",
"cpus": 2.0,
"memory_mb": 512
},
{
"name": "messenger-lab3-lb-1",
"service": "lb",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-migrate-1",
"service": "migrate",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-postgres-1",
"service": "postgres",
"cpus": 1.0,
"memory_mb": 512
}
],
"metrics": {
"successful_requests": 3750,
"failed_requests": 0,
"error_ratio": 0.0,
"rps": 187.5,
"p50_ms": 122.2,
"p95_ms": 203.6,
"p99_ms": 267.2,
"instances": {
"74b3971ded9d": 3750
},
"instance_count": 1
},
"resources": {
"api_cpu_sum_pct": 98.52,
"api_cpu_each_pct": {
"messenger-lab3-api-1": 98.52
},
"api_util_pct": 49.3,
"postgres_cpu_pct": 46.8,
"postgres_util_pct": 46.8,
"lb_cpu_pct": 19.88,
"lb_util_pct": 39.8,
"pg_backends_mean": 11.0,
"pg_active_mean": 1.38,
"pg_waiting_mean": 0.12,
"pool_capacity": 10,
"pool_util_pct": 110.0
},
"saturated": "pool",
"saturation_scores": {
"postgres": 46.8,
"lb": 39.8,
"api": 49.3,
"pool": 110.0
},
"samples": 8
},
{
"id": "fixed-2",
"series": "fixed",
"replicas": 2,
"api_cpus": "1.0",
"api_memory": "256M",
"postgres_cpus": "1.0",
"pool": 10,
"note": "baseline: 2 × 1.0 CPU / 256M (also grow-1.00)",
"inspected_limits": [
{
"name": "messenger-lab3-api-1",
"service": "api",
"cpus": 1.0,
"memory_mb": 256
},
{
"name": "messenger-lab3-api-2",
"service": "api",
"cpus": 1.0,
"memory_mb": 256
},
{
"name": "messenger-lab3-lb-1",
"service": "lb",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-migrate-1",
"service": "migrate",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-postgres-1",
"service": "postgres",
"cpus": 1.0,
"memory_mb": 512
}
],
"metrics": {
"successful_requests": 4990,
"failed_requests": 0,
"error_ratio": 0.0,
"rps": 249.5,
"p50_ms": 87.4,
"p95_ms": 196.7,
"p99_ms": 285.1,
"instances": {
"44ffd5fcad43": 2501,
"b1551f2692b4": 2489
},
"instance_count": 2
},
"resources": {
"api_cpu_sum_pct": 170.27,
"api_cpu_each_pct": {
"messenger-lab3-api-1": 83.81,
"messenger-lab3-api-2": 86.46
},
"api_util_pct": 85.1,
"postgres_cpu_pct": 66.23,
"postgres_util_pct": 66.2,
"lb_cpu_pct": 23.3,
"lb_util_pct": 46.6,
"pg_backends_mean": 21.0,
"pg_active_mean": 2.0,
"pg_waiting_mean": 0.75,
"pool_capacity": 20,
"pool_util_pct": 105.0
},
"saturated": "pool",
"saturation_scores": {
"postgres": 66.2,
"lb": 46.6,
"api": 85.1,
"pool": 105.0
},
"samples": 8
},
{
"id": "fixed-4",
"series": "fixed",
"replicas": 4,
"api_cpus": "0.5",
"api_memory": "128M",
"postgres_cpus": "1.0",
"pool": 10,
"note": "4 replicas, same 2.0 CPU / 512M total",
"inspected_limits": [
{
"name": "messenger-lab3-api-1",
"service": "api",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-api-2",
"service": "api",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-api-3",
"service": "api",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-api-4",
"service": "api",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-lb-1",
"service": "lb",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-migrate-1",
"service": "migrate",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-postgres-1",
"service": "postgres",
"cpus": 1.0,
"memory_mb": 512
}
],
"metrics": {
"successful_requests": 3717,
"failed_requests": 0,
"error_ratio": 0.0,
"rps": 185.85,
"p50_ms": 44.6,
"p95_ms": 464.5,
"p99_ms": 612.5,
"instances": {
"6b3abbff31a1": 925,
"827845da8b75": 926,
"7efb091f3c55": 925,
"16b68cf38bcd": 941
},
"instance_count": 4
},
"resources": {
"api_cpu_sum_pct": 165.5,
"api_cpu_each_pct": {
"messenger-lab3-api-1": 41.19,
"messenger-lab3-api-2": 40.7,
"messenger-lab3-api-3": 39.44,
"messenger-lab3-api-4": 44.17
},
"api_util_pct": 82.8,
"postgres_cpu_pct": 61.7,
"postgres_util_pct": 61.7,
"lb_cpu_pct": 18.79,
"lb_util_pct": 37.6,
"pg_backends_mean": 39.38,
"pg_active_mean": 1.38,
"pg_waiting_mean": 0.38,
"pool_capacity": 40,
"pool_util_pct": 98.5
},
"saturated": "pool",
"saturation_scores": {
"postgres": 61.7,
"lb": 37.6,
"api": 82.8,
"pool": 98.5
},
"samples": 8
},
{
"id": "grow-0.25",
"series": "grow",
"replicas": 2,
"api_cpus": "0.25",
"api_memory": "256M",
"postgres_cpus": "1.0",
"pool": 10,
"note": "starve API CPU",
"inspected_limits": [
{
"name": "messenger-lab3-api-2",
"service": "api",
"cpus": 0.25,
"memory_mb": 256
},
{
"name": "messenger-lab3-api-4",
"service": "api",
"cpus": 0.25,
"memory_mb": 256
},
{
"name": "messenger-lab3-lb-1",
"service": "lb",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-migrate-1",
"service": "migrate",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-postgres-1",
"service": "postgres",
"cpus": 1.0,
"memory_mb": 512
}
],
"metrics": {
"successful_requests": 501,
"failed_requests": 0,
"error_ratio": 0.0,
"rps": 25.05,
"p50_ms": 1086.6,
"p95_ms": 1916.3,
"p99_ms": 2246.0,
"instances": {
"96ce82c183ab": 252,
"08070b66fd10": 249
},
"instance_count": 2
},
"resources": {
"api_cpu_sum_pct": 48.01,
"api_cpu_each_pct": {
"messenger-lab3-api-2": 23.78,
"messenger-lab3-api-4": 24.23
},
"api_util_pct": 96.0,
"postgres_cpu_pct": 26.35,
"postgres_util_pct": 26.4,
"lb_cpu_pct": 5.8,
"lb_util_pct": 11.6,
"pg_backends_mean": 21.0,
"pg_active_mean": 1.12,
"pg_waiting_mean": 0.12,
"pool_capacity": 20,
"pool_util_pct": 105.0
},
"saturated": "pool",
"saturation_scores": {
"postgres": 26.4,
"lb": 11.6,
"api": 96.0,
"pool": 105.0
},
"samples": 8
},
{
"id": "grow-0.50",
"series": "grow",
"replicas": 2,
"api_cpus": "0.50",
"api_memory": "256M",
"postgres_cpus": "1.0",
"pool": 10,
"note": "halfway API CPU",
"inspected_limits": [
{
"name": "messenger-lab3-api-2",
"service": "api",
"cpus": 0.5,
"memory_mb": 256
},
{
"name": "messenger-lab3-api-4",
"service": "api",
"cpus": 0.5,
"memory_mb": 256
},
{
"name": "messenger-lab3-lb-1",
"service": "lb",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-migrate-1",
"service": "migrate",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-postgres-1",
"service": "postgres",
"cpus": 1.0,
"memory_mb": 512
}
],
"metrics": {
"successful_requests": 1877,
"failed_requests": 0,
"error_ratio": 0.0,
"rps": 93.85,
"p50_ms": 211.4,
"p95_ms": 593.2,
"p99_ms": 894.4,
"instances": {
"51e7463ae51d": 947,
"ad32f7164869": 930
},
"instance_count": 2
},
"resources": {
"api_cpu_sum_pct": 97.33,
"api_cpu_each_pct": {
"messenger-lab3-api-2": 48.85,
"messenger-lab3-api-4": 48.48
},
"api_util_pct": 97.3,
"postgres_cpu_pct": 37.57,
"postgres_util_pct": 37.6,
"lb_cpu_pct": 12.03,
"lb_util_pct": 24.1,
"pg_backends_mean": 21.0,
"pg_active_mean": 1.71,
"pg_waiting_mean": 0.57,
"pool_capacity": 20,
"pool_util_pct": 105.0
},
"saturated": "pool",
"saturation_scores": {
"postgres": 37.6,
"lb": 24.1,
"api": 97.3,
"pool": 105.0
},
"samples": 7
},
{
"id": "improve-pool20",
"series": "improve",
"replicas": 2,
"api_cpus": "1.0",
"api_memory": "256M",
"postgres_cpus": "1.0",
"pool": 20,
"note": "same 2×1.0 API, pool 10 → 20 per replica",
"inspected_limits": [
{
"name": "messenger-lab3-api-2",
"service": "api",
"cpus": 1.0,
"memory_mb": 256
},
{
"name": "messenger-lab3-api-4",
"service": "api",
"cpus": 1.0,
"memory_mb": 256
},
{
"name": "messenger-lab3-lb-1",
"service": "lb",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-migrate-1",
"service": "migrate",
"cpus": 0.5,
"memory_mb": 128
},
{
"name": "messenger-lab3-postgres-1",
"service": "postgres",
"cpus": 1.0,
"memory_mb": 512
}
],
"metrics": {
"successful_requests": 2813,
"failed_requests": 0,
"error_ratio": 0.0,
"rps": 140.65,
"p50_ms": 142.7,
"p95_ms": 396.9,
"p99_ms": 610.0,
"instances": {
"29e73f6300f7": 1407,
"3eb380a3d7c4": 1406
},
"instance_count": 2
},
"resources": {
"api_cpu_sum_pct": 178.32,
"api_cpu_each_pct": {
"messenger-lab3-api-2": 85.35,
"messenger-lab3-api-4": 92.97
},
"api_util_pct": 89.2,
"postgres_cpu_pct": 63.3,
"postgres_util_pct": 63.3,
"lb_cpu_pct": 22.61,
"lb_util_pct": 45.2,
"pg_backends_mean": 35.14,
"pg_active_mean": 2.86,
"pg_waiting_mean": 1.57,
"pool_capacity": 40,
"pool_util_pct": 87.8
},
"saturated": "api",
"saturation_scores": {
"postgres": 63.3,
"lb": 45.2,
"api": 89.2,
"pool": 87.8
},
"samples": 7
}
],
"finding": "Fixed 2.0 CPU / 512M budget: 1/2/4 replicas → 187.5 / 249.5 / 185.8 RPS. One replica uses only 49% of a 2.0 limit (single uvicorn worker ≈ 1 core). Four replicas cap each worker at 0.5 CPU (p95 464.5 ms). Growing 2×CPU 0.25→0.50→1.0: 25.1 → 93.8 → 249.5 RPS; API util 96%/97% then 85%; postgres util 66%; LB util 47%. Confirmed improvement: same budget, 4×0.5 → 2×1.0 raises RPS 185.8 → 249.5 (+34.2%) and p95 464.5 → 196.7 ms. Raising DB_POOL_MAX 10→20 was tried and rejected (140.7 RPS, p95 396.9 ms)."
}
+53
View File
@@ -0,0 +1,53 @@
<svg xmlns="http://www.w3.org/2000/svg" width="720" height="570" font-family="Segoe UI, sans-serif" font-size="12">
<rect width="100%" height="100%" fill="#fff"/>
<g transform="translate(0,0)">
<rect width="100%" height="100%" fill="#fff"/>
<text x="56" y="20" font-size="14" font-weight="600">Fixed API budget 2.0 CPU / 512M — 2×1.0 wins</text>
<line x1="56" y1="36" x2="56" y2="224" stroke="#333"/>
<line x1="56" y1="224" x2="696" y2="224" stroke="#333"/>
<line x1="56" y1="224.0" x2="696" y2="224.0" stroke="#eee"/>
<text x="50" y="228.0" text-anchor="end">0</text>
<line x1="56" y1="130.0" x2="696" y2="130.0" stroke="#eee"/>
<text x="50" y="134.0" text-anchor="end">149</text>
<line x1="56" y1="36.0" x2="696" y2="36.0" stroke="#eee"/>
<text x="50" y="40.0" text-anchor="end">299</text>
<rect x="120.0" y="106.3" width="133.3" height="117.7" fill="#3b6fd8"/>
<text x="186.7" y="102.3" text-anchor="middle" font-size="11">188</text>
<text x="186.7" y="252" text-anchor="middle">1×2.0</text>
<text x="186.7" y="268" text-anchor="middle" fill="#666" font-size="10">PG CPU 47%</text>
<rect x="333.3" y="67.3" width="133.3" height="156.7" fill="#3b6fd8"/>
<text x="400.0" y="63.3" text-anchor="middle" font-size="11">250</text>
<text x="400.0" y="252" text-anchor="middle">2×1.0</text>
<text x="400.0" y="268" text-anchor="middle" fill="#666" font-size="10">PG CPU 66%</text>
<rect x="546.7" y="107.3" width="133.3" height="116.7" fill="#3b6fd8"/>
<text x="613.3" y="103.3" text-anchor="middle" font-size="11">186</text>
<text x="613.3" y="252" text-anchor="middle">4×0.5</text>
<text x="613.3" y="268" text-anchor="middle" fill="#666" font-size="10">PG CPU 62%</text>
<text x="16" y="130" transform="rotate(-90 16 130)" text-anchor="middle">RPS</text>
</g>
<g transform="translate(0,290)">
<rect width="100%" height="100%" fill="#fff"/>
<text x="56" y="20" font-size="14" font-weight="600">Growing per-replica CPU (2 replicas, postgres 1.0)</text>
<line x1="56" y1="36" x2="56" y2="224" stroke="#333"/>
<line x1="56" y1="224" x2="696" y2="224" stroke="#333"/>
<line x1="56" y1="224.0" x2="696" y2="224.0" stroke="#eee"/>
<text x="50" y="228.0" text-anchor="end">0</text>
<line x1="56" y1="130.0" x2="696" y2="130.0" stroke="#eee"/>
<text x="50" y="134.0" text-anchor="end">149</text>
<line x1="56" y1="36.0" x2="696" y2="36.0" stroke="#eee"/>
<text x="50" y="40.0" text-anchor="end">299</text>
<rect x="120.0" y="208.3" width="133.3" height="15.7" fill="#3b6fd8"/>
<text x="186.7" y="204.3" text-anchor="middle" font-size="11">25</text>
<text x="186.7" y="252" text-anchor="middle">2×0.25</text>
<text x="186.7" y="268" text-anchor="middle" fill="#666" font-size="10">PG CPU 26%</text>
<rect x="333.3" y="165.1" width="133.3" height="58.9" fill="#3b6fd8"/>
<text x="400.0" y="161.1" text-anchor="middle" font-size="11">94</text>
<text x="400.0" y="252" text-anchor="middle">2×0.50</text>
<text x="400.0" y="268" text-anchor="middle" fill="#666" font-size="10">PG CPU 38%</text>
<rect x="546.7" y="67.3" width="133.3" height="156.7" fill="#3b6fd8"/>
<text x="613.3" y="63.3" text-anchor="middle" font-size="11">250</text>
<text x="613.3" y="252" text-anchor="middle">2×1.0</text>
<text x="613.3" y="268" text-anchor="middle" fill="#666" font-size="10">PG CPU 66%</text>
<text x="16" y="130" transform="rotate(-90 16 130)" text-anchor="middle">RPS</text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.3 KiB

+13
View File
@@ -0,0 +1,13 @@
Track 1 — cost of scaling
hypothesis: With a fixed total API CPU/RAM budget (2.0 CPU / 512M), two replicas outperform both one (a single uvicorn worker cannot spend 2.0 CPU) and four (each worker is capped at 0.5 CPU, p95 collapses). Growing per-replica CPU still raises RPS until that slice is no longer tiny. PostgreSQL and HAProxy stay below their limits; extra replicas past 2 only add pool connections to the same 1.0-CPU database. The confirmed improvement is 2×1.0 instead of 4×0.5 at the same budget.
profile: {"concurrency": 24, "warmup_seconds": 5, "measure_seconds": 20, "workload": "closed-loop POST /api/v1/chats/{id}/messages through HAProxy", "fixed_budget": "API total 2.0 CPU / 512M; postgres 1.0 CPU / 512M; pool 10 per replica"}
id series N api_cpu api_mem pg_cpu rps p95 err api_u%% pg_u%% lb_u%% pool%% backends sat
fixed-1 fixed 1 2.0 512M 1.0 187.5 203.6 0 49.3 46.8 39.8 110.0 11.0 pool
fixed-2 fixed 2 1.0 256M 1.0 249.5 196.7 0 85.1 66.2 46.6 105.0 21.0 pool
fixed-4 fixed 4 0.5 128M 1.0 185.8 464.5 0 82.8 61.7 37.6 98.5 39.38 pool
grow-0.25 grow 2 0.25 256M 1.0 25.1 1916.3 0 96.0 26.4 11.6 105.0 21.0 pool
grow-0.50 grow 2 0.50 256M 1.0 93.8 593.2 0 97.3 37.6 24.1 105.0 21.0 pool
improve-pool20 improve 2 1.0 256M 1.0 140.7 396.9 0 89.2 63.3 45.2 87.8 35.14 api
finding: Fixed 2.0 CPU / 512M budget: 1/2/4 replicas → 187.5 / 249.5 / 185.8 RPS. One replica uses only 49% of a 2.0 limit (single uvicorn worker ≈ 1 core). Four replicas cap each worker at 0.5 CPU (p95 464.5 ms). Growing 2×CPU 0.25→0.50→1.0: 25.1 → 93.8 → 249.5 RPS; API util 96%/97% then 85%; postgres util 66%; LB util 47%. Confirmed improvement: same budget, 4×0.5 → 2×1.0 raises RPS 185.8 → 249.5 (+34.2%) and p95 464.5 → 196.7 ms. Raising DB_POOL_MAX 10→20 was tried and rejected (140.7 RPS, p95 396.9 ms).