Files
mtusi2026lab3/student-tests/test_benchmark_profile.py

73 lines
2.7 KiB
Python

"""The scaling matrix must compare the same workload and resource budgets."""
import sys
import json
import unittest
from unittest.mock import patch
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
from benchmark import open_loop, topology, validate_matrix
class BenchmarkProfileTests(unittest.TestCase):
def test_fixed_budget_is_identical_across_replica_counts(self):
budgets = [topology("fixed", replicas) for replicas in (1, 2, 4)]
self.assertEqual([replicas * cpu for replicas, cpu, _ in budgets], [1.0] * 3)
self.assertEqual([replicas * memory for replicas, _, memory in budgets], [512] * 3)
def test_growth_budget_increases_with_replicas(self):
budgets = [topology("growth", replicas) for replicas in (1, 2, 4)]
self.assertEqual([replicas * cpu for replicas, cpu, _ in budgets], [0.5, 1.0, 2.0])
self.assertEqual([replicas * memory for replicas, _, memory in budgets], [256, 512, 1024])
def test_matrix_requires_three_repeats_and_same_workload(self):
rows = [
{"mode": mode, "replicas": replicas, "rate": rate, "repeat": repeat,
"duration": 8, "warmup": 2, "dataset_messages": 1000,
"api_cpu_each": topology(mode, replicas)[1],
"api_memory_mib_each": topology(mode, replicas)[2]}
for mode in ("fixed", "growth")
for replicas in (1, 2, 4)
for rate in (100, 250, 500)
for repeat in (1, 2, 3)
]
validate_matrix(rows)
with self.assertRaises(ValueError):
validate_matrix(rows[:-1])
rows[-1]["dataset_messages"] = 999
with self.assertRaises(ValueError):
validate_matrix(rows)
def test_open_loop_counts_responses_at_a_scheduled_rate(self):
class Response:
status = 200
def read(self):
return json.dumps({"items": [{}] * 20}).encode()
class Connection:
def __init__(self, *args, **kwargs):
pass
def request(self, *args, **kwargs):
pass
def getresponse(self):
return Response()
def close(self):
pass
with patch("benchmark.http.client.HTTPConnection", Connection):
result = open_loop("http://127.0.0.1:8080", "/history", "synthetic", 20, 0.2, workers=2)
self.assertEqual(result["scheduled_requests"], 4)
self.assertEqual(result["successful_requests"], 4)
self.assertEqual(result["failed_requests"], 0)
self.assertGreater(result["successful_latency_ms"]["p95"], 0)
if __name__ == "__main__":
unittest.main()