Skip to content

Commit

Permalink
Added benchmark for resolving from child scopes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillig committed May 24, 2019
1 parent a270512 commit ca70059
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 48 deletions.
65 changes: 65 additions & 0 deletions bench/Autofac.Benchmarks/ChildScopeResolveBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using BenchmarkDotNet.Attributes;

namespace Autofac.Benchmarks
{
public class ChildScopeResolveBenchmark
{
private IContainer _container;

[Benchmark]
public void Resolve()
{
using (var requestScope = this._container.BeginLifetimeScope("request", b => b.RegisterType<C1>()))
{
using (var unitOfWorkScope = requestScope.BeginLifetimeScope())
{
var instance = unitOfWorkScope.Resolve<A>();
GC.KeepAlive(instance);
}
}
}

[GlobalSetup]
public void Setup()
{
var builder = new ContainerBuilder();
builder.RegisterType<A>();
builder.RegisterType<B1>();
builder.RegisterType<B2>().InstancePerMatchingLifetimeScope("request");
builder.RegisterType<C2>().InstancePerLifetimeScope();
builder.RegisterType<D1>().SingleInstance();
builder.RegisterType<D2>().SingleInstance();
this._container = builder.Build();
}

internal class A
{
public A(B1 b1, B2 b2) { }
}

internal class B1
{
public B1(B2 b2, C1 c1, C2 c2) { }
}

internal class B2
{
public B2(C1 c1, C2 c2) { }
}

internal class C1
{
public C1(C2 c2, D1 d1, D2 d2) { }
}

internal class C2
{
public C2(D1 d1, D2 d2) { }
}

internal class D1 { }

internal class D2 { }
}
}
64 changes: 16 additions & 48 deletions bench/Autofac.Benchmarks/Harness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using Autofac.Benchmarks.Decorators;
using BenchmarkDotNet.Running;
using Xunit;

Expand All @@ -31,78 +32,45 @@ namespace Autofac.Benchmarks
public class Harness
{
[Fact]
public void RootContainerResolve()
{
BenchmarkRunner.Run<RootContainerResolveBenchmark>();
}
public void ChildScopeResolve() => BenchmarkRunner.Run<ChildScopeResolveBenchmark>();

[Fact]
public void DeepGraphResolve()
{
BenchmarkRunner.Run<DeepGraphResolveBenchmark>();
}
public void Concurrency() => BenchmarkRunner.Run<ConcurrencyBenchmark>();

[Fact]
public void Concurrency()
{
BenchmarkRunner.Run<ConcurrencyBenchmark>();
}
public void Decorator_Keyed_Generic() => BenchmarkRunner.Run<KeyedGenericBenchmark>();

[Fact]
public void Decorator_Keyed_Generic()
{
BenchmarkRunner.Run<Decorators.KeyedGenericBenchmark>();
}
public void Decorator_Keyed_Nested() => BenchmarkRunner.Run<KeyedNestedBenchmark>();

[Fact]
public void Decorator_Keyed_Nested()
{
BenchmarkRunner.Run<Decorators.KeyedNestedBenchmark>();
}
public void Decorator_Keyed_Simple() => BenchmarkRunner.Run<KeyedSimpleBenchmark>();

[Fact]
public void Decorator_Keyed_Simple()
{
BenchmarkRunner.Run<Decorators.KeyedSimpleBenchmark>();
}
public void Decorator_Keyless_Generic() => BenchmarkRunner.Run<KeylessGenericBenchmark>();

[Fact]
public void Decorator_Keyless_Generic()
{
BenchmarkRunner.Run<Decorators.KeylessGenericBenchmark>();
}
public void Decorator_Keyless_Nested() => BenchmarkRunner.Run<KeylessNestedBenchmark>();

[Fact]
public void Decorator_Keyless_Nested()
{
BenchmarkRunner.Run<Decorators.KeylessNestedBenchmark>();
}
public void Decorator_Keyless_Nested_Lambda() => BenchmarkRunner.Run<KeylessNestedLambdaBenchmark>();

[Fact]
public void Decorator_Keyless_Nested_Lambda()
{
BenchmarkRunner.Run<Decorators.KeylessNestedLambdaBenchmark>();
}
public void Decorator_Keyless_Simple() => BenchmarkRunner.Run<KeylessSimpleBenchmark>();

[Fact]
public void Decorator_Keyless_Simple()
{
BenchmarkRunner.Run<Decorators.KeylessSimpleBenchmark>();
}
public void Decorator_Keyless_Simple_Lambda() => BenchmarkRunner.Run<KeylessSimpleLambdaBenchmark>();

[Fact]
public void Decorator_Keyless_Simple_Lambda()
{
BenchmarkRunner.Run<Decorators.KeylessSimpleLambdaBenchmark>();
}
public void DeepGraphResolve() => BenchmarkRunner.Run<DeepGraphResolveBenchmark>();

[Fact]
public void EnumerableResolve()
{
BenchmarkRunner.Run<EnumerableResolveBenchmark>();
}
public void EnumerableResolve() => BenchmarkRunner.Run<EnumerableResolveBenchmark>();

[Fact]
public void PropertyInjection() => BenchmarkRunner.Run<PropertyInjectionBenchmark>();

[Fact]
public void RootContainerResolve() => BenchmarkRunner.Run<RootContainerResolveBenchmark>();
}
}

0 comments on commit ca70059

Please sign in to comment.