"""Exact worked bounds for a declared lossless FIFO fluid model, not a device fit. Python 3 standard library only. Units: MiB, seconds and MiB/second. Run model.py to regenerate results.json; use --check to compare without writing. """ from fractions import Fraction as Q import json from pathlib import Path import sys def quantity(value, *, positive=False): if isinstance(value, bool): raise ValueError("Boolean is not a quantity") try: value = Q(str(value)) except (ValueError, ZeroDivisionError, TypeError) as error: raise ValueError("Expected a finite rational quantity") from error if value < 0 or (positive and value == 0): raise ValueError("Quantity outside its nonnegative domain") return value def curve(time, rate, latency): time = quantity(time) rate = quantity(rate, positive=True) latency = quantity(latency) return rate * max(Q(0), time - latency) def bounds(burst, arrival_rate, stages): b, r = quantity(burst), quantity(arrival_rate) stages = [(quantity(rate, positive=True), quantity(latency)) for rate, latency in stages] if not stages: raise ValueError("At least one stage is required") bottleneck = min(rate for rate, _ in stages) if r > bottleneck: raise ValueError("No finite bound from these envelopes: input rate exceeds guarantee") latency = sum((latency for _, latency in stages), Q(0)) local_burst, separate = b, Q(0) nodes = [] for rate, lag in stages: delay = lag + local_burst / rate backlog = local_burst + r * lag nodes.append({"input_burst": local_burst, "delay": delay, "backlog": backlog}) separate += delay local_burst = backlog return {"rate": bottleneck, "latency": latency, "delay": latency + b / bottleneck, "backlog": b + r * latency, "separate_delay_sum": separate, "nodes": nodes} def document(): result = bounds(1, 80, [(200, "0.002"), (100, "0.003")]) return { "kind": "exact-worked-calculation", "units": {"burst": "MiB", "rate": "MiB/s", "latency": "s", "delay": "s", "backlog": "MiB"}, "inputs": {"burst": "1", "arrival_rate": "80", "stages": [{"rate": "200", "latency": "1/500"}, {"rate": "100", "latency": "3/1000"}]}, "exact": result, "display": {"path_delay_ms": float(result["delay"] * 1000), "separate_delay_sum_ms": float(result["separate_delay_sum"] * 1000), "path_backlog_mib": float(result["backlog"])}, "assumptions": [ "Lossless, initially empty FIFO fluid path with the same conserved work unit at every stage.", "Input envelope holds on every interval; each stage guarantees the stated minimum-service curve.", "No extra traffic, replication, work amplification or new processing stage is inserted between stages." ], "limitations": [ "Not a storage benchmark, trace replay, percentile estimate or measured latency improvement.", "The backlog bound covers total fluid work in the path, including work in service, not one device queue.", "At equal arrival and bottleneck rates the bound is finite, but spare drain capacity is not guaranteed.", "Packetized I/O and firmware stalls require their own justified model; mean IOPS is not a guarantee." ] } def rendered(): return json.dumps(document(), indent=2, default=str) + "\n" if __name__ == "__main__": if sys.argv[1:] not in ([], ["--check"]): raise SystemExit("Use model.py [--check]") path = Path(__file__).with_name("results.json") text = rendered() if sys.argv[1:]: if path.read_text() != text: raise SystemExit("Generated results differ") print("Exact results match") else: path.write_text(text)