-
Notifications
You must be signed in to change notification settings - Fork 0
/
InitializationModelImmutableList.linq
98 lines (85 loc) · 3.32 KB
/
InitializationModelImmutableList.linq
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
97
98
<Query Kind="Program">
<Reference Relative="ConfigDotNet\bin\Debug\net8.0\ConfigDotNet.dll">E:\CSProj\ConfigDotNet\ConfigDotNet\bin\Debug\net8.0\ConfigDotNet.dll</Reference>
<Reference Relative="ConfigDotNet.ObjectModel\bin\Debug\net8.0\ConfigDotNet.ObjectModel.dll">E:\CSProj\ConfigDotNet\ConfigDotNet.ObjectModel\bin\Debug\net8.0\ConfigDotNet.ObjectModel.dll</Reference>
<Namespace>BenchmarkDotNet</Namespace>
<Namespace>BenchmarkDotNet.Attributes</Namespace>
<Namespace>BenchmarkDotNet.Configs</Namespace>
<Namespace>BenchmarkDotNet.Diagnosers</Namespace>
<Namespace>BenchmarkDotNet.Engines</Namespace>
<Namespace>BenchmarkDotNet.Environments</Namespace>
<Namespace>BenchmarkDotNet.Exporters</Namespace>
<Namespace>BenchmarkDotNet.Jobs</Namespace>
<Namespace>BenchmarkDotNet.Loggers</Namespace>
<Namespace>BenchmarkDotNet.Reports</Namespace>
<Namespace>BenchmarkDotNet.Running</Namespace>
<Namespace>BenchmarkDotNet.Validators</Namespace>
<Namespace>ConfigDotNet</Namespace>
<Namespace>ConfigDotNet.ObjectModel</Namespace>
<Namespace>System.Collections.Immutable</Namespace>
<IncludeUncapsulator>false</IncludeUncapsulator>
<RuntimeVersion>8.0</RuntimeVersion>
</Query>
#load "BenchmarkDotNet"
#nullable enable
void Main()
{
"Start Benchmark".Dump();
RunBenchmark(false);
}
[MemoryDiagnoser]
public class Model
{
[GlobalSetup]
public void SetUp()
{
Dictionary<string, int> dict = [];
for(int i = 0; i < 100000; i++)
{
dict.Add(i.ToString(), i);
}
_config = new Configuration<int>(dict);
}
private Configuration<int> _config = null!;
[Benchmark]
public void Initialize()
{
var modelType = typeof(Mock);
PropsSections = (from prop in modelType.GetProperties()
where prop.PropertyType == typeof(int) &&
prop.CanWrite &&
prop.GetCustomAttribute(typeof(SectionAttribute)) is not null
select prop).ToImmutableList();
}
[Benchmark]
public void CreateInstance()
{
var model = Activator.CreateInstance<Mock>();
foreach (var section in _config)
{
PropertyInfo? prop = PropsSections.FirstOrDefault(prop =>
((SectionAttribute?)prop.GetCustomAttribute(typeof(SectionAttribute)))
!.SectionName == section.Key);
prop?.SetValue(model, section.Value);
}
}
private static ImmutableList<PropertyInfo> PropsSections { get; set; } = null!;
}
[ConfigurationModel<int>]
public class Mock
{
[Section("0")] public int A { get; set; }
[Section("1")] public int B { get; set; }
[Section("2")] public int C { get; set; }
[Section("3")] public int D { get; set; }
[Section("4")] public int E { get; set; }
[Section("5")] public int F { get; set; }
[Section("6")] public int G { get; set; }
[Section("7")] public int H { get; set; }
[Section("8")] public int I { get; set; }
[Section("9")] public int K { get; set; }
[Section("10")] public int J { get; set; }
[Section("11")] public int L { get; set; }
[Section("12")] public int M { get; set; }
[Section("13")] public int N { get; set; }
[Section("14")] public int O { get; set; }
}