From dd13d969b2dcd69182aa97e44742f52126141219 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Sun, 2 Jun 2019 01:17:07 +0200 Subject: [PATCH] Benchmarks project along with a component manager benchmark. (#251) --- .../ComponentManagerGetAllComponents.cs | 73 +++++++++++++++++++ Content.Benchmarks/Content.Benchmarks.csproj | 25 +++++++ Content.Benchmarks/Program.cs | 13 ++++ SpaceStation14.sln | 10 +++ 4 files changed, 121 insertions(+) create mode 100644 Content.Benchmarks/ComponentManagerGetAllComponents.cs create mode 100644 Content.Benchmarks/Content.Benchmarks.csproj create mode 100644 Content.Benchmarks/Program.cs diff --git a/Content.Benchmarks/ComponentManagerGetAllComponents.cs b/Content.Benchmarks/ComponentManagerGetAllComponents.cs new file mode 100644 index 0000000000..638c19e99d --- /dev/null +++ b/Content.Benchmarks/ComponentManagerGetAllComponents.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using BenchmarkDotNet.Attributes; +using Moq; +using Robust.Shared.GameObjects; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.IoC; + +namespace Content.Benchmarks +{ + public class ComponentManagerGetAllComponents + { + private readonly List _entities = new List(); + + private IComponentManager _componentManager; + + [Params(500, 1000, 5000)] public int N { get; set; } + + [GlobalSetup] + public void Setup() + { + // Initialize component manager. + IoCManager.InitThread(); + + IoCManager.Register(); + + var dummyReg = new Mock(); + dummyReg.SetupGet(p => p.Name).Returns("Dummy"); + dummyReg.SetupGet(p => p.Type).Returns(typeof(DummyComponent)); + dummyReg.SetupGet(p => p.NetID).Returns((uint?) null); + dummyReg.SetupGet(p => p.NetworkSynchronizeExistence).Returns(false); + dummyReg.SetupGet(p => p.References).Returns(new Type[] {typeof(DummyComponent)}); + + var componentFactory = new Mock(); + componentFactory.Setup(p => p.GetComponent()).Returns(new DummyComponent()); + componentFactory.Setup(p => p.GetRegistration(It.IsAny())).Returns(dummyReg.Object); + + IoCManager.RegisterInstance(componentFactory.Object); + + IoCManager.BuildGraph(); + + _componentManager = IoCManager.Resolve(); + + // Initialize N entities with one component. + for (var i = 0; i < N; i++) + { + var entity = new Entity(); + entity.SetUid(new EntityUid(i + 1)); + _entities.Add(entity); + + _componentManager.AddComponent(entity); + } + } + + [Benchmark] + public int Run() + { + var count = 0; + + foreach (var _ in _componentManager.GetAllComponents()) + { + count += 1; + } + + return count; + } + + private class DummyComponent : Component + { + public override string Name => "Dummy"; + } + } +} diff --git a/Content.Benchmarks/Content.Benchmarks.csproj b/Content.Benchmarks/Content.Benchmarks.csproj new file mode 100644 index 0000000000..e913b4af8d --- /dev/null +++ b/Content.Benchmarks/Content.Benchmarks.csproj @@ -0,0 +1,25 @@ + + + net472 + ..\bin\Content.Benchmarks\ + false + false + x86;x64 + Exe + + + + + + + + + + + + + + + + + diff --git a/Content.Benchmarks/Program.cs b/Content.Benchmarks/Program.cs new file mode 100644 index 0000000000..b794297830 --- /dev/null +++ b/Content.Benchmarks/Program.cs @@ -0,0 +1,13 @@ +using BenchmarkDotNet.Configs; +using BenchmarkDotNet.Running; + +namespace Content.Benchmarks +{ + internal class Program + { + public static void Main(string[] args) + { + BenchmarkRunner.Run(); + } + } +} diff --git a/SpaceStation14.sln b/SpaceStation14.sln index c431f1fdac..878f0c3e6d 100644 --- a/SpaceStation14.sln +++ b/SpaceStation14.sln @@ -38,6 +38,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robust.Shared", "RobustTool EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Content.IntegrationTests", "Content.IntegrationTests\Content.IntegrationTests.csproj", "{AB7AF1C8-30FF-4436-9DF0-179BE5B0C132}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Content.Benchmarks", "Content.Benchmarks\Content.Benchmarks.csproj", "{7AC832A1-2461-4EB5-AC26-26F6AFFA9E46}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -141,6 +143,14 @@ Global {F0ADA779-40B8-4F7E-BA6C-CDB19F3065D9}.Release|x64.Build.0 = Release|x64 {F0ADA779-40B8-4F7E-BA6C-CDB19F3065D9}.Release|x86.ActiveCfg = Release|x86 {F0ADA779-40B8-4F7E-BA6C-CDB19F3065D9}.Release|x86.Build.0 = Release|x86 + {7AC832A1-2461-4EB5-AC26-26F6AFFA9E46}.Debug|x64.ActiveCfg = Debug|x64 + {7AC832A1-2461-4EB5-AC26-26F6AFFA9E46}.Debug|x64.Build.0 = Debug|x64 + {7AC832A1-2461-4EB5-AC26-26F6AFFA9E46}.Debug|x86.ActiveCfg = Debug|x86 + {7AC832A1-2461-4EB5-AC26-26F6AFFA9E46}.Debug|x86.Build.0 = Debug|x86 + {7AC832A1-2461-4EB5-AC26-26F6AFFA9E46}.Release|x64.ActiveCfg = Release|x64 + {7AC832A1-2461-4EB5-AC26-26F6AFFA9E46}.Release|x64.Build.0 = Release|x64 + {7AC832A1-2461-4EB5-AC26-26F6AFFA9E46}.Release|x86.ActiveCfg = Release|x86 + {7AC832A1-2461-4EB5-AC26-26F6AFFA9E46}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE