From 4b64aa2b9dffdf76a0c17c47c4060cde0266f188 Mon Sep 17 00:00:00 2001 From: Tyler Young Date: Mon, 22 Jun 2020 04:43:47 -0400 Subject: [PATCH 1/2] fix benchmark improve some test diagnostics fix some bug where order of shutdown of AiControllerComponent mattered or Processor was never initialized --- .../ComponentManagerGetAllComponents.cs | 37 ++++++++++++++++++- Content.Benchmarks/Program.cs | 3 +- Content.IntegrationTests/Tests/EntityTest.cs | 3 +- .../Tests/SaveLoadSaveTest.cs | 22 +++++++++-- .../Items/Storage/ServerStorageComponent.cs | 4 +- .../Movement/AiControllerComponent.cs | 2 +- 6 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Content.Benchmarks/ComponentManagerGetAllComponents.cs b/Content.Benchmarks/ComponentManagerGetAllComponents.cs index 95063ba8d0..a2db430944 100644 --- a/Content.Benchmarks/ComponentManagerGetAllComponents.cs +++ b/Content.Benchmarks/ComponentManagerGetAllComponents.cs @@ -2,9 +2,13 @@ using System; using System.Collections.Generic; using BenchmarkDotNet.Attributes; using Moq; +using Robust.Shared.Exceptions; using Robust.Shared.GameObjects; using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.Interfaces.Log; +using Robust.Shared.Interfaces.Reflection; using Robust.Shared.IoC; +using Robust.Shared.Log; namespace Content.Benchmarks { @@ -14,7 +18,17 @@ namespace Content.Benchmarks private IComponentManager _componentManager; - [Params(500, 1000, 5000)] public int N { get; set; } + [Params(5000)] public int N { get; set; } + + public static void TestRun() + { + var x = new ComponentManagerGetAllComponents + { + N = 500 + }; + x.Setup(); + x.Run(); + } [GlobalSetup] public void Setup() @@ -23,6 +37,13 @@ namespace Content.Benchmarks IoCManager.InitThread(); IoCManager.Register(); + IoCManager.Register(); + IoCManager.Register(); + IoCManager.Register(); + IoCManager.Register(); + var entityManager = new Mock().Object; + IoCManager.RegisterInstance(entityManager); + IoCManager.RegisterInstance(new Mock().Object); var dummyReg = new Mock(); dummyReg.SetupGet(p => p.Name).Returns("Dummy"); @@ -34,17 +55,19 @@ namespace Content.Benchmarks var componentFactory = new Mock(); componentFactory.Setup(p => p.GetComponent()).Returns(new DummyComponent()); componentFactory.Setup(p => p.GetRegistration(It.IsAny())).Returns(dummyReg.Object); + componentFactory.Setup(p => p.GetAllRefTypes()).Returns(new[] {typeof(DummyComponent)}); IoCManager.RegisterInstance(componentFactory.Object); IoCManager.BuildGraph(); - _componentManager = IoCManager.Resolve(); + _componentManager.Initialize(); // Initialize N entities with one component. for (var i = 0; i < N; i++) { var entity = new Entity(); + entity.SetManagers(entityManager); entity.SetUid(new EntityUid(i + 1)); _entities.Add(entity); @@ -65,6 +88,16 @@ namespace Content.Benchmarks return count; } + [Benchmark] + public int Noop() + { + var count = 0; + + _componentManager.TryGetComponent(default, out DummyComponent _); + + return count; + } + private class DummyComponent : Component { public override string Name => "Dummy"; diff --git a/Content.Benchmarks/Program.cs b/Content.Benchmarks/Program.cs index 7eb128092d..74ee4e265b 100644 --- a/Content.Benchmarks/Program.cs +++ b/Content.Benchmarks/Program.cs @@ -6,7 +6,8 @@ namespace Content.Benchmarks { public static void Main(string[] args) { - BenchmarkRunner.Run(); + BenchmarkRunner.Run(); + //ComponentManagerGetAllComponents.TestRun(); } } } diff --git a/Content.IntegrationTests/Tests/EntityTest.cs b/Content.IntegrationTests/Tests/EntityTest.cs index cab5f7b68c..288105ab42 100644 --- a/Content.IntegrationTests/Tests/EntityTest.cs +++ b/Content.IntegrationTests/Tests/EntityTest.cs @@ -69,7 +69,8 @@ namespace Content.IntegrationTests.Tests catch (Exception e) { Logger.LogS(LogLevel.Error, "EntityTest", "Entity '" + prototype.ID + "' threw: " + e.Message); - Assert.Fail(); + //Assert.Fail(); + throw; } } }); diff --git a/Content.IntegrationTests/Tests/SaveLoadSaveTest.cs b/Content.IntegrationTests/Tests/SaveLoadSaveTest.cs index 67c5df2ed3..d462beb279 100644 --- a/Content.IntegrationTests/Tests/SaveLoadSaveTest.cs +++ b/Content.IntegrationTests/Tests/SaveLoadSaveTest.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Linq; using System.Threading.Tasks; using NUnit.Framework; using Robust.Server.Interfaces.Maps; @@ -37,19 +38,34 @@ namespace Content.IntegrationTests.Tests string one; string two; - using (var stream = userData.Open(new ResourcePath("save load save 1.yml"), FileMode.Open)) + var rp1 = new ResourcePath("save load save 1.yml"); + using (var stream = userData.Open(rp1, FileMode.Open)) using (var reader = new StreamReader(stream)) { one = reader.ReadToEnd(); } - using (var stream = userData.Open(new ResourcePath("save load save 2.yml"), FileMode.Open)) + var rp2 = new ResourcePath("save load save 2.yml"); + using (var stream = userData.Open(rp2, FileMode.Open)) using (var reader = new StreamReader(stream)) { two = reader.ReadToEnd(); } - Assert.That(one, Is.EqualTo(two)); + Assert.Multiple(() => { + Assert.That(one, Is.EqualTo(two)); + var failed = TestContext.CurrentContext.Result.Assertions.FirstOrDefault(); + if (failed != null) + { + var path1 = Path.Combine(userData.RootDir!,rp1.ToRelativeSystemPath()); + var path2 = Path.Combine(userData.RootDir!,rp2.ToRelativeSystemPath()); + TestContext.AddTestAttachment(path1); + TestContext.AddTestAttachment(path2); + TestContext.Error.WriteLine("Complete output:"); + TestContext.Error.WriteLine(path1); + TestContext.Error.WriteLine(path2); + } + }); } /// diff --git a/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs b/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs index 27bbc2c942..357a8acd72 100644 --- a/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs +++ b/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs @@ -60,7 +60,7 @@ namespace Content.Server.GameObjects base.ExposeData(serializer); serializer.DataField(ref StorageCapacityMax, "Capacity", 10000); - serializer.DataField(ref StorageUsed, "used", 0); + //serializer.DataField(ref StorageUsed, "used", 0); } /// @@ -348,7 +348,7 @@ namespace Content.Server.GameObjects foreach (var entity in storage.ContainedEntities) { - var item = entity.GetComponent(); + var item = entity.GetComponent(); StorageUsed += item.ObjectSize; } diff --git a/Content.Server/GameObjects/Components/Movement/AiControllerComponent.cs b/Content.Server/GameObjects/Components/Movement/AiControllerComponent.cs index 884e03479d..6a6747c6bb 100644 --- a/Content.Server/GameObjects/Components/Movement/AiControllerComponent.cs +++ b/Content.Server/GameObjects/Components/Movement/AiControllerComponent.cs @@ -61,7 +61,7 @@ namespace Content.Server.GameObjects.Components.Movement protected override void Shutdown() { base.Shutdown(); - Processor.Shutdown(); + Processor?.Shutdown(); } /// From 27aefeb35da622bb148ee79a665669f50076157e Mon Sep 17 00:00:00 2001 From: Tyler Young Date: Mon, 22 Jun 2020 21:02:50 -0400 Subject: [PATCH 2/2] fis crap w/ grids being deleted while component events still ref --- .../GameObjects/EntitySystems/IconSmoothSystem.cs | 5 ++--- .../GameObjects/EntitySystems/SubFloorHideSystem.cs | 6 +++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Content.Client/GameObjects/EntitySystems/IconSmoothSystem.cs b/Content.Client/GameObjects/EntitySystems/IconSmoothSystem.cs index 9cd35a72f4..901178e9ec 100644 --- a/Content.Client/GameObjects/EntitySystems/IconSmoothSystem.cs +++ b/Content.Client/GameObjects/EntitySystems/IconSmoothSystem.cs @@ -79,10 +79,9 @@ namespace Content.Client.GameObjects.EntitySystems } } - if (ev.LastPosition.HasValue) + // Entity is no longer valid, update around the last position it was at. + if (ev.LastPosition.HasValue && _mapManager.TryGetGrid(ev.LastPosition.Value.grid, out var grid)) { - // Entity is no longer valid, update around the last position it was at. - var grid = _mapManager.GetGrid(ev.LastPosition.Value.grid); var pos = ev.LastPosition.Value.pos; AddValidEntities(grid.GetSnapGridCell(pos + new MapIndices(1, 0), ev.Offset)); diff --git a/Content.Client/GameObjects/EntitySystems/SubFloorHideSystem.cs b/Content.Client/GameObjects/EntitySystems/SubFloorHideSystem.cs index 0551de45de..9a51fb4d8e 100644 --- a/Content.Client/GameObjects/EntitySystems/SubFloorHideSystem.cs +++ b/Content.Client/GameObjects/EntitySystems/SubFloorHideSystem.cs @@ -60,7 +60,11 @@ namespace Content.Client.GameObjects.EntitySystems private void HandleDirtyEvent(SubFloorHideDirtyEvent ev) { - var grid = _mapManager.GetGrid(ev.Sender.Transform.GridID); + if (!_mapManager.TryGetGrid(ev.Sender.Transform.GridID, out var grid)) + { + return; + } + var indices = grid.WorldToTile(ev.Sender.Transform.WorldPosition); UpdateTile(grid, indices); }