test_perf_broadphase.gd 3.5 KB

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