test_perf_broadphase.gd 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. extends Test
  2. const BOX_SIZE = Vector2(40, 40)
  3. const BOX_SPACE = Vector2(50, 50)
  4. @export_range(1, 1000) var row_size = 100
  5. @export_range(1, 1000) var column_size = 100
  6. var _objects = []
  7. var _log_physics = false
  8. var _log_physics_time = 0
  9. var _log_physics_time_start = 0
  10. func _ready():
  11. await start_timer(1.0).timeout
  12. if is_timer_canceled():
  13. return
  14. _log_physics_start()
  15. _create_objects()
  16. await wait_for_physics_ticks(5).wait_done
  17. _log_physics_stop()
  18. await start_timer(1.0).timeout
  19. if is_timer_canceled():
  20. return
  21. _log_physics_start()
  22. _add_objects()
  23. await wait_for_physics_ticks(5).wait_done
  24. _log_physics_stop()
  25. await start_timer(1.0).timeout
  26. if is_timer_canceled():
  27. return
  28. _log_physics_start()
  29. _move_objects()
  30. await wait_for_physics_ticks(5).wait_done
  31. _log_physics_stop()
  32. await start_timer(1.0).timeout
  33. if is_timer_canceled():
  34. return
  35. _log_physics_start()
  36. _remove_objects()
  37. await wait_for_physics_ticks(5).wait_done
  38. _log_physics_stop()
  39. await start_timer(1.0).timeout
  40. if is_timer_canceled():
  41. return
  42. Log.print_log("* Done.")
  43. func _exit_tree():
  44. for object in _objects:
  45. object.free()
  46. func _physics_process(delta):
  47. super._physics_process(delta)
  48. if _log_physics:
  49. var time = Time.get_ticks_usec()
  50. var time_delta = time - _log_physics_time
  51. var time_total = time - _log_physics_time_start
  52. _log_physics_time = time
  53. Log.print_log(" Physics Tick: %.3f ms (total = %.3f ms)" % [0.001 * time_delta, 0.001 * time_total])
  54. func _log_physics_start():
  55. _log_physics = true
  56. _log_physics_time_start = Time.get_ticks_usec()
  57. _log_physics_time = _log_physics_time_start
  58. func _log_physics_stop():
  59. _log_physics = false
  60. func _create_objects():
  61. _objects.clear()
  62. Log.print_log("* Creating objects...")
  63. var timer = Time.get_ticks_usec()
  64. var pos_x = -0.5 * (row_size - 1) * BOX_SPACE.x
  65. for row in row_size:
  66. var pos_y = -0.5 * (column_size - 1) * BOX_SPACE.y
  67. for column in column_size:
  68. # Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape.
  69. var box = create_rigidbody_box(BOX_SIZE)
  70. box.gravity_scale = 0.0
  71. box.position = Vector2(pos_x, pos_y)
  72. _objects.push_back(box)
  73. pos_y += BOX_SPACE.y
  74. pos_x += BOX_SPACE.x
  75. timer = Time.get_ticks_usec() - timer
  76. Log.print_log(" Create Time: %.3f ms" % (0.001 * timer))
  77. func _add_objects():
  78. var root_node = $Objects
  79. Log.print_log("* Adding objects...")
  80. var timer = Time.get_ticks_usec()
  81. for object in _objects:
  82. root_node.add_child(object)
  83. timer = Time.get_ticks_usec() - timer
  84. Log.print_log(" Add Time: %.3f ms" % (0.001 * timer))
  85. func _move_objects():
  86. Log.print_log("* Moving objects...")
  87. var timer = Time.get_ticks_usec()
  88. for object in _objects:
  89. object.position += BOX_SPACE
  90. timer = Time.get_ticks_usec() - timer
  91. Log.print_log(" Move Time: %.3f ms" % (0.001 * timer))
  92. func _remove_objects():
  93. var root_node = $Objects
  94. Log.print_log("* Removing objects...")
  95. var timer = Time.get_ticks_usec()
  96. # Remove objects in reversed order to avoid the overhead of changing children index in parent.
  97. var object_count = _objects.size()
  98. for object_index in range(object_count):
  99. root_node.remove_child(_objects[object_count - object_index - 1])
  100. timer = Time.get_ticks_usec() - timer
  101. Log.print_log(" Remove Time: %.3f ms" % (0.001 * timer))