class_performance.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the Performance.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_Performance:
  6. Performance
  7. ===========
  8. **Inherits:** :ref:`Object<class_Object>`
  9. Exposes performance-related data.
  10. Description
  11. -----------
  12. This class provides access to a number of different monitors related to performance, such as memory usage, draw calls, and FPS. These are the same as the values displayed in the **Monitor** tab in the editor's **Debugger** panel. By using the :ref:`get_monitor<class_Performance_method_get_monitor>` method of this class, you can access this data from your code.
  13. You can add custom monitors using the :ref:`add_custom_monitor<class_Performance_method_add_custom_monitor>` method. Custom monitors are available in **Monitor** tab in the editor's **Debugger** panel together with built-in monitors.
  14. \ **Note:** A few of these monitors are only available in debug mode and will always return 0 when used in a release build.
  15. \ **Note:** Many of these monitors are not updated in real-time, so there may be a short delay between changes.
  16. \ **Note:** Custom monitors do not support negative values. Negative values are clamped to 0.
  17. Methods
  18. -------
  19. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | void | :ref:`add_custom_monitor<class_Performance_method_add_custom_monitor>` **(** :ref:`StringName<class_StringName>` id, :ref:`Callable<class_Callable>` callable, :ref:`Array<class_Array>` arguments=[] **)** |
  21. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`Variant<class_Variant>` | :ref:`get_custom_monitor<class_Performance_method_get_custom_monitor>` **(** :ref:`StringName<class_StringName>` id **)** |
  23. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | :ref:`Array<class_Array>` | :ref:`get_custom_monitor_names<class_Performance_method_get_custom_monitor_names>` **(** **)** |
  25. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :ref:`float<class_float>` | :ref:`get_monitor<class_Performance_method_get_monitor>` **(** :ref:`Monitor<enum_Performance_Monitor>` monitor **)** |const| |
  27. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | :ref:`int<class_int>` | :ref:`get_monitor_modification_time<class_Performance_method_get_monitor_modification_time>` **(** **)** |
  29. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. | :ref:`bool<class_bool>` | :ref:`has_custom_monitor<class_Performance_method_has_custom_monitor>` **(** :ref:`StringName<class_StringName>` id **)** |
  31. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | void | :ref:`remove_custom_monitor<class_Performance_method_remove_custom_monitor>` **(** :ref:`StringName<class_StringName>` id **)** |
  33. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  34. Enumerations
  35. ------------
  36. .. _enum_Performance_Monitor:
  37. .. _class_Performance_constant_TIME_FPS:
  38. .. _class_Performance_constant_TIME_PROCESS:
  39. .. _class_Performance_constant_TIME_PHYSICS_PROCESS:
  40. .. _class_Performance_constant_MEMORY_STATIC:
  41. .. _class_Performance_constant_MEMORY_STATIC_MAX:
  42. .. _class_Performance_constant_MEMORY_MESSAGE_BUFFER_MAX:
  43. .. _class_Performance_constant_OBJECT_COUNT:
  44. .. _class_Performance_constant_OBJECT_RESOURCE_COUNT:
  45. .. _class_Performance_constant_OBJECT_NODE_COUNT:
  46. .. _class_Performance_constant_OBJECT_ORPHAN_NODE_COUNT:
  47. .. _class_Performance_constant_RENDER_TOTAL_OBJECTS_IN_FRAME:
  48. .. _class_Performance_constant_RENDER_TOTAL_PRIMITIVES_IN_FRAME:
  49. .. _class_Performance_constant_RENDER_TOTAL_DRAW_CALLS_IN_FRAME:
  50. .. _class_Performance_constant_RENDER_VIDEO_MEM_USED:
  51. .. _class_Performance_constant_RENDER_TEXTURE_MEM_USED:
  52. .. _class_Performance_constant_RENDER_BUFFER_MEM_USED:
  53. .. _class_Performance_constant_PHYSICS_2D_ACTIVE_OBJECTS:
  54. .. _class_Performance_constant_PHYSICS_2D_COLLISION_PAIRS:
  55. .. _class_Performance_constant_PHYSICS_2D_ISLAND_COUNT:
  56. .. _class_Performance_constant_PHYSICS_3D_ACTIVE_OBJECTS:
  57. .. _class_Performance_constant_PHYSICS_3D_COLLISION_PAIRS:
  58. .. _class_Performance_constant_PHYSICS_3D_ISLAND_COUNT:
  59. .. _class_Performance_constant_AUDIO_OUTPUT_LATENCY:
  60. .. _class_Performance_constant_MONITOR_MAX:
  61. enum **Monitor**:
  62. - **TIME_FPS** = **0** --- Number of frames per second.
  63. - **TIME_PROCESS** = **1** --- Time it took to complete one frame, in seconds.
  64. - **TIME_PHYSICS_PROCESS** = **2** --- Time it took to complete one physics frame, in seconds.
  65. - **MEMORY_STATIC** = **3** --- Static memory currently used, in bytes. Not available in release builds.
  66. - **MEMORY_STATIC_MAX** = **4** --- Available static memory. Not available in release builds.
  67. - **MEMORY_MESSAGE_BUFFER_MAX** = **5** --- Largest amount of memory the message queue buffer has used, in bytes. The message queue is used for deferred functions calls and notifications.
  68. - **OBJECT_COUNT** = **6** --- Number of objects currently instantiated (including nodes).
  69. - **OBJECT_RESOURCE_COUNT** = **7** --- Number of resources currently used.
  70. - **OBJECT_NODE_COUNT** = **8** --- Number of nodes currently instantiated in the scene tree. This also includes the root node.
  71. - **OBJECT_ORPHAN_NODE_COUNT** = **9** --- Number of orphan nodes, i.e. nodes which are not parented to a node of the scene tree.
  72. - **RENDER_TOTAL_OBJECTS_IN_FRAME** = **10**
  73. - **RENDER_TOTAL_PRIMITIVES_IN_FRAME** = **11**
  74. - **RENDER_TOTAL_DRAW_CALLS_IN_FRAME** = **12**
  75. - **RENDER_VIDEO_MEM_USED** = **13** --- The amount of video memory used, i.e. texture and vertex memory combined.
  76. - **RENDER_TEXTURE_MEM_USED** = **14** --- The amount of texture memory used.
  77. - **RENDER_BUFFER_MEM_USED** = **15**
  78. - **PHYSICS_2D_ACTIVE_OBJECTS** = **16** --- Number of active :ref:`RigidDynamicBody2D<class_RigidDynamicBody2D>` nodes in the game.
  79. - **PHYSICS_2D_COLLISION_PAIRS** = **17** --- Number of collision pairs in the 2D physics engine.
  80. - **PHYSICS_2D_ISLAND_COUNT** = **18** --- Number of islands in the 2D physics engine.
  81. - **PHYSICS_3D_ACTIVE_OBJECTS** = **19** --- Number of active :ref:`RigidDynamicBody3D<class_RigidDynamicBody3D>` and :ref:`VehicleBody3D<class_VehicleBody3D>` nodes in the game.
  82. - **PHYSICS_3D_COLLISION_PAIRS** = **20** --- Number of collision pairs in the 3D physics engine.
  83. - **PHYSICS_3D_ISLAND_COUNT** = **21** --- Number of islands in the 3D physics engine.
  84. - **AUDIO_OUTPUT_LATENCY** = **22** --- Output latency of the :ref:`AudioServer<class_AudioServer>`.
  85. - **MONITOR_MAX** = **23** --- Represents the size of the :ref:`Monitor<enum_Performance_Monitor>` enum.
  86. Method Descriptions
  87. -------------------
  88. .. _class_Performance_method_add_custom_monitor:
  89. - void **add_custom_monitor** **(** :ref:`StringName<class_StringName>` id, :ref:`Callable<class_Callable>` callable, :ref:`Array<class_Array>` arguments=[] **)**
  90. Adds a custom monitor with name same as id. You can specify the category of monitor using '/' in id. If there are more than one '/' then default category is used. Default category is "Custom".
  91. .. tabs::
  92. .. code-tab:: gdscript
  93. func _ready():
  94. var monitor_value = Callable(self, "get_monitor_value")
  95. # Adds monitor with name "MyName" to category "MyCategory".
  96. Performance.add_custom_monitor("MyCategory/MyMonitor", monitor_value)
  97. # Adds monitor with name "MyName" to category "Custom".
  98. # Note: "MyCategory/MyMonitor" and "MyMonitor" have same name but different ids so the code is valid.
  99. Performance.add_custom_monitor("MyMonitor", monitor_value)
  100. # Adds monitor with name "MyName" to category "Custom".
  101. # Note: "MyMonitor" and "Custom/MyMonitor" have same name and same category but different ids so the code is valid.
  102. Performance.add_custom_monitor("Custom/MyMonitor", monitor_value)
  103. # Adds monitor with name "MyCategoryOne/MyCategoryTwo/MyMonitor" to category "Custom".
  104. Performance.add_custom_monitor("MyCategoryOne/MyCategoryTwo/MyMonitor", monitor_value)
  105. func get_monitor_value():
  106. return randi() % 25
  107. .. code-tab:: csharp
  108. public override void _Ready()
  109. {
  110. var monitorValue = new Callable(this, nameof(GetMonitorValue));
  111. // Adds monitor with name "MyName" to category "MyCategory".
  112. Performance.AddCustomMonitor("MyCategory/MyMonitor", monitorValue);
  113. // Adds monitor with name "MyName" to category "Custom".
  114. // Note: "MyCategory/MyMonitor" and "MyMonitor" have same name but different ids so the code is valid.
  115. Performance.AddCustomMonitor("MyMonitor", monitorValue);
  116. // Adds monitor with name "MyName" to category "Custom".
  117. // Note: "MyMonitor" and "Custom/MyMonitor" have same name and same category but different ids so the code is valid.
  118. Performance.AddCustomMonitor("Custom/MyMonitor", monitorValue);
  119. // Adds monitor with name "MyCategoryOne/MyCategoryTwo/MyMonitor" to category "Custom".
  120. Performance.AddCustomMonitor("MyCategoryOne/MyCategoryTwo/MyMonitor", monitorValue);
  121. }
  122. public int GetMonitorValue()
  123. {
  124. return GD.Randi() % 25;
  125. }
  126. The debugger calls the callable to get the value of custom monitor. The callable must return a number.
  127. Callables are called with arguments supplied in argument array.
  128. \ **Note:** It throws an error if given id is already present.
  129. ----
  130. .. _class_Performance_method_get_custom_monitor:
  131. - :ref:`Variant<class_Variant>` **get_custom_monitor** **(** :ref:`StringName<class_StringName>` id **)**
  132. Returns the value of custom monitor with given id. The callable is called to get the value of custom monitor.
  133. \ **Note:** It throws an error if the given id is absent.
  134. ----
  135. .. _class_Performance_method_get_custom_monitor_names:
  136. - :ref:`Array<class_Array>` **get_custom_monitor_names** **(** **)**
  137. Returns the names of active custom monitors in an array.
  138. ----
  139. .. _class_Performance_method_get_monitor:
  140. - :ref:`float<class_float>` **get_monitor** **(** :ref:`Monitor<enum_Performance_Monitor>` monitor **)** |const|
  141. Returns the value of one of the available monitors. You should provide one of the :ref:`Monitor<enum_Performance_Monitor>` constants as the argument, like this:
  142. .. tabs::
  143. .. code-tab:: gdscript
  144. print(Performance.get_monitor(Performance.TIME_FPS)) # Prints the FPS to the console.
  145. .. code-tab:: csharp
  146. GD.Print(Performance.GetMonitor(Performance.Monitor.TimeFps)); // Prints the FPS to the console.
  147. ----
  148. .. _class_Performance_method_get_monitor_modification_time:
  149. - :ref:`int<class_int>` **get_monitor_modification_time** **(** **)**
  150. Returns the last tick in which custom monitor was added/removed.
  151. ----
  152. .. _class_Performance_method_has_custom_monitor:
  153. - :ref:`bool<class_bool>` **has_custom_monitor** **(** :ref:`StringName<class_StringName>` id **)**
  154. Returns true if custom monitor with the given id is present otherwise returns false.
  155. ----
  156. .. _class_Performance_method_remove_custom_monitor:
  157. - void **remove_custom_monitor** **(** :ref:`StringName<class_StringName>` id **)**
  158. Removes the custom monitor with given id.
  159. \ **Note:** It throws an error if the given id is already absent.
  160. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  161. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  162. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  163. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  164. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  165. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`