< Summary

Class:MainScripts.DCL.WorldRuntime.Debugging.Performance.ProfilerRecordsService
Assembly:PerformanceController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Debugging/Performance/ProfilerRecordsService.cs
Covered lines:10
Uncovered lines:32
Coverable lines:42
Total lines:105
Line coverage:23.8% (10 of 42)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:14
Method coverage:21.4% (3 of 14)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProfilerRecordsService()0%110100%
Initialize()0%110100%
Dispose()0%110100%
RecordAdditionalProfilerMetrics()0%6200%
StartRecordGCAllocatedInFrame()0%6200%
StopRecordGCAllocatedInFrame()0%6200%
GetRecorderFrameAverage(...)0%12300%

File(s)

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

#LineLine coverage
 1using Unity.Profiling;
 2
 3namespace MainScripts.DCL.WorldRuntime.Debugging.Performance
 4{
 5    public class ProfilerRecordsService : IProfilerRecordsService
 6    {
 7        private const int CAPACITY = 15; // Amounts of frames used for calculating average FPS
 8
 9        private ProfilerRecorder mainThreadTimeRecorder;
 10
 11        private ProfilerRecorder drawCallsRecorder;
 12        private ProfilerRecorder reservedMemoryRecorder;
 13        private ProfilerRecorder usedMemoryRecorder;
 14        private ProfilerRecorder gcAllocatedInFrameRecorder;
 15
 16        private bool isRecordingAdditionalProfilers;
 17
 018        public float LastFrameTimeInMS => mainThreadTimeRecorder.LastValue * 1e-6f; // [sec]
 019        public float LastFPS => 1 / (mainThreadTimeRecorder.LastValue * 1e-9f);
 20
 21        /// <summary>
 22        /// Average frame time and FPS. In-game use only due to overhead (not for external analytics)
 23        /// </summary>
 24        public (float FrameTime, float FPS) AverageData
 25        {
 26            get
 27            {
 028                float frameTime = GetRecorderFrameAverage(mainThreadTimeRecorder) * 1e-6f; // [ms]
 029                return (FrameTime: frameTime, FPS: 1000 / frameTime);
 30            }
 31        }
 32
 33        // Additional recordings
 034        public long GcAllocatedInFrame => gcAllocatedInFrameRecorder.LastValue;
 035        public long UsedMemory => usedMemoryRecorder.LastValue;
 036        public long ReservedMemory => reservedMemoryRecorder.LastValue;
 037        public long DrawCalls => drawCallsRecorder.LastValue;
 38
 42539        public ProfilerRecordsService()
 40        {
 42541            mainThreadTimeRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Internal, "Main Thread", CAPACITY);
 42542        }
 43
 42544        public void Initialize() { }
 45
 46        public void Dispose()
 47        {
 42548            mainThreadTimeRecorder.Dispose();
 49
 42550            drawCallsRecorder.Dispose();
 42551            reservedMemoryRecorder.Dispose();
 42552            usedMemoryRecorder.Dispose();
 42553            gcAllocatedInFrameRecorder.Dispose();
 42554        }
 55
 56        public void RecordAdditionalProfilerMetrics()
 57        {
 058            if (isRecordingAdditionalProfilers) return;
 59
 060            drawCallsRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Render, "Draw Calls Count");
 061            reservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Reserved Memory");
 062            usedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Used Memory");
 063            gcAllocatedInFrameRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Allocated In Frame");
 64
 065            isRecordingAdditionalProfilers = true;
 066        }
 67
 68        public void StartRecordGCAllocatedInFrame()
 69        {
 070            if (isRecordingAdditionalProfilers)
 071                return;
 72
 073            gcAllocatedInFrameRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Allocated In Frame");
 074        }
 75
 76        public void StopRecordGCAllocatedInFrame()
 77        {
 078            if (isRecordingAdditionalProfilers)
 079                return;
 80
 081            gcAllocatedInFrameRecorder.Dispose();
 082        }
 83
 84        private static float GetRecorderFrameAverage(ProfilerRecorder recorder)
 85        {
 086            int samplesCount = recorder.Capacity;
 87
 088            if (samplesCount == 0)
 089                return 0;
 90
 091            float r = 0;
 92
 93            unsafe
 94            {
 095                ProfilerRecorderSample* samples = stackalloc ProfilerRecorderSample[samplesCount];
 096                recorder.CopyTo(samples, samplesCount);
 97
 098                for (var i = 0; i < samplesCount; ++i)
 099                    r += samples[i].Value;
 100            }
 101
 0102            return r / samplesCount;
 103        }
 104    }
 105}