Add benchmark for viewport creation/activation (#61)

Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro>
This commit is contained in:
jamie-pate
2024-04-18 07:15:41 -07:00
committed by GitHub
parent 7653e5cacc
commit d0977987ee
3 changed files with 79 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
extends Benchmark
# In Godot, a lot of the cost of Viewport nodes comes during activation.
# That is, when you create it, add it to the tree, set the size and allocate the viewport texture...
# The point when the underlying render target is created is a huge weak point in some GPU drivers.
const allocation = preload("./allocation.gd")
class TestScene extends Node2D:
var viewports: Array[SubViewport] = []
var viewports_added := false
func _init(count: int):
viewports = allocation.create_viewports(count)
func _ready():
var cl := CanvasLayer.new()
add_child(cl)
var i := 0
for vp in viewports:
var vpc = SubViewportContainer.new()
vpc.add_child(vp)
vpc.size = Vector2.ONE * allocation.VIEWPORT_SIZE
cl.add_child(vpc)
vpc.position = Vector2.ONE * ((i * 10) % 1024)
i += 1
func activate_viewports(count: int):
var node = TestScene.new(count)
Manager.get_tree().root.add_child(node)
await Manager.get_tree().process_frame
node.free()
func benchmark_activate_64_viewports():
await activate_viewports(64)
func benchmark_activate_256_viewports():
await activate_viewports(256)
func benchmark_activate_1024_viewports():
await activate_viewports(1024)

View File

@@ -0,0 +1,32 @@
extends Benchmark
const VIEWPORT_SIZE = 512
func _init() -> void:
pass
static func create_viewports(count: int) -> Array[SubViewport]:
var viewports : Array[SubViewport] = []
for i in range(count):
var vp := SubViewport.new()
vp.size = Vector2.ONE * VIEWPORT_SIZE
viewports.append(vp)
return viewports
static func free_items(items: Array):
for item in items:
item.free()
func benchmark_create_64_viewports():
free_items(create_viewports(64))
func benchmark_create_256_viewports():
free_items(create_viewports(256))
func benchmark_create_1024_viewports():
free_items(create_viewports(1024))

View File

@@ -152,7 +152,8 @@ func run_test(test_id: TestID) -> void:
# Call and time the function to be tested
var begin_time := Time.get_ticks_usec()
var bench_node = bench_script.call(languages[test_id.language]["test_prefix"] + test_id.name)
# Redundant awaits don't seem to cause a performance variation.
var bench_node = await bench_script.call(languages[test_id.language]["test_prefix"] + test_id.name)
results.time = (Time.get_ticks_usec() - begin_time) * 0.001
# Continue benchmarking if the function call has returned a node