1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# Author: cirakg & claude ai
# Summary: Tests for tools/check_budgets.py (start-up time and memory budgets).
"""Tests for tools/check_budgets.py (perf and memory budgets)."""
from __future__ import annotations
import json
from collections.abc import Mapping
from pathlib import Path
import check_budgets as cb
FRAME = "DIAG first_frame startup_ms=12 mem_used=100000 mem_total=524288"
WALL = "HARNESS first_frame wall_ms=2310"
def test_reads_events_and_skips_noise() -> None:
text = f"Device Version 1\n{FRAME}\nDIAG broken\n{WALL}\nDIAG first_frame startup_ms=99 mem_used=1 mem_total=2\n"
events = cb.read_events(text)
# Only the first occurrence of each event counts.
assert events == {
"DIAG.first_frame": {"startup_ms": 12.0, "mem_used": 100000.0, "mem_total": 524288.0},
"HARNESS.first_frame": {"wall_ms": 2310.0},
}
def test_measure_perf_and_memory() -> None:
events = cb.read_events(f"{FRAME}\n{WALL}")
assert cb.measure("perf", events) == {"startup_ms": 12.0, "first_frame_wall_ms": 2310.0}
assert cb.measure("mem", events) == {"mem_used_fraction": 100000 / 524288}
assert cb.measure("perf", {}) == {}
def run(tmp_path: Path, kind: str, log_text: str, budgets: Mapping[str, Mapping[str, float]]) -> int:
log = tmp_path / "fr255s.log"
log.write_text(log_text, encoding="utf-8")
budget_file = tmp_path / "budgets.json"
budget_file.write_text(json.dumps(budgets), encoding="utf-8")
return cb.main([kind, str(log), "--budgets", str(budget_file), "--summary", str(tmp_path / "s.json")])
def test_within_budget(tmp_path: Path) -> None:
budgets = {"perf": {"startup_ms": 300, "first_frame_wall_ms": 5000}}
assert run(tmp_path, "perf", f"{FRAME}\n{WALL}", budgets) == cb.EXIT_OK
summary = json.loads((tmp_path / "s.json").read_text())
assert summary == {"fr255s": {"startup_ms": 12.0, "first_frame_wall_ms": 2310.0}}
def test_over_budget(tmp_path: Path) -> None:
assert run(tmp_path, "mem", FRAME, {"mem": {"mem_used_fraction": 0.1}}) == cb.EXIT_OVER
def test_budgeted_value_not_measured(tmp_path: Path) -> None:
budgets = {"perf": {"first_frame_wall_ms": 5000}}
assert run(tmp_path, "perf", FRAME, budgets) == cb.EXIT_MISSING
def test_missing_diagnostics(tmp_path: Path) -> None:
assert run(tmp_path, "perf", "app crashed\n", {"perf": {"startup_ms": 300}}) == cb.EXIT_MISSING
|