| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | [RequireComponent(typeof(StatsPanel))] |
| | 12 | | public class UserBenchmarkController : MonoBehaviour, IBenchmarkController |
| | 13 | | { |
| | 14 | | public enum Columns |
| | 15 | | { |
| | 16 | | LABEL, |
| | 17 | | VALUE, |
| | 18 | | } |
| | 19 | |
|
| | 20 | | public enum Rows |
| | 21 | | { |
| | 22 | | NONE, |
| | 23 | | PROCESSED_MESSAGES, |
| | 24 | | PENDING_MESSAGES, |
| | 25 | | BREAK_0, |
| | 26 | | CURRENT_SCENE, |
| | 27 | | POLYGONS_VS_LIMIT, |
| | 28 | | TEXTURES_VS_LIMIT, |
| | 29 | | MATERIALS_VS_LIMIT, |
| | 30 | | ENTITIES_VS_LIMIT, |
| | 31 | | MESHES_VS_LIMIT, |
| | 32 | | BODIES_VS_LIMIT, |
| | 33 | | COMPONENT_OBJECTS_COUNT |
| | 34 | | } |
| | 35 | |
|
| | 36 | | const string BREAK_0_TEXT = ""; |
| | 37 | |
|
| | 38 | | const string PROCESSED_MESSAGES_TEXT = "Processed Messages"; |
| | 39 | | const string PENDING_MESSAGES_TEXT = "Pending on Queue"; |
| | 40 | |
|
| | 41 | | const string CURRENT_SCENE_TEST = "Scene Location:"; |
| | 42 | | const string POLYGON_VS_LIMIT_TEXT = "Poly Count"; |
| | 43 | | const string TEXTURES_VS_LIMIT_TEXT = "Textures Count"; |
| | 44 | | const string MATERIALS_VS_LIMIT_TEXT = "Materials Count"; |
| | 45 | | const string ENTITIES_VS_LIMIT_TEXT = "Entities Count"; |
| | 46 | | const string MESHES_VS_LIMIT_TEXT = "Meshes Count"; |
| | 47 | | const string BODIES_VS_LIMIT_TEXT = "Bodies Count"; |
| | 48 | | const string COMPONENT_OBJECTS_COUNT_TEXT = "Components Count"; |
| | 49 | |
|
| | 50 | | int totalMessagesCurrent; |
| | 51 | | int totalMessagesGlobal; |
| | 52 | |
|
| | 53 | | StatsPanel statsPanel; |
| | 54 | | List<Columns> columnsList; |
| | 55 | | List<Rows> rowsList; |
| | 56 | |
|
| | 57 | | private void Awake() |
| | 58 | | { |
| 0 | 59 | | StartProfiling(); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | private void OnDestroy() |
| | 63 | | { |
| 0 | 64 | | StopProfiling(); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public void Init() |
| | 68 | | { |
| 0 | 69 | | this.statsPanel = GetComponent<StatsPanel>(); |
| | 70 | |
|
| 0 | 71 | | columnsList = Enum.GetValues(typeof(Columns)).Cast<Columns>().ToList(); |
| 0 | 72 | | rowsList = Enum.GetValues(typeof(Rows)).Cast<Rows>().ToList(); |
| | 73 | |
|
| 0 | 74 | | statsPanel.PopulateTable(columnsList.Count, rowsList.Count, 240, 240); |
| | 75 | |
|
| 0 | 76 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Columns.LABEL, ""); |
| | 77 | |
|
| | 78 | | //Init Labels |
| 0 | 79 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.PROCESSED_MESSAGES, PROCESSED_MESSAGES_TEXT); |
| 0 | 80 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.PENDING_MESSAGES, PENDING_MESSAGES_TEXT); |
| | 81 | |
|
| 0 | 82 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.BREAK_0, BREAK_0_TEXT); |
| | 83 | |
|
| 0 | 84 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.CURRENT_SCENE, CURRENT_SCENE_TEST); |
| 0 | 85 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.POLYGONS_VS_LIMIT, POLYGON_VS_LIMIT_TEXT); |
| 0 | 86 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.TEXTURES_VS_LIMIT, TEXTURES_VS_LIMIT_TEXT); |
| 0 | 87 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.MATERIALS_VS_LIMIT, MATERIALS_VS_LIMIT_TEXT); |
| 0 | 88 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.ENTITIES_VS_LIMIT, ENTITIES_VS_LIMIT_TEXT); |
| 0 | 89 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.MESHES_VS_LIMIT, MESHES_VS_LIMIT_TEXT); |
| 0 | 90 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.BODIES_VS_LIMIT, BODIES_VS_LIMIT_TEXT); |
| 0 | 91 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.COMPONENT_OBJECTS_COUNT, |
| | 92 | | COMPONENT_OBJECTS_COUNT_TEXT); |
| | 93 | |
|
| | 94 | | //Init Values |
| | 95 | |
|
| 0 | 96 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, "=/="); |
| 0 | 97 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, "=/="); |
| | 98 | |
|
| 0 | 99 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.CURRENT_SCENE, "=/="); |
| 0 | 100 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.POLYGONS_VS_LIMIT, "=/="); |
| 0 | 101 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.TEXTURES_VS_LIMIT, "=/="); |
| 0 | 102 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MATERIALS_VS_LIMIT, "=/="); |
| 0 | 103 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.ENTITIES_VS_LIMIT, "=/="); |
| 0 | 104 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MESHES_VS_LIMIT, "=/="); |
| 0 | 105 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.BODIES_VS_LIMIT, "=/="); |
| 0 | 106 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.COMPONENT_OBJECTS_COUNT, "=/="); |
| | 107 | |
|
| 0 | 108 | | Canvas.ForceUpdateCanvases(); |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | public void StartProfiling() |
| | 112 | | { |
| 0 | 113 | | if (statsPanel == null) |
| | 114 | | { |
| 0 | 115 | | Init(); |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | profilingCoroutine = CoroutineStarter.Start(RefreshProfilingData()); |
| 0 | 119 | | ProfilingEvents.OnMessageWillQueue += OnMessageWillQueue; |
| 0 | 120 | | ProfilingEvents.OnMessageWillDequeue += OnMessageWillDequeue; |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void OnMessageWillDequeue(string obj) |
| | 124 | | { |
| 0 | 125 | | totalMessagesCurrent = Math.Min(totalMessagesCurrent + 1, totalMessagesGlobal); |
| 0 | 126 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, |
| | 127 | | $"{totalMessagesCurrent} of {totalMessagesGlobal}"); |
| 0 | 128 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, |
| | 129 | | $"{totalMessagesGlobal - totalMessagesCurrent}"); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | private void OnMessageWillQueue(string obj) |
| | 133 | | { |
| 0 | 134 | | totalMessagesGlobal++; |
| 0 | 135 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, |
| | 136 | | $"{totalMessagesCurrent}:{totalMessagesGlobal}"); |
| 0 | 137 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, |
| | 138 | | $"{totalMessagesGlobal - totalMessagesCurrent}"); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private Coroutine profilingCoroutine; |
| | 142 | |
|
| | 143 | | public void StopProfiling() |
| | 144 | | { |
| 0 | 145 | | if (profilingCoroutine != null) |
| 0 | 146 | | CoroutineStarter.Stop(profilingCoroutine); |
| | 147 | |
|
| 0 | 148 | | ProfilingEvents.OnMessageWillQueue -= OnMessageWillQueue; |
| 0 | 149 | | ProfilingEvents.OnMessageWillDequeue -= OnMessageWillDequeue; |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | private IEnumerator RefreshProfilingData() |
| | 153 | | { |
| 0 | 154 | | while (true) |
| | 155 | | { |
| 0 | 156 | | IParcelScene activeScene = GetActiveScene(); |
| | 157 | |
|
| 0 | 158 | | if (activeScene != null && activeScene.metricsCounter != null) |
| | 159 | | { |
| 0 | 160 | | var metrics = activeScene.metricsCounter.currentCount; |
| 0 | 161 | | var limits = activeScene.metricsCounter.maxCount; |
| 0 | 162 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.CURRENT_SCENE, $"{activeScene.sceneData.id}") |
| 0 | 163 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.POLYGONS_VS_LIMIT, $"{metrics.triangles} of { |
| 0 | 164 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.TEXTURES_VS_LIMIT, $"{metrics.textures} of {l |
| 0 | 165 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MATERIALS_VS_LIMIT, $"{metrics.materials} of |
| 0 | 166 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.ENTITIES_VS_LIMIT, $"{metrics.entities} of {l |
| 0 | 167 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MESHES_VS_LIMIT, $"{metrics.meshes} of {limit |
| 0 | 168 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.BODIES_VS_LIMIT, $"{metrics.bodies} of {limit |
| 0 | 169 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.COMPONENT_OBJECTS_COUNT, (activeScene.compone |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | yield return WaitForSecondsCache.Get(0.2f); |
| | 173 | | } |
| | 174 | | } |
| | 175 | |
|
| | 176 | | private IParcelScene GetActiveScene() |
| | 177 | | { |
| 0 | 178 | | IWorldState worldState = Environment.i.world.state; |
| 0 | 179 | | string debugSceneId = KernelConfig.i.Get().debugConfig.sceneDebugPanelTargetSceneId; |
| | 180 | |
|
| 0 | 181 | | if (!string.IsNullOrEmpty(debugSceneId)) |
| | 182 | | { |
| 0 | 183 | | if (worldState.TryGetScene(debugSceneId, out IParcelScene scene)) |
| 0 | 184 | | return scene; |
| | 185 | | } |
| | 186 | |
|
| 0 | 187 | | var currentPos = DataStore.i.player.playerGridPosition.Get(); |
| 0 | 188 | | worldState.TryGetScene(worldState.GetSceneIdByCoords(currentPos), out IParcelScene resultScene); |
| | 189 | |
|
| 0 | 190 | | return resultScene; |
| | 191 | | } |
| | 192 | | } |
| | 193 | | } |