forked from autofac/Autofac
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added benchmark for resolving from child scopes.
- Loading branch information
Showing
2 changed files
with
81 additions
and
48 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 |
---|---|---|
@@ -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 { } | ||
} | ||
} |
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