< Summary

Class:GDX.Developer.Profiling
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/Profiling.cs
Covered lines:0
Uncovered lines:50
Coverable lines:50
Total lines:145
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)

./Packages/com.dotbunny.gdx/GDX/Developer/Profiling.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 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 System.IO;
 10using UnityEngine.Profiling;
 11#if UNITY_2022_2_OR_NEWER
 12using Unity.Profiling.Memory;
 13
 14#else
 15using UnityEngine.Profiling.Memory.Experimental;
 16#endif // UNITY_2022_2_OR_NEWER
 17
 18namespace GDX.Developer
 19{
 20    /// <summary>
 21    ///     A set of functionality useful for creating profiling data to reason about the performance of an application.
 22    /// </summary>
 23    /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception>
 24    public class Profiling
 25    {
 26        /// <summary>
 27        ///     The prefix to use with all capture files.
 28        /// </summary>
 29        const string k_MemoryCaptureFilePrefix = "MemCap-";
 30
 31        /// <summary>
 32        ///     The number of memory captures to keep in the output folder.
 33        /// </summary>
 34        const int k_MemoryCapturesToKeep = 10;
 35
 36        /// <summary>
 37        ///     The number of profile captures to keep in the output folder.
 38        /// </summary>
 39        const int k_ProfilesToKeep = 10;
 40
 41        /// <summary>
 42        ///     The prefix to use with all binary profile files.
 43        /// </summary>
 44        const string k_ProfileFilePrefix = "Profile-";
 45
 46        /// <summary>
 47        ///     The default flags (all) used when capturing memory.
 48        /// </summary>
 49        const CaptureFlags k_AllCaptureFlags = CaptureFlags.ManagedObjects | CaptureFlags.NativeAllocations |
 50                                               CaptureFlags.NativeObjects | CaptureFlags.NativeAllocationSites |
 51                                               CaptureFlags.NativeStackTraces;
 52
 53        /// <summary>
 54        ///     Take a memory snapshot and save it to <see cref="Platform.GetOutputFolder" />.
 55        /// </summary>
 56        /// <param name="prefix">Override the default prefix <see cref="k_MemoryCaptureFilePrefix" />.</param>
 57        /// <param name="finishCallback">Optional callback action once the memory capture has been made.</param>
 58        /// <param name="captureFlags">Override of the memory capture flags, defaults to <see cref="k_AllCaptureFlags" /
 59        /// <param name="manageCaptures">Should the number of captures found in the output folder be managed?</param>
 60        public static void TakeMemorySnapshot(string prefix = null, Action<string, bool> finishCallback = null,
 61            CaptureFlags captureFlags = k_AllCaptureFlags, bool manageCaptures = true)
 062        {
 063            string outputFolder = Platform.GetOutputFolder();
 064            if (manageCaptures)
 065            {
 066                string[] files = Directory.GetFiles(outputFolder,
 67                    prefix == null ? $"{k_MemoryCaptureFilePrefix}*" : $"{k_MemoryCaptureFilePrefix}{prefix}-*",
 68                    SearchOption.TopDirectoryOnly);
 069                int filesToRemove = files.Length - (k_MemoryCapturesToKeep - 1);
 70
 071                if (filesToRemove > 0)
 072                {
 073                    List<string> fileList = new List<string>(files.Length);
 074                    fileList.AddRange(files);
 075                    fileList.Sort();
 76
 077                    for (int i = 0; i < filesToRemove; i++)
 078                    {
 079                        Platform.ForceDeleteFile(fileList[i]);
 080                    }
 081                }
 082            }
 83
 084            string path = Path.Combine(outputFolder,
 85                prefix != null
 86                    ? $"{k_MemoryCaptureFilePrefix}{prefix}-{DateTime.Now:GDX.Platform.FilenameTimestampFormat}.snap"
 87                    : $"{k_MemoryCaptureFilePrefix}{DateTime.Now:GDX.Platform.FilenameTimestampFormat}.raw");
 088            MemoryProfiler.TakeSnapshot(path, finishCallback, captureFlags);
 089            UnityEngine.Debug.Log($"[MemorySnapshot] {path}");
 090        }
 91
 92        /// <summary>
 93        ///     Setup a profiling session used during an import. This will create a binary file when finished profiling.
 94        /// </summary>
 95        /// <param name="prefix">Optional descriptor for profile run used in filename.</param>
 96        /// <param name="manageProfiles">Should the number of profiles be managed.</param>
 97        public static void StartProfiling(string prefix = null, bool manageProfiles = true)
 098        {
 99            // Make sure it is off
 0100            Profiler.enabled = false;
 101
 0102            string outputFolder = Platform.GetOutputFolder();
 0103            if (manageProfiles)
 0104            {
 0105                string[] files = Directory.GetFiles(outputFolder,
 106                    prefix == null ? $"{k_ProfileFilePrefix}*" : $"{k_ProfileFilePrefix}{prefix}-*",
 107                    SearchOption.TopDirectoryOnly);
 108                // If we have 15 files, and our max is 15, we need to remove just one.
 0109                int filesToRemove = files.Length - (k_ProfilesToKeep - 1);
 110
 0111                if (filesToRemove > 0)
 0112                {
 0113                    List<string> fileList = new List<string>(files.Length);
 0114                    fileList.AddRange(files);
 0115                    fileList.Sort();
 116
 0117                    for (int i = 0; i < filesToRemove; i++)
 0118                    {
 0119                        Platform.ForceDeleteFile(fileList[i]);
 0120                    }
 0121                }
 0122            }
 123
 0124            string path = Path.Combine(outputFolder,
 125                prefix != null
 126                    ? $"{k_ProfileFilePrefix}{prefix}-{Platform.FilenameTimestampFormat}.raw"
 127                    : $"{k_ProfileFilePrefix}{Platform.FilenameTimestampFormat}.raw");
 0128            UnityEngine.Debug.Log($"[Profiling Started] {path}");
 0129            Profiler.logFile = path;
 0130            Profiler.enableBinaryLog = true;
 0131            Profiler.enabled = true;
 0132        }
 133
 134        /// <summary>
 135        ///     Finalize a profiling session during an import.
 136        /// </summary>
 137        public static void StopProfiling()
 0138        {
 0139            UnityEngine.Debug.Log($"[Profiling Stopped] {Profiler.logFile}");
 0140            Profiler.enabled = false;
 0141            Profiler.logFile = "";
 0142        }
 143    }
 144}
 145#endif // !UNITY_DOTSRUNTIME