Files

135 lines
7.0 KiB
Python

"""Pure, deterministic rules for the preregistered v2 experiment."""
import random
import statistics
CONFIGS = {
"A1": {"replicas": 1, "cpu": 1, "memory": "256m", "pool": 12},
"A2": {"replicas": 2, "cpu": .5, "memory": "128m", "pool": 6},
"A4": {"replicas": 4, "cpu": .25, "memory": "64m", "pool": 3},
"B1": {"replicas": 1, "cpu": .5, "memory": "128m", "pool": 12},
"B2": {"replicas": 2, "cpu": .5, "memory": "128m", "pool": 6},
"B4": {"replicas": 4, "cpu": .5, "memory": "128m", "pool": 3},
}
DESIGN = {
"version": "v2.0",
"hypotheses": {
"H1": "Fixed total API budget: C4/C1 < 1.20 in all three blocks",
"H2": "Growing API budget: C4/C1 >= 1.50 in all three blocks",
"G_low": "At 25% of pilot A4 capacity, GOMAXPROCS=1 reduces p95 >=15% in all five pairs, retaining success and SLO",
"G_high": "At 90% of pilot A4 capacity, the same criteria hold in all five pairs",
"DB_CPU": "At the pilot B4 upper rate, increasing only DB CPU 1->2 restores SLO in all three blocks, with CPU-DB diagnostic prerequisites",
},
"duration_s": 30, "warmup_s": 15, "cooldown_s": 10,
"timeout_s": 5, "workload_seed": 230300, "inflight_limit": 8192,
"success_min": .99, "p95_max_ms": 500,
"workload": "80% new idempotent POST messages (128 ASCII bytes), 20% first history page GET limit=50",
"fixture": {"users": 200, "chats": 100, "messages": 10000},
"dependencies": {"db_cpu": 1, "db_memory": "512m", "redis_cpu": .25, "redis_memory": "128m", "lb_cpu": .5, "lb_memory": "128m"},
"pool_total": 12, "control_gomaxprocs": 16, "tuned_gomaxprocs": 1,
"search": {"start": 100, "floor": 25, "ceiling": 6400, "max_points": 10, "relative_width": .10},
"scaling_repeats": 3, "gomax_pairs": 5, "db_pairs": 3,
"calibration_seed": 230700, "scaling_seeds": [230701, 230702, 230703],
"gomax_order_seed": 230816, "db_order_seed": 230817,
"budget_seconds": 9000,
"generator_invalid": "dropped/scheduled >0.01 OR (dispatch p95 >100ms AND generator CPU >=0.85 cores)",
"db_cpu_prerequisites": {"db_cpu_min": .90, "api_cpu_max": .85, "lb_cpu_max": .90},
"aggregation": "Keep all pairs. Mean/median/range of paired percent reductions are descriptive, not a pooled p95. Mixed outcomes are inconclusive.",
"censoring": "No failed upper point means only a lower bound, never an exact capacity. Invalid generator stops the experiment; bad API performance and OOM are retained.",
"pilot_excluded_from_confirmation": True,
}
def config(name, gomax=16, db_cpu=1):
return {**CONFIGS[name], "gomaxprocs": gomax, "db_cpu": db_cpu}
def next_rate(observations, rules=None):
rules = rules or DESIGN["search"]
if any(not row["valid"] for row in observations):
return None
if not observations:
return rules["start"]
if len(observations) >= rules["max_points"]:
return None
passed = [row["rate"] for row in observations if row["slo_pass"]]
failed = [row["rate"] for row in observations if not row["slo_pass"]]
if passed and failed and max(passed) >= min(failed):
return None
if not passed:
value = max(rules["floor"], min(failed) // 2)
return value if value < min(failed) else None
lower = max(passed)
if not failed:
value = min(lower * 2, rules["ceiling"])
return value if value > lower else None
upper = min(failed)
if (upper - lower) / lower <= rules["relative_width"] or upper - lower <= 1:
return None
return (lower + upper) // 2
def bracket(observations):
passed = [row["rate"] for row in observations if row["valid"] and row["slo_pass"]]
failed = [row["rate"] for row in observations if row["valid"] and not row["slo_pass"]]
lower = max(passed, default=0)
upper = min(failed) if failed else None
invalid = any(not row["valid"] for row in observations)
nonmonotonic = upper is not None and lower >= upper
return {"lower": lower, "upper": upper, "invalid": invalid, "nonmonotonic": nonmonotonic,
"closed": bool(lower and upper and not invalid and not nonmonotonic)}
def freeze_confirmation(calibration):
bounds = {name: bracket(rows) for name, rows in calibration.items()}
rates = {}
for name, value in bounds.items():
candidates = [100, value["lower"], value["upper"]]
rates[name] = sorted({rate for rate in candidates if rate and rate <= DESIGN["search"]["ceiling"]})
scaling = []
for repeat, seed in enumerate(DESIGN["scaling_seeds"], 1):
points = [{"phase": "scaling", "base": name, "rate": rate, "repeat": repeat, "variant": name,
"config": config(name)} for name in CONFIGS for rate in rates[name]]
random.Random(seed).shuffle(points)
scaling.extend(points)
gomax = []
if bounds["A4"]["closed"]:
groups = []
for regime, fraction in (("low", .25), ("high", .90)):
rate = max(1, int(bounds["A4"]["lower"] * fraction))
for repeat in range(1, DESIGN["gomax_pairs"] + 1):
order = [16, 1] if repeat % 2 else [1, 16]
groups.append([{"phase": "gomax", "base": "A4", "regime": regime, "rate": rate,
"repeat": repeat, "variant": "I0" if value == 16 else "I1",
"config": config("A4", gomax=value)} for value in order])
random.Random(DESIGN["gomax_order_seed"]).shuffle(groups)
gomax = [point for group in groups for point in group]
database = []
if bounds["B4"]["closed"]:
groups = []
for repeat in range(1, DESIGN["db_pairs"] + 1):
for rate in (bounds["B4"]["lower"], bounds["B4"]["upper"]):
order = [1, 2] if repeat % 2 else [2, 1]
groups.append([{"phase": "database", "base": "B4", "rate": rate, "repeat": repeat,
"variant": "D0" if cpu == 1 else "D1", "config": config("B4", db_cpu=cpu)} for cpu in order])
random.Random(DESIGN["db_order_seed"]).shuffle(groups)
database = [point for group in groups for point in group]
return {"pilot_bounds": bounds, "rates_by_config": rates, "scaling": scaling,
"database": database, "gomax": gomax,
"skipped": {"gomax": None if gomax else "A4 pilot bracket not closed",
"database": None if database else "B4 pilot bracket not closed"}}
def pair_decision(pairs, expected=5):
if len(pairs) != expected or any(not pair["valid"] for pair in pairs):
result = "inconclusive"
elif all(pair["criteria_met"] for pair in pairs):
result = "supported"
elif all(not pair["criteria_met"] for pair in pairs):
result = "refuted"
else:
result = "inconclusive"
values = [pair["reduction_percent"] for pair in pairs if pair["reduction_percent"] is not None]
return {"result": result, "pairs": pairs, "reduction_mean_percent": statistics.mean(values) if values else None,
"reduction_median_percent": statistics.median(values) if values else None,
"reduction_range_percent": [min(values), max(values)] if values else None}