-
-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathChildScopeResolveBenchmark.cs
More file actions
95 lines (82 loc) · 2.06 KB
/
Copy pathChildScopeResolveBenchmark.cs
File metadata and controls
95 lines (82 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
namespace Autofac.Benchmarks;
public class ChildScopeResolveBenchmark
{
private IContainer _container = default!;
[Benchmark]
public void Resolve()
{
using (var requestScope = _container.BeginLifetimeScope("request", b => b.RegisterType<C1>()))
{
using (var unitOfWorkScope = requestScope.BeginLifetimeScope())
{
var instance = unitOfWorkScope.Resolve<A>();
GC.KeepAlive(instance);
}
}
}
[Benchmark]
public void ResolveNeverRegisteredFromChild()
{
using (var requestScope = _container.BeginLifetimeScope("request", b => b.RegisterType<C1>()))
{
using (var unitOfWorkScope = requestScope.BeginLifetimeScope())
{
var instance = unitOfWorkScope.Resolve<IEnumerable<NeverRegistered>>();
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();
_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
{
}
internal class NeverRegistered
{
}
}