Files
airdef/tools/test_raid_bench.py
T
chexit 92c058f606 Sim::step phase timing and the CPU-profile bench driver (#147 step 3.1)
Mod side: Sim::step times plan_step, adapt, step_drones, step_moves,
update_sight, step_sams and record, plus the whole step and its untimed
remainder, through sim::profile. The simulation stays SDK-independent (also
built as airdef-core): the adapter installs the host monotonic clock
(game.monotonic-nanos) in init, native tests use std::time::Instant. The
counters are fixed-size, per-thread, read no randomness and change no
decision; /airdef stats reports them as PROFILE chat lines, reset per world.

Driver: raid_bench gains --scheduler in-frame|server-thread (the two
single-player paths, measured separately), HORIZON_MOD_CPU_PROFILE=1, the
per-tick mod-path spans joined to the frame trace (per-frame totals, >25/33 ms
counts, what the worst CPU frames carry), and the mod phase profile read at
idle and battle end with the battle delta. report.json carries mod_cpu,
mod_cpu_ticks, mod_phases_* and the derived correlation. Tests for the new
helpers.

Engine gitlink: mod-path CPU spans + game.monotonic-nanos (API level 32).

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
2026-11-21 12:29:22 +03:00

122 lines
5.7 KiB
Python

"""Тесты драйвера raid_bench (#147): перцентили как в FrameStats, худшие кадры,
расписание боя и разбор отпечатка. Запуск: python3 -m unittest tools/test_raid_bench.py."""
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import raid_bench as rb # noqa: E402
class Percentiles(unittest.TestCase):
def test_ceil_index_like_frame_stats(self):
# FrameStats::summary: sorted[ceil(n*p).clamp(1, n) - 1].
v = [float(i) for i in range(1, 101)] # 1..100
self.assertEqual(rb.percentile(v, 0.5), 50.0)
self.assertEqual(rb.percentile(v, 0.95), 95.0)
self.assertEqual(rb.percentile(v, 0.99), 99.0)
self.assertEqual(rb.percentile(v, 1.0), 100.0)
def test_small_and_empty(self):
self.assertIsNone(rb.percentile([], 0.95))
self.assertEqual(rb.percentile([7.0], 0.99), 7.0)
self.assertEqual(rb.percentile([3.0, 1.0, 2.0], 0.5), 2.0)
# ceil(2*0.95)=2 -> второй из двух.
self.assertEqual(rb.percentile([1.0, 9.0], 0.95), 9.0)
def test_unsorted_input(self):
self.assertEqual(rb.percentile([9.0, 1.0, 5.0], 0.5), 5.0)
class FrameSummary(unittest.TestCase):
def test_worst_frame_counts(self):
raw = {"frame_ms": [10.0, 25.0, 25.1, 33.0, 33.4, 100.0],
"cpu_ms": [5.0, 20.0], "gpu_ms": []}
s = rb.frame_summary(raw)
self.assertEqual(s["frames_over_25ms"], 4) # строго больше 25
self.assertEqual(s["frames_over_33ms"], 2) # строго больше 33
self.assertEqual(s["frame_ms"]["max_ms"], 100.0)
self.assertEqual(s["frame_ms"]["p99_ms"], 100.0)
self.assertEqual(s["gpu_ms"]["n"], 0)
self.assertIsNone(s["gpu_ms"]["p95_ms"])
def test_empty(self):
s = rb.frame_summary({})
self.assertEqual(s["frames_over_25ms"], 0)
self.assertIsNone(s["cpu_ms"]["avg_ms"])
class Battle(unittest.TestCase):
def test_schedule_is_ordered_and_early(self):
ticks = [t for t, _ in rb.BATTLE_SCHEDULE]
self.assertEqual(ticks, sorted(ticks))
self.assertLess(ticks[-1], rb.FINGERPRINT_TICKS[0])
self.assertLess(rb.FINGERPRINT_TICKS[-1], rb.BATTLE_END)
def test_fingerprint_regex_reads_the_stats_line(self):
line = ("В воздухе: 40, ракет: 12, ПВО: 14, сбито: 3, поражений: 1, "
"разбились о препятствия: 0; удары по ПУ: пущено 0, уничтожено 0, "
"подавлено 0, промахов 0, сбито ПВО 0; мир: Polygon")
m = rb.STATS_RE.search(line)
self.assertIsNotNone(m)
self.assertEqual(m.groups(), ("40", "12", "14", "3", "1"))
self.assertIsNone(rb.STATS_RE.search("ВОЗДУШНАЯ ТРЕВОГА"))
class CpuProfile(unittest.TestCase):
def test_parse_profile_merges_lines_and_skips_others(self):
lines = [
"В воздухе: 40, ракет: 12, ПВО: 14, сбито: 3, поражений: 1",
"PROFILE steps=200 step_ms=100.500 step_max_ms=6.250 other_ms=10.000 other_max_ms=1.500",
"[AirDef] PROFILE plan_ms=40.000 plan_max_ms=4.500 adapt_ms=2.000 adapt_max_ms=0.500 "
"drones_ms=20.000 drones_max_ms=2.000 moves_ms=5.000 moves_max_ms=1.000",
"PROFILE sight_ms=15.000 sight_max_ms=3.000 sams_ms=8.000 sams_max_ms=2.000 "
"record_ms=10.500 record_max_ms=1.250",
]
p = rb.parse_profile(lines)
self.assertEqual(p["steps"], 200)
self.assertEqual(p["step_ms"], 100.5)
self.assertEqual(p["plan_max_ms"], 4.5)
self.assertEqual(p["record_ms"], 10.5)
self.assertNotIn("воздухе", p)
def test_profile_delta_subtracts_totals_keeps_max(self):
base = {"steps": 100, "plan_ms": 10.0, "plan_max_ms": 2.0, "step_ms": 50.0}
battle = {"steps": 300, "plan_ms": 90.0, "plan_max_ms": 6.0, "step_ms": 250.0}
d = rb.profile_delta(base, battle)
self.assertEqual(d["steps"], 200)
self.assertEqual(d["plan_ms"], 80.0)
self.assertEqual(d["step_ms"], 200.0)
self.assertEqual(d["plan_max_ms"], 6.0) # max kept, not subtracted
self.assertIsNone(rb.profile_delta({}, battle))
def test_mod_cpu_frames_joins_ticks_to_frames(self):
# Two frames; frame 0 runs two ticks, frame 1 one tick.
window = {
"frame_ms": [16.0, 40.0],
"cpu_ms": [10.0, 35.0],
"mod_cpu_ticks": [
{"frame": 0, "tick": 1, "wasm_ms": 5.0, "world_ms": 1.0, "models_ms": 1.0, "total_ms": 8.0},
{"frame": 0, "tick": 2, "wasm_ms": 6.0, "world_ms": 1.0, "models_ms": 1.0, "total_ms": 9.0},
{"frame": 1, "tick": 3, "wasm_ms": 20.0, "world_ms": 2.0, "models_ms": 3.0, "total_ms": 26.0},
],
}
a = rb.mod_cpu_frames(window)
self.assertEqual(a["ticks"], 3)
self.assertEqual(a["frames_with_ticks"], 2)
self.assertEqual(a["per_frame_total_ms"]["max_ms"], 26.0) # frame 1
self.assertEqual(a["frames_mod_over_25ms"], 1)
self.assertEqual(a["frames_mod_over_33ms"], 0)
# The worst CPU frame (index 1) carries the 26 ms mod tick.
self.assertEqual(a["worst_cpu_frames"]["avg_mod_total_ms"], 26.0)
self.assertEqual(a["worst_cpu_frames"]["avg_wasm_ms"], 20.0)
self.assertEqual(a["worst_cpu_frames"]["with_mod_tick"], 1)
def test_mod_cpu_frames_empty(self):
self.assertIsNone(rb.mod_cpu_frames({}))
self.assertIsNone(rb.mod_cpu_frames({"frame_ms": [1.0], "mod_cpu_ticks": []}))
if __name__ == "__main__":
unittest.main()