extends SceneTree var _checks := 0 var _failures := 0 func _initialize() -> void: call_deferred("_run") func _run() -> void: print("S6: performance budgets") var profiler := SimulationPerformanceBudget.new(4) _expect(not profiler.record_sample(&"", 10)[&"ok"], "empty metric ID is rejected") _expect(not profiler.record_sample(&"path_query", -1)[&"ok"], "negative duration is rejected") _expect(not profiler.set_budget(&"bad", 0), "non-positive budget is rejected") _expect(profiler.set_budget(&"custom", 100, SimulationPerformanceBudget.Aggregation.P95), "custom budget registers") for value: int in [10, 20, 30, 40, 50]: profiler.record_sample(&"custom", value) var custom := profiler.get_report(&"custom") _expect(custom[&"window_sample_count"] == 4, "sample window remains bounded") _expect(custom[&"lifetime_sample_count"] == 5, "lifetime count survives window eviction") _expect(custom[&"average_us"] == 30, "lifetime average uses every recorded sample") _expect(custom[&"maximum_us"] == 50, "window maximum is exact") _expect(custom[&"p95_us"] == 50, "nearest-rank P95 is deterministic") _expect(not custom[&"budget_exceeded"], "measurement below limit passes") var exceeded: Array[StringName] = [] profiler.budget_exceeded.connect(func(metric_id: StringName, _report: Dictionary) -> void: exceeded.append(metric_id) ) profiler.record_sample(&"custom", 101) _expect(profiler.get_report(&"custom")[&"budget_exceeded"], "P95 above limit fails") _expect(exceeded == [&"custom"], "budget violation emits its exact metric once") profiler.record_simulation_tick(3_000, 1) profiler.record_simulation_tick(11_000, 3) _expect(profiler.get_report(&"simulation_x1")[&"limit_us"] == 4_000, "x1 uses the MVP average budget") _expect(profiler.get_report(&"simulation_x3")[&"limit_us"] == 12_000, "x3 uses the MVP P95 budget") _expect(not profiler.get_report(&"simulation_x3")[&"budget_exceeded"], "x3 sample within budget passes") profiler.record_sample(&"path_query", 2_001) _expect(profiler.get_report(&"path_query")[&"budget_exceeded"], "path P95 enforces two milliseconds") var reports := profiler.get_all_reports() var ids: Array[String] = [] for report: Dictionary in reports: ids.append(String(report[&"metric_id"])) var sorted_ids := ids.duplicate() sorted_ids.sort() _expect(ids == sorted_ids, "all reports are returned in stable metric order") profiler.clear(&"custom") _expect(profiler.get_report(&"custom")[&"lifetime_sample_count"] == 0, "single metric can be cleared") profiler.clear() _expect(profiler.get_report(&"path_query")[&"lifetime_sample_count"] == 0, "all diagnostic samples can be cleared") var clock = load("res://autoload/game_clock.gd").new() clock.reset(1, 6, 0) clock.process_real_delta(clock.REAL_SECONDS_PER_GAME_MINUTE) _expect( clock.performance_budget.get_report(&"simulation_x1")[&"lifetime_sample_count"] == 1, "real-delta Kernel ticks feed the diagnostic profiler" ) _expect(not clock.get_performance_reports().is_empty(), "Kernel exposes read-only performance reports") clock.reset(1, 6, 0) clock.set_speed(3) var accelerated_ticks = clock.process_real_delta(clock.REAL_SECONDS_PER_GAME_MINUTE) _expect(accelerated_ticks == 3, "x3 process frame advances the expected three game-minute ticks") _expect(clock.performance_budget.get_report(&"simulation_x3")[&"lifetime_sample_count"] == 1, "x3 performance measures the aggregate frame once rather than three isolated ticks") _expect(clock.performance_budget.get_report(&"main_thread_stall")[&"lifetime_sample_count"] == 1, "active simulation frames feed the main-thread stall budget") clock.free() if _failures == 0: print("PASS: %d checks" % _checks) else: push_error("FAIL: %d of %d checks failed" % [_failures, _checks]) quit(_failures) func _expect(condition: bool, label: String) -> void: _checks += 1 if condition: print(" ✓ %s" % label) else: _failures += 1 push_error(" ✗ %s" % label)