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>
This commit is contained in:
2026-11-21 12:29:22 +03:00
parent c08b737296
commit 92c058f606
6 changed files with 368 additions and 28 deletions
+54
View File
@@ -63,5 +63,59 @@ class Battle(unittest.TestCase):
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()