"""Exact synthetic completion examples. No queueing or device benchmark.""" import argparse from datetime import datetime, timezone from fractions import Fraction from hashlib import sha256 from itertools import product import json from math import comb from pathlib import Path import platform def validate(n, k): if type(n) is not int or type(k) is not int or not 1 <= k <= n: raise ValueError("require integer 1 <= k <= n") def independent_late(n, k, p): validate(n, k) p = Fraction(p) if not 0 <= p <= 1: raise ValueError("p must be between zero and one") # Late means fewer than k timely responses, hence at least n-k+1 slow. return sum((comb(n, j) * p**j * (1-p)**(n-j) for j in range(n-k+1, n+1)), Fraction(0)) def joint_late(states, k): if not states: raise ValueError("states must not be empty") n = len(states[0][0]) validate(n, k) if any(len(bits) != n or any(type(b) is not int or b not in (0, 1) for b in bits) or weight < 0 for bits, weight in states): raise ValueError("invalid joint state") if sum(weight for _, weight in states) != 1: raise ValueError("weights must sum to one") return sum((weight for bits, weight in states if n-sum(bits) < k), Fraction(0)) def completion(starts, durations, k, early=False): validate(len(starts), k) if len(starts) != len(durations) or any(type(x) is not int or x < 0 for x in starts): raise ValueError("starts must be nonnegative integer milliseconds") if any(type(x) is not int or x <= 0 for x in durations): raise ValueError("durations must be positive integer milliseconds") chosen = sorted(range(len(starts)), key=lambda i: (starts[i], i)) if early: # A deterministic index tie-break is for this example, not the paper's random rule. chosen = chosen[:k] finishes = sorted(starts[i]+durations[i] for i in chosen) end = finishes[k-1] busy = sum(max(0, min(durations[i], end-starts[i])) for i in chosen) return {"completion_ms": end, "busy_ms": busy} def results(): p = Fraction(1, 10) independent = [(bits, p**sum(bits)*(1-p)**(3-sum(bits))) for bits in product((0, 1), repeat=3)] shared = [((0, 0, 0), 1-p), ((1, 1, 1), p)] exclusive = [((0, 0, 0), 1-3*p)] + [ (tuple(int(j == i) for j in range(3)), p) for i in range(3)] distributions = [("Independent", independent), ("All slow events coincide", shared), ("Slow events never overlap", exclusive)] rows = [] for label, states in distributions: values = [joint_late(states, k) for k in (1, 2, 3)] rows.append({"name": label, "late_fractions": [str(v) for v in values], "late_percent": [float(100*v) for v in values], "states": [{"slow": list(bits), "weight": str(w)} for bits, w in states]}) return { "schema_version": 1, "evidence_type": "synthetic exact calculation, not measured performance", "n": 3, "per_response_late_probability": "1/10", "deadline_rule": "late is strictly after a fixed deadline; equality is timely", "assumptions": [ "Any k distinct valid responses satisfy the data contract.", "Response times use one request origin; decoding and client overhead excluded.", "Marginal timing and fixed starts are specified inputs, not policy-invariant measurements.", "The cancellation example has immediate cancellation and no load feedback." ], "rows": rows, "cancellation": { "starts_ms": [0, 0, 5], "durations_ms": [100, 10, 10], "k": 2, "on_completion": completion([0, 0, 5], [100, 10, 10], 2), "on_start": completion([0, 0, 5], [100, 10, 10], 2, early=True) } } if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() data = results() data["provenance"] = { "generated_at": datetime.now(timezone.utc).isoformat(), "python": platform.python_version(), "generator_sha256": sha256(Path(__file__).read_bytes()).hexdigest(), "sources": [ "https://www.pdl.cmu.edu/PDL-FTP/associated/a12-joshi.pdf", "https://www.usenix.org/system/files/conference/osdi16/osdi16-rashmi.pdf" ] } args.output.write_text(json.dumps(data, indent=2)+"\n")