103 lines
4.2 KiB
Python
103 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Freeze the dichotomy-wave truth corpus for src/rune/rune_profiler.py.
|
|
|
|
LAW (poisoned-truth): RUNE_RP_CORE=python is pinned at the top — truth is
|
|
generated by the PYTHON ORACLE ONLY.
|
|
|
|
The measurement loop (perf_counter_ns over a Python callable / Ollama
|
|
HTTP call) is host-side by nature and stays in the facade; the PORTABLE
|
|
LAW is the stats computation over the measured elapsed times. Truth is
|
|
generated by monkeypatching the oracle's perf_counter_ns to a controlled
|
|
queue of elapsed deltas — every per-iteration elapsed is a multiple of
|
|
15625 ns (1/64 ms), a dyadic rational, so every sum/mean/median is f64-
|
|
exact and the oracle's statistics.mean (exact Fraction) matches the
|
|
kernel's float math BIT-FOR-BIT.
|
|
|
|
profile_function 6 cases (1/2/20/100/101 iterations, unsorted times,
|
|
single iteration, two-element median)
|
|
profile_model_call 3 cases (same stats law minus p95/p99)
|
|
"""
|
|
import itertools
|
|
import json
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
os.environ["RUNE_RP_CORE"] = "python" # poisoned-truth law
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(REPO / "src"))
|
|
|
|
import rune.rune_profiler as rp # noqa: E402
|
|
|
|
OUT = REPO / "src" / "rust" / "rune_profiler" / "src" / "frozen_rp_truth.json"
|
|
|
|
cases = []
|
|
UNIT_NS = 15625 # 1/64 ms — dyadic, f64-exact through sums and /1e6
|
|
|
|
|
|
def freeze_stats(fn_name, elapsed_units, iterations):
|
|
"""Drive the REAL oracle with a controlled perf_counter_ns."""
|
|
vals, c = [], 0
|
|
for u in elapsed_units:
|
|
vals.append(c)
|
|
vals.append(c + u * UNIT_NS)
|
|
c += u * UNIT_NS
|
|
vals.append(c) # pad: 2 calls per iteration consumed
|
|
clock = iter(itertools.cycle(vals) if False else vals)
|
|
rp.time.perf_counter_ns = lambda: next(clock)
|
|
calls = {"n": 0}
|
|
|
|
def fake_func():
|
|
calls["n"] += 1
|
|
|
|
if fn_name == "profile_function":
|
|
r = rp.profile_function(fake_func, iterations=iterations)
|
|
else:
|
|
# profile_model_call calls the module-level _execute_ollama —
|
|
# stub it; the elapsed deltas drive the stats identically
|
|
def fake_ollama(*a, **k):
|
|
calls["n"] += 1
|
|
return "ok"
|
|
rp._execute_ollama = fake_ollama
|
|
r = rp.profile_model_call(iterations=iterations)
|
|
times_ms = [u * UNIT_NS / 1_000_000 for u in elapsed_units]
|
|
cases.append({"fn": fn_name,
|
|
"name": f"{'f' if fn_name == 'profile_function' else 'm'}"
|
|
f"{iterations}_{len(elapsed_units)}_{elapsed_units[0]}"
|
|
f"{'_shuffled' if len(set(elapsed_units)) > 1 and elapsed_units != sorted(elapsed_units) else ''}",
|
|
"args": {"times": times_ms, "iterations": iterations},
|
|
"expect": r})
|
|
assert calls["n"] == iterations, (calls["n"], iterations)
|
|
|
|
|
|
def main():
|
|
# ── profile_function ─────────────────────────────────────────────
|
|
freeze_stats("profile_function", [10], 1)
|
|
freeze_stats("profile_function", [3, 7], 2) # median = mean of 2
|
|
freeze_stats("profile_function", [5, 1, 9, 2, 8, 4, 6, 3, 7, 1,
|
|
2, 9, 4, 8, 1, 6, 2, 7, 3, 5], 20)
|
|
freeze_stats("profile_function", [1] * 100, 100) # p95/p99 = same value
|
|
freeze_stats("profile_function",
|
|
[(i % 13) + 1 for i in range(101)], 101) # odd count median
|
|
freeze_stats("profile_function",
|
|
[((i * 37) % 29) + 1 for i in range(100)], 100)
|
|
|
|
# ── profile_model_call ───────────────────────────────────────────
|
|
freeze_stats("profile_model_call", [6400, 3200, 12800, 1600, 6400], 5)
|
|
freeze_stats("profile_model_call", [1, 2], 2)
|
|
freeze_stats("profile_model_call", [811], 1)
|
|
|
|
OUT.parent.mkdir(parents=True, exist_ok=True)
|
|
groups = {}
|
|
for c in cases:
|
|
groups.setdefault(f"{c['fn']}_cases", []).append(c)
|
|
OUT.write_text(json.dumps(
|
|
{"module": "rune_profiler", "unit_ns": UNIT_NS, **groups},
|
|
indent=1, sort_keys=True) + "\n")
|
|
print(f"frozen {len(cases)} cases -> {OUT}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|