From c61ba5119ef06977cbe9975c799a7375a28e3266 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 14 May 2020 18:10:34 -0600 Subject: [PATCH] Add more resolution and some additional IoC features --- .../IoCPerformance.Android.csproj | 24 ++++------- .../Properties/AndroidManifest.xml | 8 ++-- .../IoCPerformance.UWP.csproj | 11 +++-- .../IoCPerformance.iOS.csproj | 11 +++-- .../IoCPerformance/IoC/AutofacPerformance.cs | 19 +++++---- .../IoCPerformance/IoC/Base/IPerformance.cs | 2 + .../IoCPerformance/IoC/DependencyResolver.cs | 39 ++++++++++++++++++ .../IoC/NetCoreServiceProvider.cs | 41 +++++++++++++++++++ .../IoCPerformance/IoC/SplatPerformance.cs | 9 +++- .../IoCPerformance/IoC/TinyIocPerformance.cs | 9 +++- .../IoCPerformance/IoC/UnityPerformance.cs | 9 +++- .../IoCPerformance/IoCPerformance.csproj | 11 ++--- .../ViewModels/MainViewModel.cs | 23 +++++++++-- 13 files changed, 167 insertions(+), 49 deletions(-) create mode 100644 src/IoC/IoCPerformance/IoCPerformance/IoC/DependencyResolver.cs create mode 100644 src/IoC/IoCPerformance/IoCPerformance/IoC/NetCoreServiceProvider.cs diff --git a/src/IoC/IoCPerformance/IoCPerformance.Android/IoCPerformance.Android.csproj b/src/IoC/IoCPerformance/IoCPerformance.Android/IoCPerformance.Android.csproj index 779f1fa..abb525b 100644 --- a/src/IoC/IoCPerformance/IoCPerformance.Android/IoCPerformance.Android.csproj +++ b/src/IoC/IoCPerformance/IoCPerformance.Android/IoCPerformance.Android.csproj @@ -16,7 +16,7 @@ Resources Assets false - v8.1 + v10.0 Xamarin.Android.Net.AndroidClientHandler @@ -50,10 +50,13 @@ - 4.9.2 + 5.2.0 + + + 3.1.4 - 6.2.8 + 6.2.10 1.3.0 @@ -61,20 +64,9 @@ all - 5.10.3 + 5.11.6 - - 28.0.0.1 - - - 28.0.0.1 - - - - - - - + diff --git a/src/IoC/IoCPerformance/IoCPerformance.Android/Properties/AndroidManifest.xml b/src/IoC/IoCPerformance/IoCPerformance.Android/Properties/AndroidManifest.xml index eed3619..22de6a8 100644 --- a/src/IoC/IoCPerformance/IoCPerformance.Android/Properties/AndroidManifest.xml +++ b/src/IoC/IoCPerformance/IoCPerformance.Android/Properties/AndroidManifest.xml @@ -1,5 +1,5 @@ - + - - - + + + \ No newline at end of file diff --git a/src/IoC/IoCPerformance/IoCPerformance.UWP/IoCPerformance.UWP.csproj b/src/IoC/IoCPerformance/IoCPerformance.UWP/IoCPerformance.UWP.csproj index 15c9245..385bd42 100644 --- a/src/IoC/IoCPerformance/IoCPerformance.UWP/IoCPerformance.UWP.csproj +++ b/src/IoC/IoCPerformance/IoCPerformance.UWP/IoCPerformance.UWP.csproj @@ -144,7 +144,10 @@ - 4.9.2 + 5.2.0 + + + 3.1.4 1.3.0 @@ -152,10 +155,10 @@ all - 5.10.3 + 5.11.6 - - + + diff --git a/src/IoC/IoCPerformance/IoCPerformance.iOS/IoCPerformance.iOS.csproj b/src/IoC/IoCPerformance/IoCPerformance.iOS/IoCPerformance.iOS.csproj index e5b298a..653af94 100644 --- a/src/IoC/IoCPerformance/IoCPerformance.iOS/IoCPerformance.iOS.csproj +++ b/src/IoC/IoCPerformance/IoCPerformance.iOS/IoCPerformance.iOS.csproj @@ -149,10 +149,13 @@ - 4.9.2 + 5.2.0 + + + 3.1.4 - 6.2.8 + 6.2.10 1.3.0 @@ -160,9 +163,9 @@ all - 5.10.3 + 5.11.6 - + diff --git a/src/IoC/IoCPerformance/IoCPerformance/IoC/AutofacPerformance.cs b/src/IoC/IoCPerformance/IoCPerformance/IoC/AutofacPerformance.cs index 967f05d..fb33282 100644 --- a/src/IoC/IoCPerformance/IoCPerformance/IoC/AutofacPerformance.cs +++ b/src/IoC/IoCPerformance/IoCPerformance/IoC/AutofacPerformance.cs @@ -11,6 +11,8 @@ namespace IoCPerformance.IoC private static ContainerBuilder _builder { get; set; } private static IContainer _container { get; set; } + ILifetimeScope _scope; + public AutofacPerformance(int numberOfTests) { _numberOfTests = numberOfTests; @@ -20,22 +22,25 @@ namespace IoCPerformance.IoC public void Registration() { - for (int x = 0; x < _numberOfTests; x++) + for (int x = 0; x < _numberOfTests + 1; x++) { _builder.RegisterType().As().Named(string.Format("Class{0}", x)); } _container = _builder.Build(); + _scope = _container.BeginLifetimeScope(); + } + + public void FirstResolve() + { + _scope.ResolveNamed(string.Format("Class{0}", "0")); } public void Resolve() - { - using (var scope = _container.BeginLifetimeScope()) + { + for (int x = 1; x < _numberOfTests + 1; x++) { - for (int x = 0; x < _numberOfTests; x++) - { - scope.ResolveNamed(string.Format("Class{0}", x)); - } + _scope.ResolveNamed(string.Format("Class{0}", x)); } } } diff --git a/src/IoC/IoCPerformance/IoCPerformance/IoC/Base/IPerformance.cs b/src/IoC/IoCPerformance/IoCPerformance/IoC/Base/IPerformance.cs index 130ccfc..60112a0 100644 --- a/src/IoC/IoCPerformance/IoCPerformance/IoC/Base/IPerformance.cs +++ b/src/IoC/IoCPerformance/IoCPerformance/IoC/Base/IPerformance.cs @@ -5,5 +5,7 @@ void Registration(); void Resolve(); + + void FirstResolve(); } } \ No newline at end of file diff --git a/src/IoC/IoCPerformance/IoCPerformance/IoC/DependencyResolver.cs b/src/IoC/IoCPerformance/IoCPerformance/IoC/DependencyResolver.cs new file mode 100644 index 0000000..4453059 --- /dev/null +++ b/src/IoC/IoCPerformance/IoCPerformance/IoC/DependencyResolver.cs @@ -0,0 +1,39 @@ +using IoCPerformance.IoC.Base; +using IoCPerformance.Services.Test; +using Unity; +using Xamarin.Forms; + +namespace IoCPerformance.IoC +{ + public class DependencyResolver : IPerformance + { + private readonly int _numberOfTests; + + + public DependencyResolver(int numberOfTests) + { + _numberOfTests = numberOfTests; + } + + public void FirstResolve() + { + DependencyService.Get(DependencyFetchTarget.NewInstance); + } + + public void Registration() + { + for (int x = 0; x < _numberOfTests; x++) + { + DependencyService.Register(); + } + } + + public void Resolve() + { + for (int x = 0; x < _numberOfTests; x++) + { + var returnMe = DependencyService.Get(DependencyFetchTarget.NewInstance); + } + } + } +} \ No newline at end of file diff --git a/src/IoC/IoCPerformance/IoCPerformance/IoC/NetCoreServiceProvider.cs b/src/IoC/IoCPerformance/IoCPerformance/IoC/NetCoreServiceProvider.cs new file mode 100644 index 0000000..9e574bf --- /dev/null +++ b/src/IoC/IoCPerformance/IoCPerformance/IoC/NetCoreServiceProvider.cs @@ -0,0 +1,41 @@ +using IoCPerformance.IoC.Base; +using IoCPerformance.Services.Test; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace IoCPerformance.IoC +{ + public class NetCoreServiceProvider : IPerformance + { + private int _numberOfTests; + IServiceProvider _serviceProvider; + + public NetCoreServiceProvider(int numberOfTests) + { + _numberOfTests = numberOfTests; + } + + public void Registration() + { + for (int x = 0; x < _numberOfTests; x++) + { + ServiceCollection serviceCollection = new ServiceCollection(); + serviceCollection.AddTransient(); + _serviceProvider = serviceCollection.BuildServiceProvider(); + } + } + + public void FirstResolve() + { + var returnMe = _serviceProvider.GetService(); + } + + public void Resolve() + { + for (int x = 0; x < _numberOfTests; x++) + { + var returnMe = _serviceProvider.GetService(); + } + } + } +} diff --git a/src/IoC/IoCPerformance/IoCPerformance/IoC/SplatPerformance.cs b/src/IoC/IoCPerformance/IoCPerformance/IoC/SplatPerformance.cs index b5fe595..408c842 100644 --- a/src/IoC/IoCPerformance/IoCPerformance/IoC/SplatPerformance.cs +++ b/src/IoC/IoCPerformance/IoCPerformance/IoC/SplatPerformance.cs @@ -21,15 +21,20 @@ namespace IoCPerformance.IoC public void Registration() { - for (int i = 0; i < _numberOfTests; i++) + for (int i = 0; i < _numberOfTests + 1; i++) { _mutableContainer.Register(() => new TestService(), contract: $"Class{i}"); } } + public void FirstResolve() + { + _container.GetService($"Class0"); + } + public void Resolve() { - for (int i = 0; i < _numberOfTests; i++) + for (int i = 1; i < _numberOfTests + 1; i++) { _container.GetService($"Class{i}"); } diff --git a/src/IoC/IoCPerformance/IoCPerformance/IoC/TinyIocPerformance.cs b/src/IoC/IoCPerformance/IoCPerformance/IoC/TinyIocPerformance.cs index 88164f2..b71aa5f 100644 --- a/src/IoC/IoCPerformance/IoCPerformance/IoC/TinyIocPerformance.cs +++ b/src/IoC/IoCPerformance/IoCPerformance/IoC/TinyIocPerformance.cs @@ -18,15 +18,20 @@ namespace IoCPerformance.IoC public void Registration() { - for (int x = 0; x < _numberOfTests; x++) + for (int x = 0; x < _numberOfTests + 1; x++) { _container.Register(string.Format("Class{0}", x)); } } + public void FirstResolve() + { + _container.Resolve(string.Format("Class{0}", "0")); + } + public void Resolve() { - for (int x = 0; x < _numberOfTests; x++) + for (int x = 1; x < _numberOfTests + 1; x++) { _container.Resolve(string.Format("Class{0}", x)); } diff --git a/src/IoC/IoCPerformance/IoCPerformance/IoC/UnityPerformance.cs b/src/IoC/IoCPerformance/IoCPerformance/IoC/UnityPerformance.cs index a433055..747cc67 100644 --- a/src/IoC/IoCPerformance/IoCPerformance/IoC/UnityPerformance.cs +++ b/src/IoC/IoCPerformance/IoCPerformance/IoC/UnityPerformance.cs @@ -18,15 +18,20 @@ namespace IoCPerformance.IoC public void Registration() { - for (int x = 0; x < _numberOfTests; x++) + for (int x = 0; x < _numberOfTests + 1; x++) { _unityContainer.RegisterType(string.Format("Class{0}", x)); } } + public void FirstResolve() + { + _unityContainer.Resolve(string.Format("Class{0}", "0")); + } + public void Resolve() { - for (int x = 0; x < _numberOfTests; x++) + for (int x = 1; x < _numberOfTests + 1; x++) { _unityContainer.Resolve(string.Format("Class{0}", x)); } diff --git a/src/IoC/IoCPerformance/IoCPerformance/IoCPerformance.csproj b/src/IoC/IoCPerformance/IoCPerformance/IoCPerformance.csproj index 798151e..e4feac1 100644 --- a/src/IoC/IoCPerformance/IoCPerformance/IoCPerformance.csproj +++ b/src/IoC/IoCPerformance/IoCPerformance/IoCPerformance.csproj @@ -10,15 +10,16 @@ - - - + + + + all runtime; build; native; contentfiles; analyzers - - + + diff --git a/src/IoC/IoCPerformance/IoCPerformance/ViewModels/MainViewModel.cs b/src/IoC/IoCPerformance/IoCPerformance/ViewModels/MainViewModel.cs index 199facf..9580d04 100644 --- a/src/IoC/IoCPerformance/IoCPerformance/ViewModels/MainViewModel.cs +++ b/src/IoC/IoCPerformance/IoCPerformance/ViewModels/MainViewModel.cs @@ -28,22 +28,31 @@ namespace IoCPerformance.ViewModels private void ExecuteTest() { - NumberOfTests = 25; + NumberOfTests = 5000; _watch = new Stopwatch(); AutofacPerformance autofacPerformanceTest = new AutofacPerformance(NumberOfTests); TinyIocPerformance tinyIocPerformanceTest = new TinyIocPerformance(NumberOfTests); UnityPerformance unityPerformanceTest = new UnityPerformance(NumberOfTests); SplatPerformance splatPerformance = new SplatPerformance(NumberOfTests); + DependencyResolver dependencyResolver = new DependencyResolver(NumberOfTests); + NetCoreServiceProvider netCoreServiceProvider = new NetCoreServiceProvider(NumberOfTests); RunTests(autofacPerformanceTest, "AutoFac"); RunTests(tinyIocPerformanceTest, "TinyIoC"); RunTests(unityPerformanceTest, "Unity"); RunTests(splatPerformance, "Splat"); + RunTests(dependencyResolver, "Xamarin.Forms Dependency Resolver"); + RunTests(netCoreServiceProvider, "Microsoft.Net Dependency Injection"); } private void RunTests(IPerformance performanceTest, string testName) { + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + GC.WaitForPendingFinalizers(); + try { Output += string.Format("Starting performance test for {0}", testName) + Environment.NewLine; @@ -52,13 +61,21 @@ namespace IoCPerformance.ViewModels performanceTest.Registration(); - Output += string.Format("IOC Registration Time for {0} tests: {1} ms", NumberOfTests, _watch.Elapsed.Milliseconds.ToString()) + Environment.NewLine; + Output += string.Format("Avg. IOC Registration Time for {0} tests: {1} ms", NumberOfTests, ((double)_watch.Elapsed.Milliseconds / (double)NumberOfTests).ToString()) + Environment.NewLine; + + _watch.Restart(); + + performanceTest.FirstResolve(); + + Output += string.Format("IOC First Resolution Time: {0} ms", _watch.Elapsed.Milliseconds.ToString()) + Environment.NewLine; _watch.Restart(); performanceTest.Resolve(); - Output += string.Format("IOC Resolution Time for {0} tests: {1} ms", NumberOfTests, _watch.Elapsed.Milliseconds.ToString()) + Environment.NewLine; + + Output += string.Format("Total IOC Resolution Time for {0} tests: {1} ms", NumberOfTests, _watch.Elapsed.Milliseconds) + Environment.NewLine; + Output += string.Format("Avg. IOC Resolution Time for {0} tests: {1} ms", NumberOfTests, ((double)_watch.Elapsed.Milliseconds / (double)NumberOfTests).ToString()) + Environment.NewLine; } finally {