harvest/game/scripts/perf_test.gd
Forrest aee60b0ba2 #35: interface scale, safe area, and the mobile test kit
MobilePort: ui_scale() (clampf(4.3 / the screen's shorter side in inches, 1, 1.8) times the
Interface size; 1 on desktop), a size-class fallback (below 500 pt = phone, 1.6) when the
density is 0 or implausible, safe_rect(ci) (the display's safe area mapped into ci's canvas
through the final transform), minimum_target() (44 pt in canvas pixels), and the `forced`
override for tests. The desktop preview (-- --mobile) plays the phone in the window:
--safe-area=l,t,r,b insets it, --dpi=N sets its density, and without --dpi a 16:9-or-wider
window is a 2.7 in phone (about 1.6x) and a squarer one a 6 in iPad (1x).

GameSettings: interface_size (0.8-1.3, saved under [touch]) and apply_scale(), which sets the
window's content_scale_factor (always 1 on desktop). PerfTest shows the screen, density, safe
area, and resulting scale, to check the density on a device.

Test kit: test_mobile (last in SUITES) runs game/tests/mobile/*_test.gd in name order and puts
the root window back between files; mobile/touch.gd sends touches through
Input.parse_input_event; scale_test covers phones, iPads, desktop, the safe rect, and the target;
sticks_test is the 8c81d81 driver's 24 checks. game/tools/capture_mobile.gd takes the phone
screenshots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 14:40:06 -04:00

246 lines
9.2 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name PerfTest
extends CanvasLayer
## The iOS test port's readout and buttons (MobilePort.perf_test(); Main adds it), to measure a
## late night on a phone (docs/ios-test-port.md). Over everything, top centre:
## * Now, over the last second: FPS, the frame time (average and worst), the CPU time in
## physics (the farm's tick, FarmSim.step, runs there) and in rendering, the GPU time to draw
## the view, the enemies alive, the 3D view's render size, and the renderer and device. And
## the screen as the device reports it (size, density, safe area) with the interface scale
## that gives (MobilePort.describe), to check the density on a phone.
## * This night, from its start to its end (then held until the next night): the frame time's
## average, 99th percentile, and worst, the share of frames slower than 30 FPS, the peak
## enemies alive, and how long it ran. Frames while paused aren't counted. Printed as one
## "PERF {json}" line at the night's end too.
## * Buttons (taps; they never take gamepad focus): Night 20 / Night 30 (in the morning review,
## CampaignDebug.jump_morning), Unlock all (CampaignDebug.unlock_all), Next phase
## (CampaignController.advance_phase), Shield (the scarecrow takes no hits, as the soak's
## --shield, without its hit flash), and Hide (folds it all into that one button).
## Frame times are measured with the wall clock between _process calls, not with delta, which
## Godot smooths.
const BUCKET_MS := 0.25
const BUCKETS := 400 # 0-100 ms; slower frames land in the last bucket
const SLOW_MS := 1000.0 / 30.0
## The nights the jump buttons go to: the ones docs/thirty-night-soak.md measured on desktop.
const TEST_NIGHTS: Array[int] = [20, 30]
var sim: FarmSim
var debug: CampaignDebug
## The scarecrow takes no hits while on.
var shield := false
var _panel: PanelContainer
var _now: Label
var _night: Label
var _buttons: HBoxContainer
var _jumps: Array[Button] = []
var _shield: Button
var _fold: Button
var _viewport_rid: RID
var _last_usec := 0
# The current second.
var _second_usec := 0
var _second_frames := 0
var _second_worst := 0.0
var _second_render_cpu := 0.0
var _second_physics := 0.0
var _second_physics_worst := 0.0
# The night being measured (or the last one).
var _in_night := false
var _night_number := 0
var _hist := PackedInt32Array()
var _frames := 0
var _sum_ms := 0.0
var _worst_ms := 0.0
var _slow := 0
var _peak_enemies := 0
var _night_usec := 0
func setup(p_sim: FarmSim, p_debug: CampaignDebug) -> void:
sim = p_sim
debug = p_debug
func _ready() -> void:
layer = 20 # over the HUD (1) and the menus (10)
process_mode = Node.PROCESS_MODE_ALWAYS
_viewport_rid = get_viewport().get_viewport_rid()
RenderingServer.viewport_set_measure_render_time(_viewport_rid, true)
_hist.resize(BUCKETS)
var box := VBoxContainer.new()
box.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.add_theme_constant_override("separation", 8)
add_child(box)
box.set_anchors_and_offsets_preset(Control.PRESET_CENTER_TOP)
box.grow_horizontal = Control.GROW_DIRECTION_BOTH
box.offset_top = 12.0
_panel = PanelContainer.new()
_panel.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.add_child(_panel)
var lines := VBoxContainer.new()
_panel.add_child(lines)
_now = _label(lines)
_night = _label(lines)
_night.text = "No night measured yet. In the morning: Night 20, Unlock all, Shield, then Next phase to the night."
_buttons = HBoxContainer.new()
_buttons.alignment = BoxContainer.ALIGNMENT_CENTER
_buttons.add_theme_constant_override("separation", 10)
box.add_child(_buttons)
for night in TEST_NIGHTS:
_jumps.append(_button("Night %d" % night, _on_jump.bind(night)))
_button("Unlock all", _on_unlock)
_button("Next phase", _on_next_phase)
_shield = _button("Shield: off", _on_shield)
_fold = _button("Hide", _on_fold)
if sim != null:
sim.campaign.phase_changed.connect(_on_phase_changed)
func _process(_delta: float) -> void:
var now := Time.get_ticks_usec()
if _last_usec == 0:
_last_usec = now
_second_usec = now
return
var ms := (now - _last_usec) / 1000.0
_last_usec = now
_second_frames += 1
_second_worst = maxf(_second_worst, ms)
_second_render_cpu += RenderingServer.viewport_get_measured_render_time_cpu(_viewport_rid) + RenderingServer.get_frame_setup_time_cpu()
var physics := Performance.get_monitor(Performance.TIME_PHYSICS_PROCESS) * 1000.0
_second_physics += physics
_second_physics_worst = maxf(_second_physics_worst, physics)
if _in_night and not get_tree().paused:
_count(ms)
if shield and sim != null and sim.scarecrow != null and sim.scarecrow.player != null:
sim.scarecrow.player.set_flash(0.0) # after Scarecrow's _process: the pinned shield would flash it all night
if now - _second_usec >= 1000000:
_refresh(now)
func _physics_process(_delta: float) -> void:
if shield and sim != null and sim.scarecrow != null:
sim.scarecrow.invulnerable_left = 1.0 # as soak_night.gd's --shield
func _count(ms: float) -> void:
_frames += 1
_sum_ms += ms
_night_usec += roundi(ms * 1000.0)
_worst_ms = maxf(_worst_ms, ms)
if ms > SLOW_MS:
_slow += 1
_hist[mini(int(ms / BUCKET_MS), BUCKETS - 1)] += 1
_peak_enemies = maxi(_peak_enemies, sim.enemies.alive_count())
## Writes the last second's line and the night's, then starts a new second.
func _refresh(now: int) -> void:
var span := (now - _second_usec) / 1000.0
var n := maxi(_second_frames, 1)
var gpu := RenderingServer.viewport_get_measured_render_time_gpu(_viewport_rid)
var win := get_window()
var size_3d := Vector2(win.size) * win.scaling_3d_scale # the window in real pixels, not the 1920-wide canvas
var enemies := sim.enemies.alive_count() if sim != null else 0
_now.text = "%d fps · frame %.1f ms (worst %.1f) · physics %.1f (worst %.1f) · render CPU %.1f · GPU %s\nEnemies %d · 3D %d×%d · %s/%s · %s" % [
roundi(_second_frames * 1000.0 / span), span / n, _second_worst, _second_physics / n, _second_physics_worst,
_second_render_cpu / n, "%.1f ms" % gpu if gpu > 0.0 else "n/a", enemies, roundi(size_3d.x), roundi(size_3d.y),
RenderingServer.get_current_rendering_method(), RenderingServer.get_current_rendering_driver_name(), OS.get_model_name()]
_now.text += "\n" + MobilePort.describe() # the screen as the phone reports it, and the interface scale it gives
if _in_night:
_night.text = _describe_night("so far")
_second_usec = now
_second_frames = 0
_second_worst = 0.0
_second_render_cpu = 0.0
_second_physics = 0.0
_second_physics_worst = 0.0
var morning := debug != null and sim != null and sim.campaign.state.phase == CampaignController.Phase.MORNING
for b: Button in _jumps:
b.disabled = not morning
func _on_phase_changed(phase: CampaignController.Phase, previous: CampaignController.Phase) -> void:
if phase == CampaignController.Phase.NIGHT:
_in_night = true
_night_number = sim.campaign.state.night
_hist.fill(0)
_frames = 0
_sum_ms = 0.0
_worst_ms = 0.0
_slow = 0
_peak_enemies = 0
_night_usec = 0
elif previous == CampaignController.Phase.NIGHT and _in_night:
_in_night = false
_night.text = _describe_night("done")
print("PERF " + JSON.stringify({"night": _night_number, "seconds": snappedf(_night_usec / 1e6, 0.1), "frames": _frames,
"avg_ms": snappedf(_sum_ms / maxi(_frames, 1), 0.01), "p99_ms": _percentile(0.99), "worst_ms": snappedf(_worst_ms, 0.01),
"slow_share": snappedf(float(_slow) / maxi(_frames, 1), 0.0001), "peak_enemies": _peak_enemies,
"renderer": RenderingServer.get_current_rendering_method(), "model": OS.get_model_name()}))
func _describe_night(status: String) -> String:
var n := maxi(_frames, 1)
return "Night %d (%s, %d s): frame avg %.1f ms · p99 %.1f · worst %.1f · %.1f%% slower than 30 FPS · peak enemies %d" % [
_night_number, status, roundi(_night_usec / 1e6), _sum_ms / n, _percentile(0.99), _worst_ms, 100.0 * _slow / n, _peak_enemies]
## The frame time (ms) that the share p of this night's frames came in under (bucket's top edge).
func _percentile(p: float) -> float:
var target := ceili(_frames * p)
var seen := 0
for i in BUCKETS:
seen += _hist[i]
if seen >= target:
return (i + 1) * BUCKET_MS
return BUCKETS * BUCKET_MS
func _on_jump(night: int) -> void:
if debug != null and sim != null:
debug.jump_morning(night - sim.campaign.state.night)
func _on_unlock() -> void:
if debug != null:
debug.unlock_all()
func _on_next_phase() -> void:
if sim != null:
sim.campaign.advance_phase()
func _on_shield() -> void:
shield = not shield
_shield.text = "Shield: on" if shield else "Shield: off"
func _on_fold() -> void:
var fold := _panel.visible
_panel.visible = not fold
for b in _buttons.get_children():
if b != _fold:
b.visible = not fold
_fold.text = "Perf" if fold else "Hide"
func _label(parent: Control) -> Label:
var l := Label.new()
l.add_theme_font_size_override("font_size", 22)
l.add_theme_color_override("font_outline_color", Color.BLACK)
l.add_theme_constant_override("outline_size", 6)
parent.add_child(l)
return l
func _button(text: String, on_press: Callable) -> Button:
var b := Button.new()
b.text = text
b.focus_mode = Control.FOCUS_NONE # taps only: the gamepad keeps driving the game and menus
b.custom_minimum_size = Vector2(170.0, 60.0)
b.pressed.connect(on_press)
_buttons.add_child(b)
return b