TableTower/ui/main_cli_perf.gd

77 lines
3.2 KiB
GDScript3
Raw Normal View History

class_name MainCliPerf
extends RefCounted
## The --perf command-line flag: it times how long the board takes to load and
## how long each frame takes (idle and while panning), prints the numbers and
## quits. MainCli uses it; only automation needs this file.
var m: Main
func _init(p_main: Main) -> void:
m = p_main
## --perf: times a reload of the current board and the frame cost while idle
## and while panning (vsync off), then prints the numbers and quits.
func run_perf() -> void:
DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
Engine.max_fps = 0
var text := m.data.to_json()
for i in 10:
await m.get_tree().process_frame
var start_usec := Time.get_ticks_usec()
m.data.load_json(text)
var parsed_usec := Time.get_ticks_usec()
m._rebuild_cards()
var rebuilt_usec := Time.get_ticks_usec()
m.rolling._seed_session()
var seeded_usec := Time.get_ticks_usec()
await m.get_tree().process_frame
await m.get_tree().process_frame
var drawn_usec := Time.get_ticks_usec()
m.board.fit()
print("PERF load: parse %.1f ms, rebuild_cards %.1f ms, seed %.1f ms, first two frames %.1f ms, total %.1f ms" % [
(parsed_usec - start_usec) / 1000.0, (rebuilt_usec - parsed_usec) / 1000.0, (seeded_usec - rebuilt_usec) / 1000.0,
(drawn_usec - seeded_usec) / 1000.0, (drawn_usec - start_usec) / 1000.0])
print("PERF nodes: %d, table cards built: %d of %d" % [Performance.get_monitor(Performance.OBJECT_NODE_COUNT), m.cards.size(), m.data.tables.size()])
for i in 30:
await m.get_tree().process_frame
print("PERF idle (fit view): %s" % await _perf_frames(240, Vector2.ZERO))
m.board.set_zoom(0.6)
print("PERF pan (zoom 0.6): %s" % await _perf_frames(240, Vector2(-6, -2)))
m.board.set_zoom(1.0)
print("PERF idle (zoom 1): %s" % await _perf_frames(240, Vector2.ZERO))
print("PERF pan (zoom 1): %s" % await _perf_frames(240, Vector2(-6, -2)))
if OS.get_cmdline_user_args().has("--perf-layers"):
m.board.fit()
print("PERF idle (fit view again): %s" % await _perf_frames(120, Vector2.ZERO))
m.board.pipes.visible = false
print("PERF idle, no pipes: %s" % await _perf_frames(120, Vector2.ZERO))
m.board.ports.visible = false
print("PERF idle, no pipes or ports: %s" % await _perf_frames(120, Vector2.ZERO))
m.board.cards.visible = false
print("PERF idle, no cards either: %s" % await _perf_frames(120, Vector2.ZERO))
m.board.set_process(false)
m.board.groups.set_process(false)
print("PERF idle, board and group processing off: %s" % await _perf_frames(120, Vector2.ZERO))
m.get_tree().quit()
## Waits `frame_count` frames (moving the view by `pan` each frame) and returns
## the mean, median and 95th-percentile frame times as text.
func _perf_frames(frame_count: int, pan: Vector2) -> String:
var times: Array[float] = []
var last := Time.get_ticks_usec()
for i in frame_count:
if pan != Vector2.ZERO:
m.board.world.position += pan
await m.get_tree().process_frame
var now := Time.get_ticks_usec()
times.append((now - last) / 1000.0)
last = now
times.sort()
var total := 0.0
for frame_ms in times:
total += frame_ms
return "mean %.2f ms, median %.2f ms, p95 %.2f ms" % [total / frame_count, times[frame_count / 2], times[int(frame_count * 0.95)]]