"""Exact constructed rank/value example; not a sketch or device benchmark.""" import argparse from bisect import bisect_right from fractions import Fraction import hashlib import json from pathlib import Path def observations(slow_start_us=1_000_000): if type(slow_start_us) is not int or slow_start_us <= 10_000: raise ValueError("slow_start_us must be an integer greater than 10000") return list(range(101, 10_001)) + list(range(slow_start_us, slow_start_us + 100)) def validated(values): ordered = list(values) if not ordered or any(type(x) is not int or x <= 0 for x in ordered): raise ValueError("observations must be nonempty positive integer microseconds") return sorted(ordered) def nearest_rank(values, probability): if not isinstance(probability, Fraction) or not 0 <= probability <= 1: raise ValueError("probability must be a Fraction in [0, 1]") ordered = validated(values) numerator = probability.numerator * len(ordered) position = max(1, (numerator + probability.denominator - 1) // probability.denominator) return ordered[position - 1] def inclusive_rank(values, value): if type(value) is not int: raise ValueError("query must be integer microseconds") ordered = validated(values) return Fraction(bisect_right(ordered, value), len(ordered)) def result(): values = observations() target = Fraction(99, 100) truth = nearest_rank(values, target) rows = [] for position in (9899, 9900, 9901): value = values[position - 1] rank = inclusive_rank(values, value) rows.append({ "position": position, "latencyUs": value, "normalizedRank": str(rank), "rankError": str(abs(rank - target)), "rankErrorPercentagePoints": str(100 * abs(rank - target)), "relativeValueError": str(Fraction(abs(value - truth), truth)), "valueRatio": str(Fraction(value, truth)), }) return { "schemaVersion": 1, "kind": "constructed-exact-example", "generatedDate": "2026-11-28", "unit": "microseconds", "observationCount": len(values), "inputRule": "Integers 101..10000 inclusive, followed by 1000000..1000099 inclusive.", "inputSha256": hashlib.sha256(json.dumps(values, separators=(",", ":")).encode()).hexdigest(), "quantileConvention": "nearest rank ceil(p*N), with p=0 selecting the minimum", "rankConvention": "count(x <= query)/N; all example observations are distinct", "targetRank": str(target), "exactQuantileUs": truth, "allowedAbsoluteRankError": "1/10000", "rows": rows, "threshold": {"latencyUs": 10000, "atOrBelow": 9900, "total": 10000, "fraction": "99/100"}, "limits": [ "No KLL or DDSketch implementation was executed.", "Candidate values illustrate what an error contract permits, not what a library returns.", "No device measurements, random sampling or population confidence interval.", "No KLL parameter k or failure probability is inferred from the chosen rank budget.", ], } if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--check", action="store_true") args = parser.parse_args() destination = Path(__file__).with_name("results.json") encoded = json.dumps(result(), indent=2) + "\n" if args.check: if destination.read_text() != encoded: raise SystemExit("Stored results do not match the exact calculation") print("Exact rank/value results match") else: destination.write_text(encoded)