| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Globalization; |
| | 5 | | using System.Linq; |
| | 6 | | using DCL.Controllers; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityGLTF; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | [RequireComponent(typeof(StatsPanel))] |
| | 13 | | public class MiscBenchmarkController : MonoBehaviour, IBenchmarkController |
| | 14 | | { |
| | 15 | | public enum Columns |
| | 16 | | { |
| | 17 | | NONE, |
| | 18 | | VALUE, |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public enum Rows |
| | 22 | | { |
| | 23 | | NONE, |
| | 24 | | SHARED_OBJECTS_COUNT, |
| | 25 | | COMPONENT_OBJECTS_COUNT, |
| | 26 | | ENTITY_OBJECTS_COUNT, |
| | 27 | | BREAK_0, |
| | 28 | | MATERIAL_COUNT, |
| | 29 | | MESHES_COUNT, |
| | 30 | | BREAK_1, |
| | 31 | | GLTF_BEING_LOADED, |
| | 32 | | AB_BEING_LOADED, |
| | 33 | | MESSAGES_PER_SECOND_REAL, |
| | 34 | | RENDERER_UNLOCK_SEGS, |
| | 35 | | MESSAGE_BUSES, |
| | 36 | | } |
| | 37 | |
|
| | 38 | | const string BREAK_0_TEXT = ""; |
| | 39 | | const string BREAK_1_TEXT = ""; |
| | 40 | |
|
| | 41 | | const string RENDERER_UNLOCK_TEXT = "Time Until Renderer Enable"; |
| | 42 | | const string SHARED_OBJECTS_COUNT_TEXT = "Shared Objects Count"; |
| | 43 | | const string COMPONENT_OBJECTS_COUNT_TEXT = "Components Count"; |
| | 44 | | const string ENTITY_OBJECTS_COUNT_TEXT = "Entity Count"; |
| | 45 | | const string MATERIAL_COUNT_TEXT = "Material Count"; |
| | 46 | | const string MESHES_COUNT_TEXT = "Meshes Count"; |
| | 47 | | const string GLTF_BEING_LOADED_TEXT = "GLTFs active requests"; |
| | 48 | | const string AB_BEING_LOADED_TEXT = "Asset bundles active requests"; |
| | 49 | | const string MESSAGES_PER_SECOND_REAL_TEXT = "Messages x sec"; |
| | 50 | | const string MESSAGES_BUSES_TEXT = "Message buses"; |
| | 51 | |
|
| | 52 | | StatsPanel statsPanel; |
| | 53 | | List<Columns> columnsList; |
| | 54 | | List<Rows> rowsList; |
| | 55 | |
|
| | 56 | | int lastPendingMessages; |
| | 57 | | int sampleCount = 0; |
| | 58 | | float mps = 0; |
| | 59 | |
|
| | 60 | | public void Init() |
| | 61 | | { |
| 0 | 62 | | this.statsPanel = GetComponent<StatsPanel>(); |
| | 63 | |
|
| 0 | 64 | | columnsList = Enum.GetValues(typeof(Columns)).Cast<Columns>().ToList(); |
| 0 | 65 | | rowsList = Enum.GetValues(typeof(Rows)).Cast<Rows>().ToList(); |
| | 66 | |
|
| 0 | 67 | | statsPanel.PopulateTable(columnsList.Count, rowsList.Count, 240, 240); |
| | 68 | |
|
| | 69 | | //NOTE(Brian): Top-left cell, unused. |
| 0 | 70 | | statsPanel.SetCellText(0, 0, ""); |
| | 71 | |
|
| | 72 | | //NOTE(Brian): Row stuff (left vertical header) |
| 0 | 73 | | statsPanel.SetCellText(0, (int) Rows.SHARED_OBJECTS_COUNT, SHARED_OBJECTS_COUNT_TEXT); |
| 0 | 74 | | statsPanel.SetCellText(0, (int) Rows.COMPONENT_OBJECTS_COUNT, COMPONENT_OBJECTS_COUNT_TEXT); |
| 0 | 75 | | statsPanel.SetCellText(0, (int) Rows.ENTITY_OBJECTS_COUNT, ENTITY_OBJECTS_COUNT_TEXT); |
| 0 | 76 | | statsPanel.SetCellText(0, (int) Rows.BREAK_0, BREAK_0_TEXT); |
| 0 | 77 | | statsPanel.SetCellText(0, (int) Rows.MATERIAL_COUNT, MATERIAL_COUNT_TEXT); |
| 0 | 78 | | statsPanel.SetCellText(0, (int) Rows.MESHES_COUNT, MESHES_COUNT_TEXT); |
| 0 | 79 | | statsPanel.SetCellText(0, (int) Rows.BREAK_1, BREAK_1_TEXT); |
| 0 | 80 | | statsPanel.SetCellText(0, (int) Rows.GLTF_BEING_LOADED, GLTF_BEING_LOADED_TEXT); |
| 0 | 81 | | statsPanel.SetCellText(0, (int) Rows.AB_BEING_LOADED, AB_BEING_LOADED_TEXT); |
| 0 | 82 | | statsPanel.SetCellText(0, (int) Rows.MESSAGES_PER_SECOND_REAL, MESSAGES_PER_SECOND_REAL_TEXT); |
| 0 | 83 | | statsPanel.SetCellText(0, (int) Rows.RENDERER_UNLOCK_SEGS, RENDERER_UNLOCK_TEXT); |
| 0 | 84 | | statsPanel.SetCellText(0, (int) Rows.MESSAGE_BUSES, MESSAGES_BUSES_TEXT); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | private Coroutine updateCoroutine; |
| | 88 | |
|
| | 89 | | public void StartProfiling() |
| | 90 | | { |
| 0 | 91 | | if (enabled) |
| | 92 | | { |
| 0 | 93 | | return; |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | if (statsPanel == null) |
| | 97 | | { |
| 0 | 98 | | Init(); |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | updateCoroutine = CoroutineStarter.Start(RefreshProfilingData()); |
| 0 | 102 | | enabled = true; |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | public void StopProfiling() |
| | 106 | | { |
| 0 | 107 | | if (!enabled) |
| | 108 | | { |
| 0 | 109 | | return; |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | CoroutineStarter.Stop(updateCoroutine); |
| 0 | 113 | | enabled = false; |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | static float budgetMax = 0; |
| | 117 | |
|
| | 118 | | void Update() |
| | 119 | | { |
| 0 | 120 | | MessagingControllersManager messagingManager = Environment.i.messaging.manager as MessagingControllersManage |
| | 121 | |
|
| 0 | 122 | | int messagesProcessedLastFrame = lastPendingMessages - messagingManager.pendingMessagesCount; |
| | 123 | |
|
| 0 | 124 | | if (messagesProcessedLastFrame > 0) |
| | 125 | | { |
| 0 | 126 | | sampleCount++; |
| 0 | 127 | | mps += 1 / (Time.deltaTime / messagesProcessedLastFrame); |
| 0 | 128 | | statsPanel.SetCellText(1, (int) Rows.MESSAGES_PER_SECOND_REAL, (mps / sampleCount).ToString(CultureInfo. |
| | 129 | | } |
| | 130 | |
|
| 0 | 131 | | lastPendingMessages = messagingManager.pendingMessagesCount; |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | private IEnumerator RefreshProfilingData() |
| | 135 | | { |
| 0 | 136 | | while (true) |
| | 137 | | { |
| 0 | 138 | | int sharedCount = 0; |
| 0 | 139 | | int sharedAttachCount = 0; |
| 0 | 140 | | int componentCount = 0; |
| 0 | 141 | | int entityCount = 0; |
| 0 | 142 | | int materialCount = 0; |
| 0 | 143 | | int meshesCount = 0; |
| | 144 | |
|
| 0 | 145 | | var loadedScenes = Environment.i.world.state.loadedScenes; |
| | 146 | |
|
| 0 | 147 | | foreach (var v in loadedScenes) |
| | 148 | | { |
| 0 | 149 | | ParcelScene scene = v.Value as ParcelScene; |
| 0 | 150 | | if (scene.metricsCounter != null) |
| | 151 | | { |
| 0 | 152 | | meshesCount += scene.metricsCounter.currentCount.meshes; |
| 0 | 153 | | materialCount += scene.metricsCounter.currentCount.materials; |
| | 154 | | } |
| | 155 | |
|
| 0 | 156 | | sharedCount += scene.disposableComponents.Count; |
| | 157 | |
|
| 0 | 158 | | foreach (var e in scene.disposableComponents) |
| | 159 | | { |
| 0 | 160 | | sharedAttachCount += e.Value.GetAttachedEntities().Count; |
| | 161 | | } |
| | 162 | |
|
| 0 | 163 | | entityCount += scene.entities.Count; |
| | 164 | |
|
| 0 | 165 | | foreach (var e in scene.entities) |
| | 166 | | { |
| 0 | 167 | | componentCount += e.Value.components.Count; |
| | 168 | | } |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | statsPanel.SetCellText(1, (int) Rows.SHARED_OBJECTS_COUNT, sharedCount.ToString()); |
| 0 | 172 | | statsPanel.SetCellText(1, (int) Rows.COMPONENT_OBJECTS_COUNT, componentCount.ToString()); |
| 0 | 173 | | statsPanel.SetCellText(1, (int) Rows.ENTITY_OBJECTS_COUNT, entityCount.ToString()); |
| 0 | 174 | | statsPanel.SetCellText(1, (int) Rows.MATERIAL_COUNT, materialCount.ToString()); |
| 0 | 175 | | statsPanel.SetCellText(1, (int) Rows.MESHES_COUNT, meshesCount.ToString()); |
| 0 | 176 | | statsPanel.SetCellText(1, (int) Rows.GLTF_BEING_LOADED, GLTFComponent.downloadingCount.ToString() + " .. |
| 0 | 177 | | statsPanel.SetCellText(1, (int) Rows.AB_BEING_LOADED, AssetPromise_AB.downloadingCount.ToString() + " .. |
| 0 | 178 | | statsPanel.SetCellText(1, (int) Rows.RENDERER_UNLOCK_SEGS, RenderingController.firstActivationTime.ToStr |
| | 179 | |
|
| 0 | 180 | | string busesLog = ""; |
| 0 | 181 | | Dictionary<string, int> pendingMessagesCount = new Dictionary<string, int>(); |
| 0 | 182 | | Dictionary<string, int> messagesReplaced = new Dictionary<string, int>(); |
| | 183 | |
|
| 0 | 184 | | MessagingControllersManager messagingManager = Environment.i.messaging.manager as MessagingControllersMa |
| | 185 | |
|
| 0 | 186 | | using (var controllersIter = messagingManager.messagingControllers.GetEnumerator()) |
| | 187 | | { |
| 0 | 188 | | while (controllersIter.MoveNext()) |
| | 189 | | { |
| 0 | 190 | | using (var iterator = controllersIter.Current.Value.messagingBuses.GetEnumerator()) |
| | 191 | | { |
| 0 | 192 | | while (iterator.MoveNext()) |
| | 193 | | { |
| | 194 | | //access to pair using iterator.Current |
| 0 | 195 | | MessagingBusType key = iterator.Current.Key; |
| 0 | 196 | | MessagingBus bus = controllersIter.Current.Value.messagingBuses[key]; |
| | 197 | |
|
| 0 | 198 | | string keyString = key.ToString(); |
| | 199 | |
|
| 0 | 200 | | if (!pendingMessagesCount.ContainsKey(keyString)) |
| 0 | 201 | | pendingMessagesCount[keyString] = 0; |
| | 202 | |
|
| 0 | 203 | | if (!messagesReplaced.ContainsKey(keyString)) |
| 0 | 204 | | messagesReplaced[keyString] = 0; |
| | 205 | |
|
| 0 | 206 | | pendingMessagesCount[keyString] += bus.pendingMessagesCount; |
| 0 | 207 | | messagesReplaced[keyString] += bus.unreliableMessagesReplaced; |
| | 208 | | } |
| 0 | 209 | | } |
| | 210 | | } |
| 0 | 211 | | } |
| | 212 | |
|
| 0 | 213 | | busesLog += $"{MessagingBusType.UI.ToString()} bus: {pendingMessagesCount[MessagingBusType.UI.ToString() |
| 0 | 214 | | busesLog += $"{MessagingBusType.INIT.ToString()} bus: {pendingMessagesCount[MessagingBusType.INIT.ToStri |
| 0 | 215 | | busesLog += $"{MessagingBusType.SYSTEM.ToString()} bus: {pendingMessagesCount[MessagingBusType.SYSTEM.To |
| | 216 | |
|
| 0 | 217 | | statsPanel.SetCellText(1, (int) Rows.MESSAGE_BUSES, busesLog); |
| | 218 | |
|
| 0 | 219 | | yield return WaitForSecondsCache.Get(0.2f); |
| | 220 | | } |
| | 221 | | } |
| | 222 | | } |
| | 223 | | } |