< Summary

Class:GDX.Developer.Reports.ResourcesDiffReport
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/Reports/ResourcesDiffReport.cs
Covered lines:130
Uncovered lines:14
Coverable lines:144
Total lines:233
Line coverage:90.2% (130 of 144)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:3
Method coverage:100% (3 of 3)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ResourcesDiffReport(...)0%25.6125090.07%
Output(...)0%330100%
OutputObjectInfos(...)0%440100%

File(s)

./Packages/com.dotbunny.gdx/GDX/Developer/Reports/ResourcesDiffReport.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.Text;
 10using GDX.Developer.Reports.Resource;
 11using GDX.Developer.Reports.Resource.Objects;
 12using GDX.Developer.Reports.Resource.Sections;
 13
 14namespace GDX.Developer.Reports
 15{
 16    /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception>
 17    public class ResourcesDiffReport : ResourceReport
 18    {
 119        public readonly Dictionary<Type, Dictionary<TransientReference, ObjectInfo>> AddedObjects =
 20            new Dictionary<Type, Dictionary<TransientReference, ObjectInfo>>();
 21
 122        public readonly Dictionary<Type, Dictionary<TransientReference, ObjectInfo>> CommonObjects =
 23            new Dictionary<Type, Dictionary<TransientReference, ObjectInfo>>();
 24
 125        public readonly Dictionary<Type, LongDiff> KnownUsage =
 26            new Dictionary<Type, LongDiff>();
 27
 28        public readonly MemoryDiffSection MemoryContext;
 29
 30        public readonly IntegerDiff ObjectCount;
 31
 132        public readonly Dictionary<Type, Dictionary<TransientReference, ObjectInfo>> RemovedObjects =
 33            new Dictionary<Type, Dictionary<TransientReference, ObjectInfo>>();
 34
 135        public ResourcesDiffReport(ResourcesAuditReport lhs, ResourcesAuditReport rhs)
 136        {
 137            ObjectCount = new IntegerDiff(lhs.ObjectCount, rhs.ObjectCount);
 138            MemoryContext = MemoryDiffSection.Get(lhs.MemoryContext, rhs.MemoryContext);
 39
 40            // Known Usage Changes
 2541            foreach (KeyValuePair<Type, long> kvp in lhs.KnownUsage)
 1142            {
 1143                KnownUsage.Add(kvp.Key,
 44                    rhs.KnownUsage.ContainsKey(kvp.Key)
 45                        ? new LongDiff(kvp.Value, rhs.KnownUsage[kvp.Key])
 46                        : new LongDiff(kvp.Value, 0));
 1147            }
 48
 549            foreach (KeyValuePair<Type, long> kvp in rhs.KnownUsage)
 150            {
 151                if (!lhs.KnownUsage.ContainsKey(kvp.Key))
 152                {
 153                    KnownUsage.Add(kvp.Key, new LongDiff(0, kvp.Value));
 154                }
 155            }
 56
 57            // Known Objects - Start on right hand side
 2558            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in lhs.KnownObjects)
 1159            {
 60                // Lets just add something ahead to be safe
 1161                if (!AddedObjects.ContainsKey(kvpType.Key))
 1162                {
 1163                    AddedObjects.Add(kvpType.Key, new Dictionary<TransientReference, ObjectInfo>());
 1164                }
 65
 1166                if (!RemovedObjects.ContainsKey(kvpType.Key))
 1167                {
 1168                    RemovedObjects.Add(kvpType.Key, new Dictionary<TransientReference, ObjectInfo>());
 1169                }
 70
 1171                if (!CommonObjects.ContainsKey(kvpType.Key))
 1172                {
 1173                    CommonObjects.Add(kvpType.Key, new Dictionary<TransientReference, ObjectInfo>());
 1174                }
 75
 76                // RHS Dictionary?
 1177                Dictionary<TransientReference, ObjectInfo> rhsKnownObjects = null;
 1178                if (rhs.KnownObjects.ContainsKey(kvpType.Key))
 079                {
 080                    rhsKnownObjects = rhs.KnownObjects[kvpType.Key];
 081                }
 82
 83                // Iterate over everything in the left hand side
 183184                foreach (KeyValuePair<TransientReference, ObjectInfo> knownObject in kvpType.Value)
 89985                {
 86                    // None of this type in the right, means removed
 89987                    if (rhsKnownObjects == null)
 89988                    {
 89989                        RemovedObjects[kvpType.Key].Add(knownObject.Key, knownObject.Value);
 89990                    }
 91                    else
 092                    {
 093                        if (rhsKnownObjects.ContainsKey(knownObject.Key))
 094                        {
 095                            ObjectInfo clone = rhsKnownObjects[knownObject.Key].Clone();
 096                            clone.CopyCount -= knownObject.Value.CopyCount;
 97
 098                            CommonObjects[kvpType.Key].Add(knownObject.Key, clone);
 099                        }
 100                        else
 0101                        {
 0102                            RemovedObjects[kvpType.Key].Add(knownObject.Key, knownObject.Value);
 0103                        }
 0104                    }
 899105                }
 11106            }
 107
 108            // Iterate over rhs for added
 5109            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in rhs.KnownObjects)
 1110            {
 1111                if (!lhs.KnownObjects.ContainsKey(kvpType.Key))
 1112                {
 1113                    AddedObjects.Add(kvpType.Key, new Dictionary<TransientReference, ObjectInfo>());
 114                    // Iterate over everything in the left hand side
 14505115                    foreach (KeyValuePair<TransientReference, ObjectInfo> knownObject in kvpType.Value)
 7251116                    {
 7251117                        AddedObjects[kvpType.Key].Add(knownObject.Key, knownObject.Value);
 7251118                    }
 1119                }
 1120            }
 121
 1122            List<Type> removeType = new List<Type>();
 27123            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in AddedObjects)
 12124            {
 12125                if (kvpType.Value.Count == 0)
 11126                {
 11127                    removeType.Add(kvpType.Key);
 11128                }
 12129            }
 130
 25131            foreach (Type type in removeType)
 11132            {
 11133                AddedObjects.Remove(type);
 11134            }
 135
 1136            removeType.Clear();
 25137            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in RemovedObjects)
 11138            {
 11139                if (kvpType.Value.Count == 0)
 1140                {
 1141                    removeType.Add(kvpType.Key);
 1142                }
 11143            }
 144
 5145            foreach (Type type in removeType)
 1146            {
 1147                RemovedObjects.Remove(type);
 1148            }
 149
 1150            removeType.Clear();
 25151            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in CommonObjects)
 11152            {
 11153                if (kvpType.Value.Count == 0)
 11154                {
 11155                    removeType.Add(kvpType.Key);
 11156                }
 11157            }
 158
 25159            foreach (Type type in removeType)
 11160            {
 11161                CommonObjects.Remove(type);
 11162            }
 1163        }
 164
 165        /// <inheritdoc />
 166        public override bool Output(StringBuilder builder, ResourceReportContext context = null)
 1167        {
 168            // We need to make the context if its not provided
 1169            context ??= new ResourceReportContext();
 170
 171            // Create header
 1172            builder.AppendLine(CreateHeader(context, "START: Resources Diff Report"));
 173
 174            // Custom header information
 1175            builder.AppendLine(CreateKeyValuePair(context, "Total Objects", ObjectCount.GetOutput(context)));
 1176            builder.AppendLine();
 27177            foreach (KeyValuePair<Type, LongDiff> usage in KnownUsage)
 12178            {
 12179                builder.AppendLine(
 180                    CreateKeyValuePair(context, usage.Key.ToString(), usage.Value.GetSizeOutput(context)));
 12181            }
 182
 1183            builder.AppendLine();
 184
 185            // Add memory information
 1186            MemoryContext.Output(context, builder);
 1187            builder.AppendLine();
 188
 1189            builder.AppendLine(CreateHeader(context, "Added Objects"));
 1190            OutputObjectInfos(builder, context, AddedObjects);
 191
 1192            builder.AppendLine(CreateHeader(context, "Common Objects"));
 1193            OutputObjectInfos(builder, context, CommonObjects);
 194
 1195            builder.AppendLine(CreateHeader(context, "Removed Objects"));
 1196            OutputObjectInfos(builder, context, RemovedObjects);
 197
 198            // Footer
 1199            builder.AppendLine(CreateHeader(context, "END: Resources Diff Report"));
 200
 1201            return true;
 1202        }
 203
 204        static void OutputObjectInfos(StringBuilder builder, ResourceReportContext context,
 205            Dictionary<Type, Dictionary<TransientReference, ObjectInfo>> targetObjects)
 3206        {
 31207            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> target in targetObjects)
 11208            {
 11209                int count = target.Value.Count;
 210
 11211                builder.AppendLine(CreateHeader(context, $"{target.Key} [{count.ToString()}] ", '-'));
 212
 213                // Sort the known objects based on size as that's the most useful context to have them listed
 11214                List<ObjectInfo> newList = new List<ObjectInfo>(count);
 16333215                foreach (KeyValuePair<TransientReference, ObjectInfo> objectInfo in target.Value)
 8150216                {
 8150217                    newList.Add(objectInfo.Value);
 8150218                }
 219
 11220                newList.Sort();
 221
 222                // Output each item
 16322223                for (int i = 0; i < count; i++)
 8150224                {
 8150225                    newList[i].Output(context, builder);
 8150226                }
 227
 11228                builder.AppendLine();
 11229            }
 3230        }
 231    }
 232}
 233#endif // !UNITY_DOTSRUNTIME