< Summary

Class:DCL.MiscBenchmarkController
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Debugging/Panels/MiscBenchmarkController.cs
Covered lines:0
Uncovered lines:89
Coverable lines:89
Total lines:234
Line coverage:0% (0 of 89)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Init()0%2100%
StartProfiling()0%12300%
StopProfiling()0%6200%
Update()0%6200%
RefreshProfilingData()0%2101400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Debugging/Panels/MiscBenchmarkController.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Globalization;
 5using System.Linq;
 6using DCL.Controllers;
 7using UnityEngine;
 8using UnityGLTF;
 9
 10namespace 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        {
 062            this.statsPanel = GetComponent<StatsPanel>();
 63
 064            columnsList = Enum.GetValues(typeof(Columns)).Cast<Columns>().ToList();
 065            rowsList = Enum.GetValues(typeof(Rows)).Cast<Rows>().ToList();
 66
 067            statsPanel.PopulateTable(columnsList.Count, rowsList.Count, 240, 240);
 68
 69            //NOTE(Brian): Top-left cell, unused.
 070            statsPanel.SetCellText(0, 0, "");
 71
 72            //NOTE(Brian): Row stuff (left vertical header)
 073            statsPanel.SetCellText(0, (int) Rows.SHARED_OBJECTS_COUNT, SHARED_OBJECTS_COUNT_TEXT);
 074            statsPanel.SetCellText(0, (int) Rows.COMPONENT_OBJECTS_COUNT, COMPONENT_OBJECTS_COUNT_TEXT);
 075            statsPanel.SetCellText(0, (int) Rows.ENTITY_OBJECTS_COUNT, ENTITY_OBJECTS_COUNT_TEXT);
 076            statsPanel.SetCellText(0, (int) Rows.BREAK_0, BREAK_0_TEXT);
 077            statsPanel.SetCellText(0, (int) Rows.MATERIAL_COUNT, MATERIAL_COUNT_TEXT);
 078            statsPanel.SetCellText(0, (int) Rows.MESHES_COUNT, MESHES_COUNT_TEXT);
 079            statsPanel.SetCellText(0, (int) Rows.BREAK_1, BREAK_1_TEXT);
 080            statsPanel.SetCellText(0, (int) Rows.GLTF_BEING_LOADED, GLTF_BEING_LOADED_TEXT);
 081            statsPanel.SetCellText(0, (int) Rows.AB_BEING_LOADED, AB_BEING_LOADED_TEXT);
 082            statsPanel.SetCellText(0, (int) Rows.MESSAGES_PER_SECOND_REAL, MESSAGES_PER_SECOND_REAL_TEXT);
 083            statsPanel.SetCellText(0, (int) Rows.RENDERER_UNLOCK_SEGS, RENDERER_UNLOCK_TEXT);
 084            statsPanel.SetCellText(0, (int) Rows.MESSAGE_BUSES, MESSAGES_BUSES_TEXT);
 085        }
 86
 87        private Coroutine updateCoroutine;
 88
 89        public void StartProfiling()
 90        {
 091            if (enabled)
 92            {
 093                return;
 94            }
 95
 096            if (statsPanel == null)
 97            {
 098                Init();
 99            }
 100
 0101            updateCoroutine = CoroutineStarter.Start(RefreshProfilingData());
 0102            enabled = true;
 0103        }
 104
 105        public void StopProfiling()
 106        {
 0107            if (!enabled)
 108            {
 0109                return;
 110            }
 111
 0112            CoroutineStarter.Stop(updateCoroutine);
 0113            enabled = false;
 0114        }
 115
 116        static float budgetMax = 0;
 117
 118        void Update()
 119        {
 0120            MessagingControllersManager messagingManager =
 121                Environment.i.messaging.manager as MessagingControllersManager;
 122
 0123            int messagesProcessedLastFrame = lastPendingMessages - messagingManager.pendingMessagesCount;
 124
 0125            if (messagesProcessedLastFrame > 0)
 126            {
 0127                sampleCount++;
 0128                mps += 1 / (Time.deltaTime / messagesProcessedLastFrame);
 0129                statsPanel.SetCellText(1, (int) Rows.MESSAGES_PER_SECOND_REAL,
 130                    (mps / sampleCount).ToString(CultureInfo.InvariantCulture));
 131            }
 132
 0133            lastPendingMessages = messagingManager.pendingMessagesCount;
 0134        }
 135
 136        private IEnumerator RefreshProfilingData()
 137        {
 0138            while (true)
 139            {
 0140                int sharedCount = 0;
 0141                int sharedAttachCount = 0;
 0142                int componentCount = 0;
 0143                int entityCount = 0;
 0144                int materialCount = 0;
 0145                int meshesCount = 0;
 146
 0147                var loadedScenes = Environment.i.world.state.GetLoadedScenes();
 148
 0149                foreach (var v in loadedScenes)
 150                {
 0151                    ParcelScene scene = v.Value as ParcelScene;
 0152                    if (scene.metricsCounter != null)
 153                    {
 0154                        meshesCount += scene.metricsCounter.currentCount.meshes;
 0155                        materialCount += scene.metricsCounter.currentCount.materials;
 156                    }
 157
 0158                    sharedCount += scene.componentsManagerLegacy.GetSceneSharedComponentsDictionary().Count;
 159
 0160                    foreach (var e in scene.componentsManagerLegacy.GetSceneSharedComponentsDictionary())
 161                    {
 0162                        sharedAttachCount += e.Value.GetAttachedEntities().Count;
 163                    }
 164
 0165                    entityCount += scene.entities.Count;
 166
 0167                    foreach (var e in scene.entities)
 168                    {
 0169                        componentCount += scene.componentsManagerLegacy.GetComponentsDictionary(e.Value).Count;
 170                    }
 171                }
 172
 0173                statsPanel.SetCellText(1, (int) Rows.SHARED_OBJECTS_COUNT, sharedCount.ToString());
 0174                statsPanel.SetCellText(1, (int) Rows.COMPONENT_OBJECTS_COUNT, componentCount.ToString());
 0175                statsPanel.SetCellText(1, (int) Rows.ENTITY_OBJECTS_COUNT, entityCount.ToString());
 0176                statsPanel.SetCellText(1, (int) Rows.MATERIAL_COUNT, materialCount.ToString());
 0177                statsPanel.SetCellText(1, (int) Rows.MESHES_COUNT, meshesCount.ToString());
 0178                statsPanel.SetCellText(1, (int) Rows.GLTF_BEING_LOADED,
 179                    GLTFComponent.downloadingCount + " ... In Queue: " +
 180                    GLTFComponent.queueCount);
 0181                statsPanel.SetCellText(1, (int) Rows.AB_BEING_LOADED,
 182                    DataStore.i.performance.concurrentABRequests.Get() + " ...  In Queue: " +
 183                    AssetPromise_AB.queueCount);
 0184                statsPanel.SetCellText(1, (int) Rows.RENDERER_UNLOCK_SEGS,
 185                    RenderingController.firstActivationTime.ToString(CultureInfo.InvariantCulture));
 186
 0187                string busesLog = "";
 0188                Dictionary<string, int> pendingMessagesCount = new Dictionary<string, int>();
 0189                Dictionary<string, int> messagesReplaced = new Dictionary<string, int>();
 190
 0191                MessagingControllersManager messagingManager =
 192                    Environment.i.messaging.manager as MessagingControllersManager;
 193
 0194                using (var controllersIter = messagingManager.messagingControllers.GetEnumerator())
 195                {
 0196                    while (controllersIter.MoveNext())
 197                    {
 0198                        using (var iterator = controllersIter.Current.Value.messagingBuses.GetEnumerator())
 199                        {
 0200                            while (iterator.MoveNext())
 201                            {
 202                                //access to pair using iterator.Current
 0203                                MessagingBusType key = iterator.Current.Key;
 0204                                MessagingBus bus = controllersIter.Current.Value.messagingBuses[key];
 205
 0206                                string keyString = key.ToString();
 207
 0208                                if (!pendingMessagesCount.ContainsKey(keyString))
 0209                                    pendingMessagesCount[keyString] = 0;
 210
 0211                                if (!messagesReplaced.ContainsKey(keyString))
 0212                                    messagesReplaced[keyString] = 0;
 213
 0214                                pendingMessagesCount[keyString] += bus.pendingMessagesCount;
 0215                                messagesReplaced[keyString] += bus.unreliableMessagesReplaced;
 216                            }
 0217                        }
 218                    }
 0219                }
 220
 0221                busesLog +=
 222                    $"{MessagingBusType.UI.ToString()} bus: {pendingMessagesCount[MessagingBusType.UI.ToString()]} repla
 0223                busesLog +=
 224                    $"{MessagingBusType.INIT.ToString()} bus: {pendingMessagesCount[MessagingBusType.INIT.ToString()]} r
 0225                busesLog +=
 226                    $"{MessagingBusType.SYSTEM.ToString()} bus: {pendingMessagesCount[MessagingBusType.SYSTEM.ToString()
 227
 0228                statsPanel.SetCellText(1, (int) Rows.MESSAGE_BUSES, busesLog);
 229
 0230                yield return WaitForSecondsCache.Get(0.2f);
 231            }
 232        }
 233    }
 234}