Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/measuring_fps.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ there goes round views of about 24 s with different draw counts, so phases of 15
Standing, `findings.md` cuts the phases into stretches where the draw count jumps, leaves out the first phase of a
stretch and fits a level for each stretch; walking, the switch of bit *b* has runs of 2^*b* phases off and 2^*b* on:
each pair of neighbouring runs gives one difference, and their mean is the effect. Give the effect **with its standard
error**. The scripts it names for this (`ab_pair.py`, `ab_eff.py`) are not in the repository.
error**. `python -I tools/d3d12_ab_compare.py phases <log>` does the walking way for every switch of the run (pairs of
blocks, a pair whose draws a frame differ by more than a tenth left out), for the median frame time and for each
counter of the `A/B counters` line the runtime writes after a phase line (with `diagnostics`).

The standard errors in `findings.md` are mostly 0.2 to 0.6 ms for a standing run of some minutes (up to 0.94) and 1 to
2 ms for a walking one (up to 2.24). Two separate
Expand All @@ -61,6 +63,10 @@ A change of code, or a switch that cannot be turned in play, needs separate runs
- Check every run for the state you think it was in: it reached the world, it did not stay in the menu, it did not end early. One that
stayed in the menu showed 60 throughout (seen once, 2026-11-30).
- With two runs a side, do not claim a difference smaller than the spread between runs of the same side.
- `python -I tools/d3d12_ab_compare.py plan --pairs 4` prints such an order; `abba A:<run> B:<run> B:<run> A:<run> ...`
reads the runs in that order (logs, or the game gate's run folders with their `summary.json`): one value a run (its
world median frame time), neighbouring pairs, the effect with its standard error and 95 % interval from at least
4 balanced pairs, and "B faster" or "B slower" only when the interval leaves 0 out and all pairs but one agree.

## 4. Which number means what

Expand Down
144 changes: 144 additions & 0 deletions tests/test_d3d12_ab_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
"""tools/d3d12_ab_compare.py on synthetic numbers and synthetic logs (no game data)."""
import io
import json
import os
import sys
import tempfile
import unittest
from contextlib import redirect_stdout

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
import d3d12_ab_compare as ab # noqa: E402


def items(order, values):
return [(arm, v, 4000.0, None) for arm, v in zip(order, values)]


class Pairs(unittest.TestCase):
def test_the_worked_example_is_b_faster(self):
# A B B A A B B A with A about 1.2 ms slower and a drift between runs.
r = ab.abba(items("ABBAABBA", [20.4, 19.1, 19.6, 20.9, 21.3, 19.8, 20.2, 21.0]))
e = r["result"]
self.assertEqual(r["method"], "pairs")
self.assertAlmostEqual(e["effect"], -1.225, places=3)
self.assertAlmostEqual(e["se"], 0.1493, places=3)
self.assertAlmostEqual(e["ci95"][0], -1.700, places=2)
self.assertAlmostEqual(e["ci95"][1], -0.750, places=2)
self.assertEqual(e["agree"], 4)
self.assertEqual(r["verdict"], "B faster")

def test_a_drift_alone_in_an_unbalanced_order_is_no_effect(self):
# A B A B A B A B with every run 0.25 ms slower than the one before and no effect at all: each pair would
# read B 0.25 ms slower. The pairs are not balanced, so the side and a trend are fitted instead.
r = ab.abba(items("ABABABAB", [20.0 + 0.25 * i for i in range(8)]))
self.assertNotEqual(r["method"], "pairs")
self.assertAlmostEqual(r["result"]["effect"], 0.0, places=6)
self.assertEqual(r["verdict"], "no clear difference")

def test_one_odd_run_gives_no_verdict(self):
r = ab.abba(items("ABBAABBA", [20.0, 16.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0]))
self.assertEqual(r["verdict"], "no clear difference")

def test_three_pairs_are_not_enough_for_pairs(self):
r = ab.abba(items("ABBAAB", [20.4, 19.1, 19.6, 20.9, 21.3, 19.8]))
self.assertNotEqual(r["method"], "pairs")

def test_three_runs_a_side_fit_the_side_and_a_trend(self):
# B is 1 ms faster, and every run is 0.3 ms slower than the one before.
order = "ABBAAB"
values = [20.0 + 0.3 * i - (1.0 if arm == "B" else 0.0) for i, arm in enumerate(order)]
r = ab.abba(items(order, values))
self.assertEqual(r["method"], "side and linear trend")
self.assertAlmostEqual(r["result"]["effect"], -1.0, places=6)
self.assertAlmostEqual(r["result"]["trend_per_run"], 0.3, places=6)

