< 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:65
Coverable lines:65
Total lines:170
Line coverage:0% (0 of 65)
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%20400%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL.Controllers;
 3using DCL.Interface;
 4using DCL.FPSDisplay;
 5using DCL.SettingsCommon;
 6using MainScripts.DCL.Analytics.PerformanceAnalytics;
 7using Newtonsoft.Json;
 8using Unity.Profiling;
 9using UnityEngine;
 10
 11namespace DCL
 12{
 13    public class PerformanceMetricsController
 14    {
 15        private const int SAMPLES_SIZE = 1000; // Send performance report every 1000 samples
 16        private const string PROFILER_METRICS_FEATURE_FLAG = "profiler_metrics";
 17
 018        private readonly LinealBufferHiccupCounter tracker = new LinealBufferHiccupCounter();
 019        private readonly char[] encodedSamples = new char[SAMPLES_SIZE];
 20        private readonly PerformanceMetricsDataVariable performanceMetricsDataVariable;
 021        private IWorldState worldState => Environment.i.world.state;
 022        private BaseVariable<FeatureFlag> featureFlags => DataStore.i.featureFlags.flags;
 023        private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers;
 024        private GeneralSettings generalSettings => Settings.i.generalSettings.Data;
 25        private ProfilerRecorder drawCallsRecorder;
 26        private ProfilerRecorder reservedMemoryRecorder;
 27        private ProfilerRecorder usedMemoryRecorder;
 28        private ProfilerRecorder gcAllocatedInFrameRecorder;
 29        private bool trackProfileRecords;
 30        private int currentIndex = 0;
 31        private long totalAllocSample;
 32        private bool isTrackingProfileRecords = false;
 033        private readonly Dictionary<string, long> scenesMemoryScore = new Dictionary<string, long>();
 34
 035        public PerformanceMetricsController()
 36        {
 037            performanceMetricsDataVariable = Resources.Load<PerformanceMetricsDataVariable>("ScriptableObjects/Performan
 38
 039            featureFlags.OnChange += OnFeatureFlagChange;
 040            OnFeatureFlagChange(featureFlags.Get(), null);
 041        }
 42        private void OnFeatureFlagChange(FeatureFlag current, FeatureFlag previous)
 43        {
 044            trackProfileRecords = current.IsFeatureEnabled(PROFILER_METRICS_FEATURE_FLAG);
 45
 046            if (trackProfileRecords && !isTrackingProfileRecords)
 47            {
 048                drawCallsRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Render, "Draw Calls Count");
 049                reservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Reserved Memory");
 050                usedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Used Memory");
 051                gcAllocatedInFrameRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Allocated In Frame")
 052                isTrackingProfileRecords = true;
 53            }
 054        }
 55
 56        public void Update()
 57        {
 58#if !UNITY_EDITOR && UNITY_WEBGL
 59            if (!CommonScriptableObjects.focusState.Get())
 60                return;
 61#endif
 062            if (!CommonScriptableObjects.rendererState.Get() && !CommonScriptableObjects.forcePerformanceMeter.Get())
 063                return;
 64
 065            var deltaInMs = Time.deltaTime * 1000;
 66
 067            tracker.AddDeltaTime(Time.deltaTime);
 68
 069            performanceMetricsDataVariable.Set(tracker.CurrentFPSCount(),
 70                tracker.CurrentHiccupCount(),
 71                tracker.HiccupsSum,
 72                tracker.GetTotalSeconds());
 73
 074            encodedSamples[currentIndex++] = (char) deltaInMs;
 75
 076            if (trackProfileRecords)
 77            {
 078                totalAllocSample += gcAllocatedInFrameRecorder.LastValue;
 79            }
 80
 081            if (currentIndex == SAMPLES_SIZE)
 82            {
 083                currentIndex = 0;
 084                Report(new string(encodedSamples));
 085                totalAllocSample = 0;
 86            }
 87
 088        }
 89
 90        private void Report(string encodedSamples)
 91        {
 92
 093            Dictionary<string, IParcelScene>.ValueCollection loadedScenesValues = worldState.loadedScenes.Values;
 094            scenesMemoryScore.Clear();
 095            foreach (IParcelScene parcelScene in loadedScenesValues)
 96            {
 97                // we ignore global scene
 098                if (parcelScene.isPersistent)
 99                    continue;
 100
 0101                scenesMemoryScore.Add(parcelScene.sceneData.id, parcelScene.metricsCounter.currentCount.totalMemoryScore
 102            }
 103
 0104            object drawCalls = null;
 0105            object totalMemoryReserved = null;
 0106            object totalMemoryUsage = null;
 0107            object totalGCAlloc = null;
 108
 0109            if (trackProfileRecords)
 110            {
 0111                drawCalls = (int)drawCallsRecorder.LastValue;
 0112                totalMemoryReserved = reservedMemoryRecorder.LastValue;
 0113                totalMemoryUsage = usedMemoryRecorder.LastValue;
 0114                totalGCAlloc = totalAllocSample;
 115            }
 116
 0117            var playerCount = otherPlayers.Count();
 0118            var loadRadius = generalSettings.scenesLoadRadius;
 119
 0120            (int gltfloading, int gltffailed, int gltfcancelled, int gltfloaded) = PerformanceAnalytics.GLTFTracker.GetD
 0121            (int abloading, int abfailed, int abcancelled, int abloaded) = PerformanceAnalytics.ABTracker.GetData();
 0122            var gltfTextures = PerformanceAnalytics.GLTFTextureTracker.Get();
 0123            var abTextures = PerformanceAnalytics.ABTextureTracker.Get();
 0124            var promiseTextures = PerformanceAnalytics.PromiseTextureTracker.Get();
 0125            var queuedMessages = PerformanceAnalytics.MessagesEnqueuedTracker.Get();
 0126            var processedMessages = PerformanceAnalytics.MessagesProcessedTracker.Get();
 127
 0128            bool usingFPSCap = Settings.i.qualitySettings.Data.fpsCap;
 129
 0130            int hiccupsInThousandFrames = tracker.CurrentHiccupCount();
 131
 0132            float hiccupsTime = tracker.GetHiccupSum();
 133
 0134            float totalTime = tracker.GetTotalSeconds();
 135
 0136            WebInterface.PerformanceReportPayload performanceReportPayload = new WebInterface.PerformanceReportPayload
 137            {
 138                samples = encodedSamples,
 139                fpsIsCapped = usingFPSCap,
 140                hiccupsInThousandFrames = hiccupsInThousandFrames,
 141                hiccupsTime = hiccupsTime,
 142                totalTime = totalTime,
 143                gltfInProgress = gltfloading,
 144                gltfFailed = gltffailed,
 145                gltfCancelled = gltfcancelled,
 146                gltfLoaded = gltfloaded,
 147                abInProgress = abloading,
 148                abFailed = abfailed,
 149                abCancelled = abcancelled,
 150                abLoaded = abloaded,
 151                gltfTexturesLoaded = gltfTextures,
 152                abTexturesLoaded = abTextures,
 153                promiseTexturesLoaded = promiseTextures,
 154                enqueuedMessages = queuedMessages,
 155                processedMessages = processedMessages,
 156                playerCount = playerCount,
 157                loadRadius = (int)loadRadius,
 158                sceneScores = scenesMemoryScore,
 159                drawCalls = drawCalls,
 160                memoryReserved = totalMemoryReserved,
 161                memoryUsage = totalMemoryUsage,
 162                totalGCAlloc = totalGCAlloc
 163            };
 164
 0165            var result = JsonConvert.SerializeObject(performanceReportPayload);
 0166            WebInterface.SendPerformanceReport(result);
 0167            PerformanceAnalytics.ResetAll();
 0168        }
 169    }
 170}