extends TestCase ## The long-run stability report (H4): what counts as a clean run of back-to-back ## matches, and that a memory trend, a hang, an error or a missing match fails it. func _log(matches: int, static_growth: float, seconds: float = 20.0, winners: bool = true) -> PackedStringArray: var lines: PackedStringArray = ["Godot Engine v4.7.2.stable.official - https://godotengine.org"] var tick: int = 0 var t_ms: float = 0.0 for index: int in matches: if index > 0: lines.append(JSON.stringify({"event": "match", "type": "match_started", "match": 1000 + index, "index": index, "tick": tick, "t_ms": t_ms, "mem_bytes": 70000000})) tick += 25000 t_ms += seconds * 1000.0 lines.append(JSON.stringify({"event": "match", "type": "match_ended", "match": 1000 + index, "winners": [-3] if winners else [], "timed_out": false, "tick": tick, "t_ms": t_ms, "mem_bytes": 70000000 + roundi(static_growth * index)})) lines.append(JSON.stringify({"event": "stop", "exit_code": 0, "errors": 0, "tick": tick, "t_ms": t_ms})) return lines func _rss(matches: int, growth_kb: float, seconds: float = 20.0) -> PackedStringArray: var lines: PackedStringArray = [] for index: int in matches * 4 + 1: var at: float = index * seconds / 4.0 lines.append("%d %d" % [roundi(at), 200000 + roundi(growth_kb * at / seconds)]) return lines func test_slope_is_least_squares() -> void: assert_eq(StabilityReport.slope([1.0, 3.0, 5.0] as Array[float]), 2.0) assert_eq(StabilityReport.slope([4.0, 4.0, 4.0, 4.0] as Array[float]), 0.0) assert_eq(StabilityReport.slope([7.0] as Array[float]), 0.0) func test_a_clean_flat_run_passes() -> void: var a: Dictionary = StabilityReport.analyse(_log(50, 1000.0), _rss(50, 10.0), 0, "", 50, 240.0) assert_eq(a["problems"], [], "%s" % [a["problems"]]) assert_eq(a["ended"], 50) assert_eq(a["matches"][0]["ticks"], 25000, "the first match counts from the start") assert_near(a["static_slope"], 1000.0, 1.0) assert_true(a["rss_slope"] > 0.0 and a["rss_slope"] < 20.0 * 1024.0) assert_true(StabilityReport.format(a).contains("stability: ok")) func test_growth_hangs_errors_and_missing_matches_fail() -> void: var leak: Dictionary = StabilityReport.analyse(_log(50, 512.0 * 1024.0), _rss(50, 0.0), 0, "", 50, 240.0) assert_eq(leak["problems"].size(), 1, "%s" % [leak["problems"]]) assert_true(String(leak["problems"][0]).begins_with("static memory grows 512 KiB")) var rss: Dictionary = StabilityReport.analyse(_log(50, 0.0), _rss(50, 4096.0), 0, "", 50, 240.0) assert_true(String(rss["problems"][0]).begins_with("resident memory grows"), "%s" % [rss["problems"]]) var slow: Dictionary = StabilityReport.analyse(_log(20, 0.0, 300.0), _rss(20, 0.0, 300.0), 0, "", 20, 240.0) assert_eq(slow["problems"].size(), 20, "every match over the hang limit") var lines: PackedStringArray = _log(20, 0.0) lines.insert(3, "SCRIPT ERROR: Invalid call. Nonexistent function 'x' in base 'Nil'.") var broken: Dictionary = StabilityReport.analyse(lines, _rss(20, 0.0), 1, "", 20, 240.0) var text: String = StabilityReport.format(broken) assert_true(text.contains("the server exited 1")) assert_true(text.contains("1 error lines, first: SCRIPT ERROR")) var short: Dictionary = StabilityReport.analyse(_log(30, 0.0, 20.0, false), _rss(30, 0.0), 0, "over the 7320 s limit: a hang", 50, 240.0) var short_text: String = StabilityReport.format(short) assert_true(short_text.contains("the run was stopped: over the 7320 s limit")) assert_true(short_text.contains("30 of 50 matches ended")) assert_true(short_text.contains("ended with no winner"))