< Summary

Class:GDX.Developer.Profiling
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Developer/Profiling.cs
Covered lines:0
Uncovered lines:50
Coverable lines:50
Total lines:125
Line coverage:0% (0 of 50)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:3
Method coverage:0% (0 of 3)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TakeMemorySnapshot(...)0%42600%
StartProfiling(...)0%56700%
StopProfiling()0%2100%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Developer/Profiling.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5#if !UNITY_DOTSRUNTIME
 6
 7using System;
 8using System.Collections.Generic;
 9using UnityEngine.Profiling;
 10using System.IO;
 11using UnityEngine.Profiling.Memory.Experimental;
 12
 13namespace GDX.Developer
 14{
 15    /// <summary>
 16    ///     A set of functionality useful for creating profiling data to reason about the performance of an application.
 17    /// </summary>
 18    /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception>
 19    public class Profiling
 20    {
 21        /// <summary>
 22        ///     The prefix to use with all capture files.
 23        /// </summary>
 24        const string k_MemoryCaptureFilePrefix = "MemCap-";
 25        /// <summary>
 26        ///     The number of memory captures to keep in the output folder.
 27        /// </summary>
 28        const int k_MemoryCapturesToKeep = 10;
 29        /// <summary>
 30        ///     The number of profile captures to keep in the output folder.
 31        /// </summary>
 32        const int k_ProfilesToKeep = 10;
 33        /// <summary>
 34        ///     The prefix to use with all binary profile files.
 35        /// </summary>
 36        const string k_ProfileFilePrefix = "Profile-";
 37
 38        /// <summary>
 39        ///     The default flags (all) used when capturing memory.
 40        /// </summary>
 41        const CaptureFlags k_AllCaptureFlags = CaptureFlags.ManagedObjects | CaptureFlags.NativeAllocations | CaptureFla
 42
 43        /// <summary>
 44        ///     Take a memory snapshot and save it to <see cref="Platform.GetOutputFolder"/>.
 45        /// </summary>
 46        /// <param name="prefix">Override the default prefix <see cref="k_MemoryCaptureFilePrefix"/>.</param>
 47        /// <param name="finishCallback">Optional callback action once the memory capture has been made.</param>
 48        /// <param name="captureFlags">Override of the memory capture flags, defaults to <see cref="k_AllCaptureFlags"/>
 49        /// <param name="manageCaptures">Should the number of captures found in the output folder be managed?</param>
 50        public static void TakeMemorySnapshot(string prefix = null, Action<string, bool> finishCallback = null, CaptureF
 051        {
 052            string outputFolder = Platform.GetOutputFolder();
 053            if (manageCaptures)
 054            {
 055                string[] files = Directory.GetFiles(outputFolder, prefix == null ? $"{k_MemoryCaptureFilePrefix}*" : $"{
 056                int filesToRemove = files.Length - (k_MemoryCapturesToKeep - 1);
 57
 058                if (filesToRemove > 0)
 059                {
 060                    List<string> fileList = new List<string>(files.Length);
 061                    fileList.AddRange(files);
 062                    fileList.Sort();
 63
 064                    for (int i = 0; i < filesToRemove; i++)
 065                    {
 066                        Platform.ForceDeleteFile(fileList[i]);
 067                    }
 068                }
 069            }
 70
 071            string path = Path.Combine(outputFolder, prefix != null ? $"{k_MemoryCaptureFilePrefix}{prefix}-{DateTime.No
 72                $"{k_MemoryCaptureFilePrefix}{DateTime.Now:GDX.Platform.FilenameTimestampFormat}.raw");
 073            MemoryProfiler.TakeSnapshot(path, finishCallback, captureFlags);
 074            Trace.Output(Trace.TraceLevel.Info, $"[MemorySnapshot] {path}");
 075        }
 76
 77        /// <summary>
 78        ///     Setup a profiling session used during an import. This will create a binary file when finished profiling.
 79        /// </summary>
 80        /// <param name="prefix">Optional descriptor for profile run used in filename.</param>
 81        /// <param name="manageProfiles">Should the number of profiles be managed.</param>
 82        public static void StartProfiling(string prefix = null, bool manageProfiles = true)
 083        {
 84            // Make sure it is off
 085            Profiler.enabled = false;
 86
 087            string outputFolder = Platform.GetOutputFolder();
 088            if (manageProfiles)
 089            {
 090                string[] files = Directory.GetFiles(outputFolder, prefix == null ? $"{k_ProfileFilePrefix}*" : $"{k_Prof
 91                // If we have 15 files, and our max is 15, we need to remove just one.
 092                int filesToRemove = files.Length - (k_ProfilesToKeep - 1);
 93
 094                if (filesToRemove > 0)
 095                {
 096                    List<string> fileList = new List<string>(files.Length);
 097                    fileList.AddRange(files);
 098                    fileList.Sort();
 99
 0100                    for (int i = 0; i < filesToRemove; i++)
 0101                    {
 0102                        Platform.ForceDeleteFile(fileList[i]);
 0103                    }
 0104                }
 0105            }
 106
 0107            string path = Path.Combine(outputFolder, prefix != null ? $"{k_ProfileFilePrefix}{prefix}-{Platform.Filename
 0108            Trace.Output(Trace.TraceLevel.Info, $"[Profiling Started] {path}");
 0109            Profiler.logFile = path;
 0110            Profiler.enableBinaryLog = true;
 0111            Profiler.enabled = true;
 0112        }
 113
 114        /// <summary>
 115        ///     Finalize a profiling session during an import.
 116        /// </summary>
 117        public static void StopProfiling()
 0118        {
 0119            Trace.Output(Trace.TraceLevel.Info, $"[Profiling Stopped] {Profiler.logFile}");
 0120            Profiler.enabled = false;
 0121            Profiler.logFile = "";
 0122        }
 123    }
 124}
 125#endif