< Summary

Class:DCL.UserBenchmarkController
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Debugging/Panels/UserBenchmarkController.cs
Covered lines:0
Uncovered lines:73
Coverable lines:73
Total lines:192
Line coverage:0% (0 of 73)
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%30500%
GetActiveScene()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/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
 57        private void Awake()
 58        {
 059            StartProfiling();
 060        }
 61
 62        private void OnDestroy()
 63        {
 064            StopProfiling();
 065        }
 66
 67        public void Init()
 68        {
 069            this.statsPanel = GetComponent<StatsPanel>();
 70
 071            columnsList = Enum.GetValues(typeof(Columns)).Cast<Columns>().ToList();
 072            rowsList = Enum.GetValues(typeof(Rows)).Cast<Rows>().ToList();
 73
 074            statsPanel.PopulateTable(columnsList.Count, rowsList.Count, 240, 240);
 75
 076            statsPanel.SetCellText((int) Columns.LABEL, (int) Columns.LABEL, "");
 77
 78            //Init Labels
 079            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.PROCESSED_MESSAGES, PROCESSED_MESSAGES_TEXT);
 080            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.PENDING_MESSAGES, PENDING_MESSAGES_TEXT);
 81
 082            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.BREAK_0, BREAK_0_TEXT);
 83
 084            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.CURRENT_SCENE, CURRENT_SCENE_TEST);
 085            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.POLYGONS_VS_LIMIT, POLYGON_VS_LIMIT_TEXT);
 086            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.TEXTURES_VS_LIMIT, TEXTURES_VS_LIMIT_TEXT);
 087            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.MATERIALS_VS_LIMIT, MATERIALS_VS_LIMIT_TEXT);
 088            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.ENTITIES_VS_LIMIT, ENTITIES_VS_LIMIT_TEXT);
 089            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.MESHES_VS_LIMIT, MESHES_VS_LIMIT_TEXT);
 090            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.BODIES_VS_LIMIT, BODIES_VS_LIMIT_TEXT);
 091            statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.COMPONENT_OBJECTS_COUNT,
 92                COMPONENT_OBJECTS_COUNT_TEXT);
 93
 94            //Init Values
 95
 096            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, "=/=");
 097            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, "=/=");
 98
 099            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.CURRENT_SCENE, "=/=");
 0100            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.POLYGONS_VS_LIMIT, "=/=");
 0101            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.TEXTURES_VS_LIMIT, "=/=");
 0102            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MATERIALS_VS_LIMIT, "=/=");
 0103            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.ENTITIES_VS_LIMIT, "=/=");
 0104            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MESHES_VS_LIMIT, "=/=");
 0105            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.BODIES_VS_LIMIT, "=/=");
 0106            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.COMPONENT_OBJECTS_COUNT, "=/=");
 107
 0108            Canvas.ForceUpdateCanvases();
 0109        }
 110
 111        public void StartProfiling()
 112        {
 0113            if (statsPanel == null)
 114            {
 0115                Init();
 116            }
 117
 0118            profilingCoroutine = CoroutineStarter.Start(RefreshProfilingData());
 0119            ProfilingEvents.OnMessageWillQueue += OnMessageWillQueue;
 0120            ProfilingEvents.OnMessageWillDequeue += OnMessageWillDequeue;
 0121        }
 122
 123        private void OnMessageWillDequeue(string obj)
 124        {
 0125            totalMessagesCurrent = Math.Min(totalMessagesCurrent + 1, totalMessagesGlobal);
 0126            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES,
 127                $"{totalMessagesCurrent} of {totalMessagesGlobal}");
 0128            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES,
 129                $"{totalMessagesGlobal - totalMessagesCurrent}");
 0130        }
 131
 132        private void OnMessageWillQueue(string obj)
 133        {
 0134            totalMessagesGlobal++;
 0135            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES,
 136                $"{totalMessagesCurrent}:{totalMessagesGlobal}");
 0137            statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES,
 138                $"{totalMessagesGlobal - totalMessagesCurrent}");
 0139        }
 140
 141        private Coroutine profilingCoroutine;
 142
 143        public void StopProfiling()
 144        {
 0145            if (profilingCoroutine != null)
 0146                CoroutineStarter.Stop(profilingCoroutine);
 147
 0148            ProfilingEvents.OnMessageWillQueue -= OnMessageWillQueue;
 0149            ProfilingEvents.OnMessageWillDequeue -= OnMessageWillDequeue;
 0150        }
 151
 152        private IEnumerator RefreshProfilingData()
 153        {
 0154            while (true)
 155            {
 0156                IParcelScene activeScene = GetActiveScene();
 157
 0158                if (activeScene != null && activeScene.metricsCounter != null)
 159                {
 0160                    var metrics = activeScene.metricsCounter.currentCount;
 0161                    var limits = activeScene.metricsCounter.maxCount;
 0162                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.CURRENT_SCENE, $"{activeScene.sceneData.scene
 0163                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.POLYGONS_VS_LIMIT, $"{metrics.triangles} of {
 0164                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.TEXTURES_VS_LIMIT, $"{metrics.textures} of {l
 0165                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MATERIALS_VS_LIMIT, $"{metrics.materials} of 
 0166                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.ENTITIES_VS_LIMIT, $"{metrics.entities} of {l
 0167                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MESHES_VS_LIMIT, $"{metrics.meshes} of {limit
 0168                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.BODIES_VS_LIMIT, $"{metrics.bodies} of {limit
 0169                    statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.COMPONENT_OBJECTS_COUNT, (activeScene.compone
 170                }
 171
 0172                yield return WaitForSecondsCache.Get(0.2f);
 173            }
 174        }
 175
 176        private IParcelScene GetActiveScene()
 177        {
 0178            IWorldState worldState = Environment.i.world.state;
 0179            int debugSceneNumber = KernelConfig.i.Get().debugConfig.sceneDebugPanelTargetSceneNumber;
 0180            if (debugSceneNumber > 0)
 181            {
 0182                if (worldState.TryGetScene(debugSceneNumber, out IParcelScene scene))
 0183                    return scene;
 184            }
 185
 0186            var currentPos = DataStore.i.player.playerGridPosition.Get();
 0187            worldState.TryGetScene(worldState.GetSceneNumberByCoords(currentPos), out IParcelScene resultScene);
 188
 0189            return resultScene;
 190        }
 191    }
 192}