Files
airdef/tools/test_raid_bench.py
T
chexit aaf72ef171
build / mod (push) Failing after 17s
Fix HD benchmark reporting and record renderer A/B results (#147)
Resolve benchmark input paths before switching to the disposable game home and report startup failures without a missing screenshot traceback. Capture the executable hash and keep report environment metadata limited to benchmark settings.

Record the earlier light and smoke measurements and the repeated idle A/B runs. Verified with 18 Python benchmark tests, 36 Horizon HD tests, the Metal TAAU smoke test, and the real idle launch and error paths.
2026-11-24 18:24:00 +03:00

137 lines
6.4 KiB
Python

"""Тесты драйвера raid_bench (#147): перцентили как в FrameStats, худшие кадры,
расписание боя и разбор отпечатка. Запуск: python3 -m unittest tools/test_raid_bench.py."""
import os
import sys
import unittest
from pathlib import Path
from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parent))
import raid_bench as rb # noqa: E402
class ReportEnvironment(unittest.TestCase):
def test_keeps_benchmark_settings_and_excludes_credentials(self):
driver = rb.Driver("out", "game", "resources", "airdef.hmod",
{"HORIZON_HD_SCALE": "0.67", "EXAMPLE_PASSWORD": "test"}, 1280, 720)
with patch.dict(os.environ, {"HORIZON_HD_WATER": "0", "EXAMPLE_API_KEY": "test"}, clear=True):
env = driver.report_env()
self.assertEqual(env["HORIZON_HD_SCALE"], "0.67")
self.assertEqual(env["HORIZON_HD_WATER"], "0")
self.assertEqual(env["HORIZON_MOD_CPU_PROFILE"], "1")
self.assertNotIn("EXAMPLE_API_KEY", env)
self.assertNotIn("EXAMPLE_PASSWORD", env)
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()