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