-
-
Notifications
You must be signed in to change notification settings - Fork 832
/
ConcurrencyBenchmak.cs
96 lines (80 loc) · 2.38 KB
/
ConcurrencyBenchmak.cs
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
96
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
namespace Autofac.Benchmarks
{
/// <summary>
/// Tests the performance of concurrent resolution of a (reasonably) deeply-nested object graph.
/// </summary>
public class ConcurrencyBenchmark
{
private readonly IContainer _container;
public ConcurrencyBenchmark()
{
var builder = new ContainerBuilder();
builder.RegisterType<A>().SingleInstance();
builder.RegisterType<B1>();
builder.RegisterType<B2>();
builder.RegisterType<C1>();
builder.RegisterType<C2>();
builder.RegisterType<D1>();
builder.RegisterType<D2>();
_container = builder.Build();
}
[Params(10, 100, 1_000)]
public int ResolveTaskCount { get; set; }
[Params(1_000, 10_000)]
public int ResolvesPerTask { get; set; }
[Benchmark]
public void MultipleResolvesOnMultipleTasks()
{
var tasks = new List<Task>(ResolveTaskCount);
for (var i = 0; i < ResolveTaskCount; i++)
{
var task = Task.Run(() =>
{
try
{
for (var j = 0; j < ResolvesPerTask; j++)
{
var instance = _container.Resolve<A>();
Assert.NotNull(instance);
}
}
catch (Exception ex)
{
Assert.True(false, ex.ToString());
}
});
tasks.Add(task);
}
Task.WhenAll(tasks);
}
#pragma warning disable SA1402, SA1502
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 { }
#pragma warning restore SA1402, SA1502
}
}