def test_a_run_left_out_drops_its_pair(self):
it = items("ABBAABBAAB", [20.4, 19.1, 19.6, 20.9, 21.3, 19.8, 20.2, 21.0, 20.6, 19.4])
it[2] = ("B", None, None, "gate FAIL")
r = ab.abba(it)
self.assertEqual(len(r["pairs"]), 4)
self.assertEqual(r["runs"][2]["left_out"], "gate FAIL")


class Runs(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp(prefix="ab-test-")

def gate(self, name, fps, result="PASS"):
d = os.path.join(self.dir, name)
os.makedirs(d)
with open(os.path.join(d, "summary.json"), "w", encoding="utf-8") as f:
json.dump({"fps": {"median": fps, "draws_median": 4400}, "verdict": {"result": result}}, f)
return d

def log(self, name, fps_values, draws):
path = os.path.join(self.dir, name)
with open(path, "w", encoding="utf-8") as f:
for fps in fps_values:
f.write(f"2026-11-30T18:00:00.000000Z INFO leonida_gpu::host: {fps:.1f} fps, {1000 / fps:.1f} ms/frame "
f"(1.0 preparing: copies 0.1 ms 0.1 MB, textures 0.1 ms 0.1 MB; 5.0 on the GPU; 0.0 copying back "
f"0.0 MB), {draws} draws/frame, dropped 0 without a host pipeline (0 being made) and 0 indirect\n")
f.write("a line the title printed: keep it out\n")
return path

def test_a_gate_folder_gives_its_world_median_and_a_failed_gate_is_left_out(self):
self.assertAlmostEqual(ab.run_value(self.gate("ok", 25.0))[0], 40.0)
self.assertEqual(ab.run_value(self.gate("bad", 25.0, "FAIL"))[2], "gate FAIL")

def test_a_log_gives_the_median_of_its_world_lines(self):
menus = self.log("menus.log", [60.0] * 5, 13)
world = self.log("world.log", [20.0, 25.0, 25.0], 4000)
self.assertEqual(ab.run_value(menus)[2], "no frame line in the world")
self.assertAlmostEqual(ab.run_value(world)[0], 40.0)

def test_the_command_prints_numbers_only(self):
runs = []
for i, (arm, fps) in enumerate(zip("ABBAABBA", [25, 26, 26, 25, 25, 26, 26, 25])):
runs.append(f"{arm}:" + self.gate(f"r{i}", float(fps)))
out = io.StringIO()
with redirect_stdout(out):
self.assertEqual(ab.main(["abba"] + runs), 0)
text = out.getvalue()
self.assertIn("verdict: B faster", text)
self.assertNotIn(self.dir, text)


class Phases(unittest.TestCase):
def test_a_switch_effect_and_its_counters_per_pair_of_blocks(self):
path = os.path.join(tempfile.mkdtemp(prefix="ab-phases-"), "run.log")
with open(path, "w", encoding="utf-8") as f:
for n in range(12):
on = n % 2
median = 30.0 - (1.0 if on else 0.0) + 0.01 * n
f.write(f"2026-11-30T18:00:{n:02d}.000000Z INFO leonida_gpu::ab: A/B phase {n} bits {on} on [{'LEONIDA_X' if on else ''}]: "
f"200 frames, mean {median:.1f} ms, median {median:.2f}, p90 {median + 5:.1f}, 4400 draws a frame; rest\n")
f.write(f"2026-11-30T18:00:{n:02d}.500000Z INFO leonida_gpu::ab: A/B counters phase {n} bits {on}: "
f"protect_ms={(2.0 if on else 8.0):.3f} kernel_asks=3.00\n")
r = ab.phase_effects(ab.read_phases(path))
s = r["switches"][0]
self.assertEqual(s["pairs"], 6)
self.assertAlmostEqual(s["effects"]["median"]["effect"], -0.99, places=2)
self.assertAlmostEqual(s["effects"]["protect_ms"]["effect"], -6.0, places=6)
self.assertEqual(s["verdict"], "B faster")

def test_a_scene_change_between_blocks_is_left_out(self):
phases = {0: {"bits": 0, "median": 30.0, "draws": 4000.0, "counters": {}},
1: {"bits": 1, "median": 20.0, "draws": 9000.0, "counters": {}}}
s = ab.phase_effects(phases)["switches"][0]
self.assertEqual((s["pairs"], s["left_out_for_draws"]), (0, 1))


class Plan(unittest.TestCase):
def test_builds_end_the_warm_up_on_the_first_measured_side(self):
p = ab.plan(4, switch=False)
self.assertEqual(p["warm_up_and_discarded"], ["warm-up A", "warm-up B", "discarded B", "discarded A"])
self.assertEqual("".join(p["measured"]), "ABBAABBA")

def test_one_exe_two_settings_discards_one_run(self):
self.assertEqual(ab.plan(2, switch=True)["warm_up_and_discarded"], ["warm-up A", "warm-up B", "discarded A"])


if __name__ == "__main__":
unittest.main()
Loading