From 4c4e5f4b2e669199127ebc2f1e1bb75a7dfe4a53 Mon Sep 17 00:00:00 2001 From: Ullrich Praetz Date: Fri, 19 Jul 2024 14:43:37 +0200 Subject: [PATCH] Engine - Tests: add Test_Entity_Tree_Allocation() --- .../src/Tests/ECS/Entity/Test_Entity_Tree.cs | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/Engine/src/Tests/ECS/Entity/Test_Entity_Tree.cs b/Engine/src/Tests/ECS/Entity/Test_Entity_Tree.cs index c73af6008..d65b7f5da 100644 --- a/Engine/src/Tests/ECS/Entity/Test_Entity_Tree.cs +++ b/Engine/src/Tests/ECS/Entity/Test_Entity_Tree.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using Friflo.Engine.ECS; using NUnit.Framework; using Tests.Utils; @@ -425,8 +426,48 @@ public static void Test_Entity_Tree_move_with_AddChild() } [Test] - public static void Test_Entity_Tree_Allocation() { + public static void Test_Entity_Tree_Allocation() + { + int count = 10; // 1000 + // Test_Entity_Tree_Allocation - count: 1000 entities: 1001001 duration: 360 ms + var store = new EntityStore(); + var root = store.CreateEntity(1); + var type = store.GetArchetype(default); + var children = type.CreateEntities(count).ToArray(); + var subChildren = type.CreateEntities(count * count).ToArray(); + store.SetStoreRoot(root); + for (int repeat = 0; repeat < 2; repeat++) + { + var sw = new Stopwatch(); + sw.Start(); + var start = Mem.GetAllocatedBytes(); + int subIndex = 0; + foreach (var child in children) + { + root.AddChild(child); + for (int i = 0; i < count; i++) { + var subChild = subChildren[subIndex++]; + child.AddChild(subChild); + } + Mem.AreEqual(count, child.ChildCount); + } + Mem.AreEqual(count, root.ChildCount); + + for (int n = count - 1; n >= 0; n--) + { + var child = children[n]; + var childEntities = child.ChildEntities; + for (int i = count - 1; i >= 0; i--) { + Mem.IsTrue(child.RemoveChild(childEntities[i])); + } + Mem.IsTrue(root.RemoveChild(child)); + } + if (repeat > 0) { + Mem.AssertNoAlloc(start); + Console.WriteLine($"Test_Entity_Tree_Allocation - count: {count} entities: {store.Count} duration: {sw.ElapsedMilliseconds} ms"); + } + } } [Test]