< Summary

Class:DCL.PerformanceMetricsController
Assembly:PerformanceController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Debugging/Performance/PerformanceMetricsController.cs
Covered lines:0
Uncovered lines:66
Coverable lines:66
Total lines:173
Line coverage:0% (0 of 66)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PerformanceMetricsController()0%2100%
OnFeatureFlagChange(...)0%12300%
Update()0%30500%
Report(...)0%30500%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Debugging/Performance/PerformanceMetricsController.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using DCL.Controllers;
 4using DCL.Interface;
 5using DCL.FPSDisplay;
 6using DCL.SettingsCommon;
 7using MainScripts.DCL.Analytics.PerformanceAnalytics;
 8using Newtonsoft.Json;
 9using Unity.Profiling;
 10using UnityEngine;
 11
 12namespace DCL
 13{
 14    public class PerformanceMetricsController
 15    {
 16        private const int SAMPLES_SIZE = 1000; // Send performance report every 1000 samples
 17        private const string PROFILER_METRICS_FEATURE_FLAG = "profiler_metrics";
 18
 019        private readonly LinealBufferHiccupCounter tracker = new LinealBufferHiccupCounter();
 020        private readonly char[] encodedSamples = new char[SAMPLES_SIZE];
 21        private readonly PerformanceMetricsDataVariable performanceMetricsDataVariable;
 022        private IWorldState worldState => Environment.i.world.state;
 023        private BaseVariable<FeatureFlag> featureFlags => DataStore.i.featureFlags.flags;
 024        private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers;
 025        private GeneralSettings generalSettings => Settings.i.generalSettings.Data;
 26        private ProfilerRecorder drawCallsRecorder;
 27        private ProfilerRecorder reservedMemoryRecorder;
 28        private ProfilerRecorder usedMemoryRecorder;
 29        private ProfilerRecorder gcAllocatedInFrameRecorder;
 30        private bool trackProfileRecords;
 31        private int currentIndex = 0;
 32        private long totalAllocSample;
 33        private bool isTrackingProfileRecords = false;
 034        private readonly Dictionary<int, long> scenesMemoryScore = new Dictionary<int, long>();
 35
 036        public PerformanceMetricsController()
 37        {
 038            performanceMetricsDataVariable = Resources.Load<PerformanceMetricsDataVariable>("ScriptableObjects/Performan
 39
 040            featureFlags.OnChange += OnFeatureFlagChange;
 041            OnFeatureFlagChange(featureFlags.Get(), null);
 042        }
 43        private void OnFeatureFlagChange(FeatureFlag current, FeatureFlag previous)
 44        {
 045            trackProfileRecords = current.IsFeatureEnabled(PROFILER_METRICS_FEATURE_FLAG);
 46
 047            if (trackProfileRecords && !isTrackingProfileRecords)
 48            {
 049                drawCallsRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Render, "Draw Calls Count");
 050                reservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Reserved Memory");
 051                usedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Used Memory");
 052                gcAllocatedInFrameRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Allocated In Frame")
 053                isTrackingProfileRecords = true;
 54            }
 055        }
 56
 57        public void Update()
 58        {
 59#if !UNITY_EDITOR && UNITY_WEBGL
 60            if (!CommonScriptableObjects.focusState.Get())
 61                return;
 62#endif
 063            if (!CommonScriptableObjects.rendererState.Get() && !CommonScriptableObjects.forcePerformanceMeter.Get())
 064                return;
 65
 066            var deltaInMs = Time.deltaTime * 1000;
 67
 068            tracker.AddDeltaTime(Time.deltaTime);
 69
 070            performanceMetricsDataVariable.Set(tracker.CurrentFPSCount(),
 71                tracker.CurrentHiccupCount(),
 72                tracker.HiccupsSum,
 73                tracker.GetTotalSeconds());
 74
 075            encodedSamples[currentIndex++] = (char) deltaInMs;
 76
 077            if (trackProfileRecords)
 78            {
 079                totalAllocSample += gcAllocatedInFrameRecorder.LastValue;
 80            }
 81
 082            if (currentIndex == SAMPLES_SIZE)
 83            {
 084                currentIndex = 0;
 085                Report(new string(encodedSamples));
 086                totalAllocSample = 0;
 87            }
 88
 089        }
 90
 91        private void Report(string encodedSamples)
 92        {
 93
 094            var loadedScenesValues = worldState.GetLoadedScenes();
 095            scenesMemoryScore.Clear();
 096            foreach (var parcelScene in loadedScenesValues)
 97            {
 98                // we ignore global scene
 099                IParcelScene parcelSceneValue = parcelScene.Value;
 100
 0101                if (parcelSceneValue.isPersistent)
 102                    continue;
 103
 0104                scenesMemoryScore.Add(parcelSceneValue.sceneData.sceneNumber, parcelSceneValue.metricsCounter.currentCou
 105            }
 106
 0107            object drawCalls = null;
 0108            object totalMemoryReserved = null;
 0109            object totalMemoryUsage = null;
 0110            object totalGCAlloc = null;
 111
 0112            if (trackProfileRecords)
 113            {
 0114                drawCalls = (int)drawCallsRecorder.LastValue;
 0115                totalMemoryReserved = reservedMemoryRecorder.LastValue;
 0116                totalMemoryUsage = usedMemoryRecorder.LastValue;
 0117                totalGCAlloc = totalAllocSample;
 118            }
 119
 0120            var playerCount = otherPlayers.Count();
 0121            var loadRadius = generalSettings.scenesLoadRadius;
 122
 0123            (int gltfloading, int gltffailed, int gltfcancelled, int gltfloaded) = PerformanceAnalytics.GLTFTracker.GetD
 0124            (int abloading, int abfailed, int abcancelled, int abloaded) = PerformanceAnalytics.ABTracker.GetData();
 0125            var gltfTextures = PerformanceAnalytics.GLTFTextureTracker.Get();
 0126            var abTextures = PerformanceAnalytics.ABTextureTracker.Get();
 0127            var promiseTextures = PerformanceAnalytics.PromiseTextureTracker.Get();
 0128            var queuedMessages = PerformanceAnalytics.MessagesEnqueuedTracker.Get();
 0129            var processedMessages = PerformanceAnalytics.MessagesProcessedTracker.Get();
 130
 0131            bool usingFPSCap = Settings.i.qualitySettings.Data.fpsCap;
 132
 0133            int hiccupsInThousandFrames = tracker.CurrentHiccupCount();
 134
 0135            float hiccupsTime = tracker.GetHiccupSum();
 136
 0137            float totalTime = tracker.GetTotalSeconds();
 138
 0139            WebInterface.PerformanceReportPayload performanceReportPayload = new WebInterface.PerformanceReportPayload
 140            {
 141                samples = encodedSamples,
 142                fpsIsCapped = usingFPSCap,
 143                hiccupsInThousandFrames = hiccupsInThousandFrames,
 144                hiccupsTime = hiccupsTime,
 145                totalTime = totalTime,
 146                gltfInProgress = gltfloading,
 147                gltfFailed = gltffailed,
 148                gltfCancelled = gltfcancelled,
 149                gltfLoaded = gltfloaded,
 150                abInProgress = abloading,
 151                abFailed = abfailed,
 152                abCancelled = abcancelled,
 153                abLoaded = abloaded,
 154                gltfTexturesLoaded = gltfTextures,
 155                abTexturesLoaded = abTextures,
 156                promiseTexturesLoaded = promiseTextures,
 157                enqueuedMessages = queuedMessages,
 158                processedMessages = processedMessages,
 159                playerCount = playerCount,
 160                loadRadius = (int)loadRadius,
 161                sceneScores = scenesMemoryScore,
 162                drawCalls = drawCalls,
 163                memoryReserved = totalMemoryReserved,
 164                memoryUsage = totalMemoryUsage,
 165                totalGCAlloc = totalGCAlloc
 166            };
 167
 0168            var result = JsonConvert.SerializeObject(performanceReportPayload);
 0169            WebInterface.SendPerformanceReport(result);
 0170            PerformanceAnalytics.ResetAll();
 0171        }
 172    }
 173}