< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%2100%
OnDestroy()0%2100%
Init()0%2100%
StartProfiling()0%6200%
OnMessageWillDequeue(...)0%2100%
OnMessageWillQueue(...)0%2100%
StopProfiling()0%6200%
RefreshProfilingData()0%56700%
GetActiveScene()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/Debugging/Panels/UserBenchmarkController.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Linq;
 5using DCL.Controllers;
 6using DCL.Helpers;
 7using UnityEngine;
 8
 9namespace 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
 057        private void Awake() { StartProfiling(); }
 58
 059        private void OnDestroy() { StopProfiling(); }
 60
 61        public void Init()
 62        {
 063            this.statsPanel = GetComponent<StatsPanel>();
 64
 065            columnsList = Enum.GetValues(typeof(Columns)).Cast<Columns>().ToList();
 066            rowsList = Enum.GetValues(typeof(Rows)).Cast<Rows>().ToList();
 67
 068            statsPanel.PopulateTable(columnsList.Count, rowsList.Count, 240, 240);
 69
 070            statsPanel.SetCellText((int) Columns.LABEL, (int) Columns.LABEL, "");
 71
 72            //Init Labels
 073            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.PROCESSED_MESSAGES, PROCESSED_MESSAGES_TEXT);
 074            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.PENDING_MESSAGES, PENDING_MESSAGES_TEXT);
 75
 076            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.BREAK_0, BREAK_0_TEXT);
 77
 078            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.CURRENT_SCENE, CURRENT_SCENE_TEST);
 079            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.POLYGONS_VS_LIMIT, POLYGON_VS_LIMIT_TEXT);
 080            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.TEXTURES_VS_LIMIT, TEXTURES_VS_LIMIT_TEXT);
 081            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.MATERIALS_VS_LIMIT, MATERIALS_VS_LIMIT_TEXT);
 082            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.ENTITIES_VS_LIMIT, ENTITIES_VS_LIMIT_TEXT);
 083            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.MESHES_VS_LIMIT, MESHES_VS_LIMIT_TEXT);
 084            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.BODIES_VS_LIMIT, BODIES_VS_LIMIT_TEXT);
 085            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.COMPONENT_OBJECTS_COUNT, COMPONENT_OBJECTS_COUNT_TEXT
 86
 87            //Init Values
 88
 089            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, "=/=");
 090            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, "=/=");
 91
 092            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.CURRENT_SCENE, "=/=");
 093            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.POLYGONS_VS_LIMIT, "=/=");
 094            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.TEXTURES_VS_LIMIT, "=/=");
 095            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MATERIALS_VS_LIMIT, "=/=");
 096            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.ENTITIES_VS_LIMIT, "=/=");
 097            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MESHES_VS_LIMIT, "=/=");
 098            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.BODIES_VS_LIMIT, "=/=");
 099            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.COMPONENT_OBJECTS_COUNT, "=/=");
 100
 0101            Canvas.ForceUpdateCanvases();
 0102        }
 103
 104        public void StartProfiling()
 105        {
 0106            if (statsPanel == null)
 107            {
 0108                Init();
 109            }
 110
 0111            profilingCoroutine = CoroutineStarter.Start(RefreshProfilingData());
 0112            ProfilingEvents.OnMessageWillQueue += OnMessageWillQueue;
 0113            ProfilingEvents.OnMessageWillDequeue += OnMessageWillDequeue;
 0114        }
 115
 116        private void OnMessageWillDequeue(string obj)
 117        {
 0118            totalMessagesCurrent = Math.Min(totalMessagesCurrent + 1, totalMessagesGlobal);
 0119            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, $"{totalMessagesCurrent} of {tota
 0120            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, $"{totalMessagesGlobal - totalMessa
 0121        }
 122
 123        private void OnMessageWillQueue(string obj)
 124        {
 0125            totalMessagesGlobal++;
 0126            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, $"{totalMessagesCurrent}:{totalMe
 0127            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, $"{totalMessagesGlobal - totalMessa
 0128        }
 129
 130        private Coroutine profilingCoroutine;
 131
 132        public void StopProfiling()
 133        {
 0134            if (profilingCoroutine != null)
 0135                CoroutineStarter.Stop(profilingCoroutine);
 136
 0137            ProfilingEvents.OnMessageWillQueue -= OnMessageWillQueue;
 0138            ProfilingEvents.OnMessageWillDequeue -= OnMessageWillDequeue;
 0139        }
 140
 141        private IEnumerator RefreshProfilingData()
 142        {
 0143            while (true)
 144            {
 0145                IParcelScene activeScene = GetActiveScene();
 146
 0147                if (activeScene != null && activeScene.metricsCounter != null)
 148                {
 0149                    var metrics = activeScene.metricsCounter.currentCount;
 0150                    var limits = activeScene.metricsCounter.maxCount;
 0151                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.CURRENT_SCENE, $"{activeScene.sceneData.id}")
 0152                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.POLYGONS_VS_LIMIT, $"{metrics.triangles} of {
 0153                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.TEXTURES_VS_LIMIT, $"{metrics.textures} of {l
 0154                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MATERIALS_VS_LIMIT, $"{metrics.materials} of 
 0155                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.ENTITIES_VS_LIMIT, $"{metrics.entities} of {l
 0156                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MESHES_VS_LIMIT, $"{metrics.meshes} of {limit
 0157                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.BODIES_VS_LIMIT, $"{metrics.bodies} of {limit
 0158                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.COMPONENT_OBJECTS_COUNT, activeScene.disposab
 159                }
 160
 0161                yield return WaitForSecondsCache.Get(0.2f);
 162            }
 163        }
 164
 165        private IParcelScene GetActiveScene()
 166        {
 0167            IWorldState worldState = Environment.i.world.state;
 0168            string debugSceneId = KernelConfig.i.Get().debugConfig.sceneDebugPanelTargetSceneId;
 169
 0170            if (!string.IsNullOrEmpty(debugSceneId))
 171            {
 0172                if (worldState.loadedScenes.TryGetValue(debugSceneId, out IParcelScene scene))
 0173                    return scene;
 174            }
 175
 0176            var currentPos = Utils.WorldToGridPosition(DCLCharacterController.i.characterPosition.worldPosition);
 0177            return worldState.loadedScenes.Values.FirstOrDefault(
 0178                x => x.sceneData.parcels != null
 0179                     && x.sceneData.parcels.Any(y => y == currentPos));
 180        }
 181    }
 182}