< 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:62
Coverable lines:62
Total lines:159
Line coverage:0% (0 of 62)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:8
Method coverage:0% (0 of 8)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PerformanceMetricsController()0%2100%
OnFeatureFlagChange(...)0%6200%
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 DCL.Controllers;
 3using DCL.Interface;
 4using DCL.FPSDisplay;
 5using DCL.SettingsCommon;
 6using MainScripts.DCL.Analytics.PerformanceAnalytics;
 7using MainScripts.DCL.WorldRuntime.Debugging.Performance;
 8using Newtonsoft.Json;
 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 char[] encodedSamples = new char[SAMPLES_SIZE];
 019        private readonly Dictionary<int, long> scenesMemoryScore = new ();
 20
 21        private readonly PerformanceMetricsDataVariable performanceMetricsDataVariable;
 22        private readonly LinealBufferHiccupCounter tracker;
 23        private readonly IProfilerRecordsService profilerRecordsService;
 24
 025        private BaseVariable<FeatureFlag> featureFlags => DataStore.i.featureFlags.flags;
 026        private IWorldState worldState => Environment.i.world.state;
 027        private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers;
 028        private GeneralSettings generalSettings => Settings.i.generalSettings.Data;
 29
 30        private bool trackProfileRecords;
 31        private int currentIndex;
 32        private long totalAllocSample;
 33
 034        public PerformanceMetricsController()
 35        {
 036            performanceMetricsDataVariable = Resources.Load<PerformanceMetricsDataVariable>("ScriptableObjects/Performan
 37
 038            profilerRecordsService = Environment.i.serviceLocator.Get<IProfilerRecordsService>();
 039            tracker = new LinealBufferHiccupCounter(SAMPLES_SIZE);
 40
 041            featureFlags.OnChange += OnFeatureFlagChange;
 042            OnFeatureFlagChange(featureFlags.Get(), null);
 043        }
 44
 45        private void OnFeatureFlagChange(FeatureFlag current, FeatureFlag previous)
 46        {
 047            trackProfileRecords = current.IsFeatureEnabled(PROFILER_METRICS_FEATURE_FLAG);
 48
 049            if (trackProfileRecords)
 050                profilerRecordsService.RecordAdditionalProfilerMetrics();
 051        }
 52
 53        public void Update()
 54        {
 55#if !UNITY_EDITOR && UNITY_WEBGL
 56            if (!CommonScriptableObjects.focusState.Get())
 57                return;
 58#endif
 059            if (!CommonScriptableObjects.rendererState.Get() && !CommonScriptableObjects.forcePerformanceMeter.Get())
 060                return;
 61
 062            tracker.AddDeltaTime(Time.unscaledDeltaTime);
 063            performanceMetricsDataVariable.Set(profilerRecordsService.LastFPS, tracker.HiccupsCountInBuffer, tracker.Hic
 64
 065            encodedSamples[currentIndex++] = (char)profilerRecordsService.LastFrameTimeInMS;
 66
 067            if (trackProfileRecords)
 068                totalAllocSample +=  profilerRecordsService.GcAllocatedInFrame;
 69
 070            if (currentIndex == SAMPLES_SIZE)
 71            {
 072                currentIndex = 0;
 073                Report(new string(encodedSamples));
 074                totalAllocSample = 0;
 75            }
 076        }
 77
 78        private void Report(string encodedSamples)
 79        {
 080            var loadedScenesValues = worldState.GetLoadedScenes();
 081            scenesMemoryScore.Clear();
 82
 083            foreach (var parcelScene in loadedScenesValues)
 84            {
 85                // we ignore global scene
 086                IParcelScene parcelSceneValue = parcelScene.Value;
 87
 088                if (parcelSceneValue.isPersistent)
 89                    continue;
 90
 091                scenesMemoryScore.Add(parcelSceneValue.sceneData.sceneNumber, parcelSceneValue.metricsCounter.currentCou
 92            }
 93
 094            object drawCalls = null;
 095            object totalMemoryReserved = null;
 096            object totalMemoryUsage = null;
 097            object totalGCAlloc = null;
 98
 099            if (trackProfileRecords)
 100            {
 0101                drawCalls = (int)profilerRecordsService.DrawCalls;
 0102                totalMemoryReserved = profilerRecordsService.ReservedMemory;
 0103                totalMemoryUsage = profilerRecordsService.UsedMemory;
 0104                totalGCAlloc = totalAllocSample;
 105            }
 106
 0107            var playerCount = otherPlayers.Count();
 0108            var loadRadius = generalSettings.scenesLoadRadius;
 109
 0110            (int gltfloading, int gltffailed, int gltfcancelled, int gltfloaded) = PerformanceAnalytics.GLTFTracker.GetD
 0111            (int abloading, int abfailed, int abcancelled, int abloaded) = PerformanceAnalytics.ABTracker.GetData();
 0112            var gltfTextures = PerformanceAnalytics.GLTFTextureTracker.Get();
 0113            var abTextures = PerformanceAnalytics.ABTextureTracker.Get();
 0114            var promiseTextures = PerformanceAnalytics.PromiseTextureTracker.Get();
 0115            var queuedMessages = PerformanceAnalytics.MessagesEnqueuedTracker.Get();
 0116            var processedMessages = PerformanceAnalytics.MessagesProcessedTracker.Get();
 117
 0118            bool usingFPSCap = Settings.i.qualitySettings.Data.fpsCap;
 119
 0120            int hiccupsInThousandFrames = tracker.HiccupsCountInBuffer;
 0121            float hiccupsTime = tracker.HiccupsSum;
 122
 0123            float totalTime = tracker.TotalSeconds;
 124
 0125            WebInterface.PerformanceReportPayload performanceReportPayload = new WebInterface.PerformanceReportPayload
 126            {
 127                samples = encodedSamples,
 128                fpsIsCapped = usingFPSCap,
 129                hiccupsInThousandFrames = hiccupsInThousandFrames,
 130                hiccupsTime = hiccupsTime,
 131                totalTime = totalTime,
 132                gltfInProgress = gltfloading,
 133                gltfFailed = gltffailed,
 134                gltfCancelled = gltfcancelled,
 135                gltfLoaded = gltfloaded,
 136                abInProgress = abloading,
 137                abFailed = abfailed,
 138                abCancelled = abcancelled,
 139                abLoaded = abloaded,
 140                gltfTexturesLoaded = gltfTextures,
 141                abTexturesLoaded = abTextures,
 142                promiseTexturesLoaded = promiseTextures,
 143                enqueuedMessages = queuedMessages,
 144                processedMessages = processedMessages,
 145                playerCount = playerCount,
 146                loadRadius = (int)loadRadius,
 147                sceneScores = scenesMemoryScore,
 148                drawCalls = drawCalls,
 149                memoryReserved = totalMemoryReserved,
 150                memoryUsage = totalMemoryUsage,
 151                totalGCAlloc = totalGCAlloc
 152            };
 153
 0154            string result = JsonConvert.SerializeObject(performanceReportPayload);
 0155            WebInterface.SendPerformanceReport(result);
 0156            PerformanceAnalytics.ResetAll();
 0157        }
 158    }
 159}