< Summary

Class:GDX.Developer.Reports.ResourcesDiffReport
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Developer/Reports/ResourcesDiffReport.cs
Covered lines:130
Uncovered lines:14
Coverable lines:144
Total lines:223
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)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Developer/Reports/ResourcesDiffReport.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 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            }
 548            foreach (KeyValuePair<Type, long> kvp in rhs.KnownUsage)
 149            {
 150                if (!lhs.KnownUsage.ContainsKey(kvp.Key))
 151                {
 152                    KnownUsage.Add(kvp.Key, new LongDiff(0, kvp.Value));
 153                }
 154            }
 55
 56            // Known Objects - Start on right hand side
 2557            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in lhs.KnownObjects)
 1158            {
 59                // Lets just add something ahead to be safe
 1160                if (!AddedObjects.ContainsKey(kvpType.Key))
 1161                {
 1162                    AddedObjects.Add(kvpType.Key, new Dictionary<TransientReference, ObjectInfo>());
 1163                }
 1164                if (!RemovedObjects.ContainsKey(kvpType.Key))
 1165                {
 1166                    RemovedObjects.Add(kvpType.Key, new Dictionary<TransientReference, ObjectInfo>());
 1167                }
 1168                if (!CommonObjects.ContainsKey(kvpType.Key))
 1169                {
 1170                    CommonObjects.Add(kvpType.Key, new Dictionary<TransientReference, ObjectInfo>());
 1171                }
 72
 73                // RHS Dictionary?
 1174                Dictionary<TransientReference, ObjectInfo> rhsKnownObjects = null;
 1175                if (rhs.KnownObjects.ContainsKey(kvpType.Key))
 076                {
 077                    rhsKnownObjects = rhs.KnownObjects[kvpType.Key];
 078                }
 79
 80                // Iterate over everything in the left hand side
 182581                foreach (KeyValuePair<TransientReference, ObjectInfo> knownObject in kvpType.Value)
 89682                {
 83                    // None of this type in the right, means removed
 89684                    if (rhsKnownObjects == null)
 89685                    {
 89686                        RemovedObjects[kvpType.Key].Add(knownObject.Key, knownObject.Value);
 89687                    }
 88                    else
 089                    {
 090                        if (rhsKnownObjects.ContainsKey(knownObject.Key))
 091                        {
 092                            ObjectInfo clone = rhsKnownObjects[knownObject.Key].Clone();
 093                            clone.CopyCount -= knownObject.Value.CopyCount;
 94
 095                            CommonObjects[kvpType.Key].Add(knownObject.Key, clone);
 096                        }
 97                        else
 098                        {
 099                            RemovedObjects[kvpType.Key].Add(knownObject.Key, knownObject.Value);
 0100                        }
 101
 0102                    }
 896103                }
 11104            }
 105
 106            // Iterate over rhs for added
 5107            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in rhs.KnownObjects)
 1108            {
 1109                if (!lhs.KnownObjects.ContainsKey(kvpType.Key))
 1110                {
 1111                    AddedObjects.Add(kvpType.Key, new Dictionary<TransientReference, ObjectInfo>());
 112                    // Iterate over everything in the left hand side
 14721113                    foreach (KeyValuePair<TransientReference, ObjectInfo> knownObject in kvpType.Value)
 7359114                    {
 7359115                        AddedObjects[kvpType.Key].Add(knownObject.Key, knownObject.Value);
 7359116                    }
 1117                }
 1118            }
 119
 1120            List<Type> removeType = new List<Type>();
 27121            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in AddedObjects)
 12122            {
 12123                if (kvpType.Value.Count == 0)
 11124                {
 11125                    removeType.Add(kvpType.Key);
 11126                }
 12127            }
 25128            foreach (Type type in removeType)
 11129            {
 11130                AddedObjects.Remove(type);
 11131            }
 1132            removeType.Clear();
 25133            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in RemovedObjects)
 11134            {
 11135                if (kvpType.Value.Count == 0)
 1136                {
 1137                    removeType.Add(kvpType.Key);
 1138                }
 11139            }
 5140            foreach (Type type in removeType)
 1141            {
 1142                RemovedObjects.Remove(type);
 1143            }
 1144            removeType.Clear();
 25145            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> kvpType in CommonObjects)
 11146            {
 11147                if (kvpType.Value.Count == 0)
 11148                {
 11149                    removeType.Add(kvpType.Key);
 11150                }
 11151            }
 25152            foreach (Type type in removeType)
 11153            {
 11154                CommonObjects.Remove(type);
 11155            }
 1156        }
 157
 158        /// <inheritdoc />
 159        public override bool Output(StringBuilder builder, ResourceReportContext context = null)
 1160        {
 161            // We need to make the context if its not provided
 1162            context ??= new ResourceReportContext();
 163
 164            // Create header
 1165            builder.AppendLine(CreateHeader(context, "START: Resources Diff Report"));
 166
 167            // Custom header information
 1168            builder.AppendLine(CreateKeyValuePair(context,"Total Objects", ObjectCount.GetOutput(context)));
 1169            builder.AppendLine();
 27170            foreach (KeyValuePair<Type, LongDiff> usage in KnownUsage)
 12171            {
 12172                builder.AppendLine(CreateKeyValuePair(context,usage.Key.ToString(), usage.Value.GetSizeOutput(context)))
 12173            }
 1174            builder.AppendLine();
 175
 176            // Add memory information
 1177            MemoryContext.Output(context, builder);
 1178            builder.AppendLine();
 179
 1180            builder.AppendLine(CreateHeader(context, "Added Objects"));
 1181            OutputObjectInfos(builder, context, AddedObjects);
 182
 1183            builder.AppendLine(CreateHeader(context,"Common Objects"));
 1184            OutputObjectInfos(builder, context, CommonObjects);
 185
 1186            builder.AppendLine(CreateHeader(context,"Removed Objects"));
 1187            OutputObjectInfos(builder, context, RemovedObjects);
 188
 189            // Footer
 1190            builder.AppendLine(CreateHeader(context,"END: Resources Diff Report"));
 191
 1192            return true;
 1193        }
 194
 195        static void OutputObjectInfos(StringBuilder builder, ResourceReportContext context, Dictionary<Type, Dictionary<
 3196        {
 31197            foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> target in targetObjects)
 11198            {
 11199                int count = target.Value.Count;
 200
 11201                builder.AppendLine(CreateHeader(context, $"{target.Key} [{count.ToString()}] ", '-'));
 202
 203                // Sort the known objects based on size as that's the most useful context to have them listed
 11204                List<ObjectInfo> newList = new List<ObjectInfo>(count);
 16543205                foreach (KeyValuePair<TransientReference, ObjectInfo> objectInfo in target.Value)
 8255206                {
 8255207                    newList.Add(objectInfo.Value);
 8255208                }
 209
 11210                newList.Sort();
 211
 212                // Output each item
 16532213                for (int i = 0; i < count; i++)
 8255214                {
 8255215                    newList[i].Output(context, builder);
 8255216                }
 217
 11218                builder.AppendLine();
 11219            }
 3220        }
 221    }
 222}
 223#endif