-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Setup basic benchmarking project. * Avoid unnecessary lock when singleton already constructed * Avoid unneeded reflection calls when resolving already instantiated singleton as dependency. * Change .Count() to .Length where possible, avoid repeat calls to GetParameters * Cache 'best constructor' per type. Provides significant performance improvement for resolving multi-instance registrations. * Do not auto-register types that are nested *and* private. * Apply categories and baselines to benchmarks to improve result readability. * Cahce generic types when resolving open generics. * Cache lazy automatic factories (per container) for significant perf improvement when resolving multiple times.
- Loading branch information
Showing
8 changed files
with
4,920 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,4 @@ src/.vs/TinyIoC/DesignTimeBuild/.dtbcache.v2 | |
.DS_Store | ||
|
||
artifacts/ | ||
/.vs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace TinyIoc.Benchmarks | ||
{ | ||
|
||
#if NET48 //These work on either platform but no point running them twice | ||
[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.Net48)] | ||
[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.Net461)] | ||
#endif | ||
#if NETCOREAPP3_1_OR_GREATER // These don't seem to work in a FX app | ||
[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.NetCoreApp31)] | ||
[RyuJitX86Job] | ||
#endif | ||
#if NET48 //These don't seem to work in a .net core app | ||
[RyuJitX64Job] | ||
[MonoJob] | ||
#endif | ||
[MemoryDiagnoser] | ||
public class AutoRegisterBenchmarks | ||
{ | ||
|
||
[BenchmarkCategory("AutoResolve"), Benchmark(Baseline = true)] | ||
public TinyIoC.Original.TinyIoCContainer Original_AutoRegister() | ||
{ | ||
var retVal = new TinyIoC.Original.TinyIoCContainer(); | ||
retVal.AutoRegister(); | ||
return retVal; | ||
} | ||
|
||
[BenchmarkCategory("AutoResolve"), Benchmark] | ||
public TinyIoC.TinyIoCContainer New_AutoRegister() | ||
{ | ||
var retVal = new TinyIoC.TinyIoCContainer(); | ||
retVal.AutoRegister(); | ||
return retVal; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace TinyIoc.Benchmarks | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace TinyIoc.Benchmarks | ||
{ | ||
|
||
#if NET48 //These work on either platform but no point running them twice | ||
[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.Net48)] | ||
[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.Net461)] | ||
#endif | ||
#if NETCOREAPP3_1_OR_GREATER // These don't seem to work in a FX app | ||
[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.NetCoreApp31)] | ||
[RyuJitX86Job] | ||
#endif | ||
#if NET48 //These don't seem to work in a .net core app | ||
[RyuJitX64Job] | ||
[MonoJob] | ||
#endif | ||
[MemoryDiagnoser] | ||
[GroupBenchmarksBy(BenchmarkDotNet.Configs.BenchmarkLogicalGroupRule.ByCategory)] | ||
//[CategoriesColumn] | ||
public class ResolveBenchmarks | ||
{ | ||
private TinyIoC.TinyIoCContainer _NewContainer; | ||
private TinyIoC.Original.TinyIoCContainer _OriginalContainer; | ||
|
||
[BenchmarkDotNet.Attributes.GlobalSetup] | ||
public void InitContainer() | ||
{ | ||
_NewContainer = new TinyIoC.TinyIoCContainer(); | ||
_NewContainer.Register<ISingleton, Singleton>().AsSingleton(); | ||
_NewContainer.Register<IParameterlessInstance, ParameterlessInstance>().AsMultiInstance(); | ||
_NewContainer.Register<ISingleParameterInstance, SingleParameterInstance>().AsMultiInstance(); | ||
_NewContainer.Register(typeof(IOpenGeneric<>), typeof(OpenGenericInstance<>)).AsMultiInstance(); | ||
|
||
_OriginalContainer = new TinyIoC.Original.TinyIoCContainer(); | ||
_OriginalContainer.Register<ISingleton, Singleton>().AsSingleton(); | ||
_OriginalContainer.Register<IParameterlessInstance, ParameterlessInstance>().AsMultiInstance(); | ||
_OriginalContainer.Register<ISingleParameterInstance, SingleParameterInstance>().AsMultiInstance(); | ||
_OriginalContainer.Register(typeof(IOpenGeneric<>), typeof(OpenGenericInstance<>)).AsMultiInstance(); | ||
} | ||
|
||
[BenchmarkCategory("ResolveSingleton"), Benchmark(Baseline = true)] | ||
public ISingleton Original_Resolve_Singleton() | ||
{ | ||
return _OriginalContainer.Resolve<ISingleton>(); | ||
} | ||
|
||
[BenchmarkCategory("ResolveSingleton"), Benchmark] | ||
public ISingleton New_Resolve_Singleton() | ||
{ | ||
return _NewContainer.Resolve<ISingleton>(); | ||
} | ||
|
||
|
||
[BenchmarkCategory("ResolveInstance"), Benchmark(Baseline = true)] | ||
public IParameterlessInstance Original_Resolve_Instance_Without_Dependencies() | ||
{ | ||
return _OriginalContainer.Resolve<IParameterlessInstance>(); | ||
} | ||
|
||
[BenchmarkCategory("ResolveInstance"), Benchmark] | ||
public IParameterlessInstance New_Resolve_Instance_Without_Dependencies() | ||
{ | ||
return _NewContainer.Resolve<IParameterlessInstance>(); | ||
} | ||
|
||
|
||
[BenchmarkCategory("ResolveInstanceWithSingletonDependency"), Benchmark(Baseline = true)] | ||
public ISingleParameterInstance Original_Resolve_Instance_With_Singleton_Dependency() | ||
{ | ||
return _OriginalContainer.Resolve<ISingleParameterInstance>(); | ||
} | ||
|
||
[BenchmarkCategory("ResolveInstanceWithSingletonDependency"), Benchmark] | ||
public ISingleParameterInstance New_Resolve_Instance_With_Singleton_Dependency() | ||
{ | ||
return _NewContainer.Resolve<ISingleParameterInstance>(); | ||
} | ||
|
||
|
||
[BenchmarkCategory("ResolveOpenGeneric"), Benchmark(Baseline = true)] | ||
public IOpenGeneric<string> Original_Resolve_OpenGeneric() | ||
{ | ||
return _OriginalContainer.Resolve<IOpenGeneric<string>>(); | ||
} | ||
|
||
[BenchmarkCategory("ResolveOpenGeneric"), Benchmark] | ||
public IOpenGeneric<string> New_Resolve_OpenGeneric() | ||
{ | ||
return _NewContainer.Resolve<IOpenGeneric<string>>(); | ||
} | ||
|
||
|
||
[BenchmarkCategory("ResolveUnregistered"), Benchmark(Baseline = true)] | ||
public UnregisteredInstance Original_Resolve_Unregistered() | ||
{ | ||
return _OriginalContainer.Resolve<UnregisteredInstance>(); | ||
} | ||
|
||
[BenchmarkCategory("ResolveUnregistered"), Benchmark] | ||
public UnregisteredInstance New_Resolve_Unregistered() | ||
{ | ||
return _NewContainer.Resolve<UnregisteredInstance>(); | ||
} | ||
|
||
|
||
[BenchmarkCategory("AutomaticFuncFactory"), Benchmark(Baseline = true)] | ||
public Func<ParameterlessInstance> Original_Resolve_AutomaticFuncFactory() | ||
{ | ||
return _OriginalContainer.Resolve<Func<ParameterlessInstance>>(); | ||
} | ||
|
||
[BenchmarkCategory("AutomaticFuncFactory"), Benchmark] | ||
public Func<ParameterlessInstance> New_Resolve_AutomaticFuncFactory() | ||
{ | ||
return _NewContainer.Resolve<Func<ParameterlessInstance>>(); | ||
} | ||
} | ||
|
||
public interface ISingleton | ||
{ | ||
|
||
} | ||
|
||
public class Singleton : ISingleton | ||
{ | ||
public Singleton() | ||
{ | ||
|
||
} | ||
} | ||
|
||
public interface IParameterlessInstance { } | ||
|
||
public class ParameterlessInstance : IParameterlessInstance { } | ||
|
||
public class UnregisteredInstance { public UnregisteredInstance(ISingleton singleton) {} } | ||
|
||
public interface ISingleParameterInstance { } | ||
|
||
public class SingleParameterInstance : ISingleParameterInstance | ||
{ | ||
private readonly ISingleton _Singleton; | ||
public SingleParameterInstance(ISingleton singleton) | ||
{ | ||
_Singleton = singleton; | ||
} | ||
} | ||
|
||
public interface IOpenGeneric<T> { T Value { get; set; } } | ||
public class OpenGenericInstance<T> : IOpenGeneric<T> | ||
{ | ||
public T Value { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>netcoreapp3.1;net48;net461</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="bin\Release\net48\BenchmarkDotNet.Artifacts\results\TinyIoc.Benchmarks.ResolveBenchmarks-report.html" /> | ||
<Content Include="bin\Release\netcoreapp3.1\BenchmarkDotNet.Artifacts\results\TinyIoc.Benchmarks.ResolveBenchmarks-report.html" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="bin\Release\net48\BenchmarkDotNet.Artifacts\results\TinyIoc.Benchmarks.ResolveBenchmarks-report-github.md" /> | ||
<None Include="bin\Release\net48\BenchmarkDotNet.Artifacts\results\TinyIoc.Benchmarks.ResolveBenchmarks-report.csv" /> | ||
<None Include="bin\Release\netcoreapp3.1\BenchmarkDotNet.Artifacts\results\TinyIoc.Benchmarks.ResolveBenchmarks-report-github.md" /> | ||
<None Include="bin\Release\netcoreapp3.1\BenchmarkDotNet.Artifacts\results\TinyIoc.Benchmarks.ResolveBenchmarks-report.csv" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\TinyIoC\TinyIoC.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.