"""Exact stationary PS/FCFS examples with invented inputs; not a simulator.""" from fractions import Fraction import hashlib import json from pathlib import Path import sys def rational(value): if isinstance(value, bool) or not isinstance(value, (str, int, Fraction)): raise ValueError("Use integers, rational strings or Fraction") try: return Fraction(value) except (ValueError, ZeroDivisionError) as error: raise ValueError("Expected a finite rational value") from error def compare(distribution, arrivals_per_second): pairs = [(rational(p), rational(s)) for p, s in distribution] if not pairs or any(p <= 0 or s <= 0 for p, s in pairs): raise ValueError("Positive probabilities and service requirements required") if sum(p for p, _ in pairs) != 1: raise ValueError("Probabilities must sum to one") rate = rational(arrivals_per_second) / 1000 mean = sum(p * s for p, s in pairs) second = sum(p * s * s for p, s in pairs) rho = rate * mean if not 0 <= rho < 1: raise ValueError("Stationary calculation requires 0 <= rho < 1") return { "mean_service_ms": mean, "second_moment_ms2": second, "utilization": rho, "ps_mean_response_ms": mean / (1 - rho), "fcfs_mean_response_ms": mean + rate * second / (2 * (1 - rho)), "ps_conditional_mean_slowdown": 1 / (1 - rho), } def conditional_mean(service_ms, utilization): size, rho = rational(service_ms), rational(utilization) if size <= 0 or not 0 <= rho < 1: raise ValueError("Positive size and 0 <= rho < 1 required") return size / (1 - rho) def batch_completion(service_ms, other_remaining_ms): """Exact tagged completion under ideal PS with no future arrivals.""" size = rational(service_ms) if not isinstance(other_remaining_ms, (list, tuple)): raise ValueError("Remaining work must be a list or tuple") remaining = [rational(value) for value in other_remaining_ms] if size <= 0 or any(value < 0 for value in remaining): raise ValueError("Positive tagged size and nonnegative remaining work required") return size + sum(min(size, value) for value in remaining) def batch_examples(): rows = [] for name, remaining in [("unequal", ["1/4", "7/4"]), ("equal", ["1", "1"])]: completion = batch_completion("1", remaining) rows.append({ "id": name, "tagged_service_ms": "1", "other_remaining_ms": remaining, "other_job_count": len(remaining), "total_other_remaining_ms": str(sum(map(Fraction, remaining))), "completion_ms_exact": str(completion), "completion_ms": float(completion), }) return { "kind": "constructed deterministic finite batch", "method": "T = x + sum(min(x, r_i))", "assumptions": [ "No future arrivals; all other work is present at time zero", "Ideal equal sharing, constant unit capacity, no overhead or interruptions", "Service requirements and remaining work are full-rate milliseconds", ], "limitations": "Not a stationary M/G/1 mean or a device measurement.", "rows": rows, } def result(): rows = [] distributions = [ ("constant", "Always 1 ms", [("1", "1")]), ("mixture", "99% at 0.5 ms; 1% at 50.5 ms", [("99/100", "1/2"), ("1/100", "101/2")]), ] for rate in (200, 800): for name, label, distribution in distributions: values = compare(distribution, rate) rows.append({ "id": f"{name}-{rate}", "service": label, "distribution": [{"probability": p, "service_ms": s} for p, s in distribution], "arrivals_per_second": rate, "exact": {key: str(value) for key, value in values.items()}, **{key: float(value) for key, value in values.items()}, }) return { "schema_version": 1, "generated": "2026-11-20", "kind": "constructed analytic examples", "methods": { "ps": "E[T] = E[S]/(1-rho); E[T | S=x] = x/(1-rho)", "fcfs": "E[T] = E[S] + lambda E[S^2]/(2(1-rho))", "load": "rho = lambda E[S]; lambda converted from requests/s to requests/ms", }, "assumptions": [ "Independent Poisson arrivals and iid positive service requirements", "One constant-capacity server; stationary regime; rho < 1", "Unlimited admission; no abandonment, interruptions or switching overhead", "PS: continuous equal sharing among all present jobs", "FCFS: nonpreemptive first-come-first-served; finite second moment", ], "limitations": "Not device measurements, a finite trace, p99 or a completion guarantee.", "generator_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "rows": rows, "finite_batch": batch_examples(), } if __name__ == "__main__": if sys.argv[1:] not in ([], ["--check"]): raise SystemExit("Use model.py [--check]") path = Path(__file__).with_name("results.json") encoded = json.dumps(result(), indent=2) + "\n" if sys.argv[1:]: if path.read_text() != encoded: raise SystemExit("Published results differ from the exact calculation") print("Exact PS/FCFS examples verified") else: path.write_text(encoded)