"""Validate native mesh/proxy coexistence measurements and compare medians.""" import json import math from statistics import median PHASES = (("mesh_only", "mesh"), ("proxy_only", "proxy"), ("combined_idle_mesh", "mesh"), ("combined_idle_proxy", "proxy"), ("concurrent", "mesh"), ("concurrent", "proxy")) FLAGS = ("mesh_survives_proxy_stop", "proxy_survives_mesh_stop", "no_mesh_to_proxy_fallback", "proxy_recovered", "ports_released") def positive(value): return type(value) in (int, float) and math.isfinite(value) and value > 0 def parse(log, spec): flows, functional = [], [] for line in log.splitlines(): if not line.startswith("{"): continue record = json.loads(line) if record.get("mode") == "combined_flow": flows.append(record) elif record.get("mode") == "combined_functional": functional.append(record) expected = {(phase, lane, direction, repeat) for phase, lane in PHASES for direction in ("download", "upload") for repeat in range(1, spec["repeats"] + 1)} seen = set() for flow in flows: key = tuple(flow[k] for k in ("phase", "lane", "direction", "repeat")) if key not in expected or key in seen: raise ValueError("unexpected or duplicate joint sample: " + str(key)) seen.add(key) if any(not positive(flow[k]) for k in ("bytes", "seconds", "mbps")): raise ValueError("invalid joint transfer measurement") if flow["seconds"] < spec["duration_seconds"] * .8: raise ValueError("joint transfer duration too short") if not math.isclose(flow["mbps"], flow["bytes"] * 8 / flow["seconds"] / 1e6, rel_tol=1e-6): raise ValueError("inconsistent joint transfer rate") overlap = flow["overlap_ms"] if type(overlap) not in (float, int) or not math.isfinite(overlap) or overlap < 0: raise ValueError("invalid joint overlap") if flow["phase"] == "concurrent" and not spec["duration_seconds"] * 800 <= overlap <= flow["seconds"] * 1000: raise ValueError("insufficient concurrent overlap") if seen != expected: raise ValueError(f"missing joint measurements: {len(expected - seen)}") if len(functional) != 1 or any(functional[0].get(key) is not True for key in FLAGS): raise ValueError("joint fault-isolation checks incomplete or failed") for key in ("repeats", "duration_seconds", "rtt_ms", "proxy_limit_mbps"): if functional[0].get(key) != spec[key]: raise ValueError("joint configuration mismatch: " + key) if functional[0].get("udp_probes_per_lane") != spec["repeats"] * 8: raise ValueError("missing UDP probes under concurrent TCP load") groups = [] for direction in ("download", "upload"): for phase, lane in PHASES: rates = [f["mbps"] for f in flows if (f["phase"], f["lane"], f["direction"]) == (phase, lane, direction)] groups.append(dict(phase=phase, lane=lane, direction=direction, samples=len(rates), median_mbps=median(rates), min_mbps=min(rates), max_mbps=max(rates))) comparisons, issues = [], [] for direction in ("download", "upload"): for lane in ("mesh", "proxy"): rates = {g["phase"]: g["median_mbps"] for g in groups if g["lane"] == lane and g["direction"] == direction} for label, before, after in (("enabled_idle", lane + "_only", "combined_idle_" + lane), ("concurrent_vs_idle", "combined_idle_" + lane, "concurrent"), ("concurrent_vs_only", lane + "_only", "concurrent")): change = (rates[after] / rates[before] - 1) * 100 comparisons.append(dict(lane=lane, direction=direction, comparison=label, change_percent=change)) if change < -spec["max_slowdown_percent"]: issues.append(f"{lane}/{direction}/{label}: {change:+.2f}% exceeds {spec['max_slowdown_percent']}% slowdown limit") return dict(measurements=flows, functional=functional[0], summary=groups, comparisons=comparisons, issues=issues) def markdown(result): lines = ["# Native mesh + sing-box coexistence", "", "Local upstream fixture; real Rust SDK and Go FFI in one process. No system TUN or WAN.", "", "| Phase | Lane | Direction | Samples | Median Mbps | Min | Max |", "|---|---|---|---:|---:|---:|---:|"] for g in result["summary"]: lines.append(f"| {g['phase']} | {g['lane']} | {g['direction']} | {g['samples']} | {g['median_mbps']:.2f} | {g['min_mbps']:.2f} | {g['max_mbps']:.2f} |") lines += ["", "| Lane | Direction | Comparison | Change |", "|---|---|---|---:|"] for c in result["comparisons"]: lines.append(f"| {c['lane']} | {c['direction']} | {c['comparison']} | {c['change_percent']:+.2f}% |") lines += ["", "Functional evidence: `" + json.dumps(result["functional"], sort_keys=True) + "`", ""] lines += result["issues"] or ["All configured joint criteria passed."] return "\n".join(lines) + "\n"