Skip to content

Commit b65eee0

Browse files
committed
Differentiating between design-time and runtime args methods
1 parent 295f4a5 commit b65eee0

File tree

3 files changed

+110
-102
lines changed

3 files changed

+110
-102
lines changed

CreateInstanceFromType.Benchmark/Program.cs

+103-95
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,98 @@
1-
using System;
2-
using BenchmarkDotNet.Attributes;
3-
using BenchmarkDotNet.Configs;
4-
using BenchmarkDotNet.Running;
5-
using CreateInstanceFromType.Tests.TestClasses;
6-
7-
namespace CreateInstanceFromType.Benchmark
1+
namespace CreateInstanceFromType.Benchmark
82
{
3+
using System;
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Columns;
6+
using BenchmarkDotNet.Configs;
7+
using BenchmarkDotNet.Diagnosers;
8+
using BenchmarkDotNet.Running;
9+
using Tests.TestClasses;
10+
911
public class Program
1012
{
11-
[MemoryDiagnoser]
12-
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
13+
[Config(typeof(Config))]
1314
public class CreateInstanceFromTypeBenchmark
1415
{
16+
private class Config : ManualConfig
17+
{
18+
public Config()
19+
{
20+
AddDiagnoser(MemoryDiagnoser.Default);
21+
AddLogicalGroupRules(BenchmarkLogicalGroupRule.ByCategory);
22+
AddColumn(new TagColumn("Params", name => name.Substring(name.LastIndexOf('_') + 1)));
23+
AddColumn(new TagColumn("Args", name => name[0] == 'R' ? "Runtime" : "Design-time"));
24+
}
25+
}
26+
1527
private const string _parameterless = "Parameterless";
1628
private const string _oneParamCtor = "One Param";
1729
private const string _twoParamsCtor = "Two Params";
1830
private const string _threeParamsCtor = "Three Params";
1931

20-
[Benchmark(Baseline = true, Description = "New"), BenchmarkCategory(_parameterless)]
21-
public object New_0()
32+
private const string _designTimeArgs = "Design-time args";
33+
private const string _runtimeArgs = "Runtime args";
34+
35+
#region New (Design-time Args)
36+
37+
[BenchmarkCategory(_designTimeArgs, _parameterless)]
38+
[Benchmark(Baseline = true, Description = "New")]
39+
public object D_New_0()
2240
{
2341
return new Parameterless();
2442
}
2543

26-
[Benchmark(Baseline = true, Description = "New"), BenchmarkCategory(_oneParamCtor)]
27-
public object New_1()
44+
[BenchmarkCategory(_designTimeArgs, _oneParamCtor)]
45+
[Benchmark(Baseline = true, Description = "New")]
46+
public object D_New_1()
2847
{
2948
return new OneParamCtor("hello!");
3049
}
3150

32-
[Benchmark(Baseline = true, Description = "New"), BenchmarkCategory(_twoParamsCtor)]
33-
public object New_2()
51+
[BenchmarkCategory(_designTimeArgs, _twoParamsCtor)]
52+
[Benchmark(Baseline = true, Description = "New")]
53+
public object D_New_2()
3454
{
3555
return new TwoParamCtor("hello!", 123);
3656
}
3757

38-
[Benchmark(Baseline = true, Description = "New"), BenchmarkCategory(_threeParamsCtor)]
39-
public object New_3()
58+
[BenchmarkCategory(_designTimeArgs, _threeParamsCtor)]
59+
[Benchmark(Baseline = true, Description = "New")]
60+
public object D_New_3()
4061
{
4162
return new MultiCtor("hello!", 123, DateTime.MinValue);
4263
}
4364

44-
#region Activator.CreateInstance
45-
46-
[Benchmark(Description = "Activator"), BenchmarkCategory(_parameterless)]
47-
public object ActivatorCreateInstance_0()
48-
{
49-
return Activator.CreateInstance(typeof(Parameterless));
50-
}
51-
52-
[Benchmark(Description = "Activator"), BenchmarkCategory(_oneParamCtor)]
53-
public object ActivatorCreateInstance_1()
54-
{
55-
return Activator.CreateInstance(typeof(OneParamCtor), "hello!");
56-
}
57-
58-
[Benchmark(Description = "Activator"), BenchmarkCategory(_twoParamsCtor)]
59-
public object ActivatorCreateInstance_2()
60-
{
61-
return Activator.CreateInstance(typeof(TwoParamCtor), "hello!", 123);
62-
}
63-
64-
[Benchmark(Description = "Activator"), BenchmarkCategory(_threeParamsCtor)]
65-
public object ActivatorCreateInstance_3()
66-
{
67-
return Activator.CreateInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
68-
}
69-
7065
#endregion
7166

72-
#region 2012
67+
#region 2012 (Design-time Args)
7368

74-
[Benchmark(Description = "2012"), BenchmarkCategory(_parameterless)]
75-
public object CreateInstanceFromType_2012_0()
69+
[BenchmarkCategory(_designTimeArgs, _parameterless)]
70+
[Benchmark(Description = "2012")]
71+
public object D_2012_CreateInstanceFromType_0()
7672
{
7773
return CreateInstanceFromType2012
7874
.GetInstance(typeof(Parameterless));
7975
}
8076

81-
[Benchmark(Description = "2012"), BenchmarkCategory(_oneParamCtor)]
82-
public object CreateInstanceFromType_2012_1()
77+
[BenchmarkCategory(_designTimeArgs, _oneParamCtor)]
78+
[Benchmark(Description = "2012")]
79+
public object D_2012_CreateInstanceFromType_1()
8380
{
8481
return CreateInstanceFromType2012
8582
.GetInstance(typeof(OneParamCtor), "hello!");
8683
}
8784

88-
[Benchmark(Description = "2012"), BenchmarkCategory(_twoParamsCtor)]
89-
public object CreateInstanceFromType_2012_2()
85+
[BenchmarkCategory(_designTimeArgs, _twoParamsCtor)]
86+
[Benchmark(Description = "2012")]
87+
public object D_2012_CreateInstanceFromType_2()
9088
{
9189
return CreateInstanceFromType2012
9290
.GetInstance(typeof(TwoParamCtor), "hello!", 123);
9391
}
9492

95-
[Benchmark(Description = "2012"), BenchmarkCategory(_threeParamsCtor)]
96-
public object CreateInstanceFromType_2012_3()
93+
[BenchmarkCategory(_designTimeArgs, _threeParamsCtor)]
94+
[Benchmark(Description = "2012")]
95+
public object D_2012_CreateInstanceFromType_3()
9796
{
9897
return CreateInstanceFromType2012
9998
.GetInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
@@ -103,98 +102,107 @@ public object CreateInstanceFromType_2012_3()
103102

104103
#region 2020 Design-Time Args
105104

106-
[Benchmark(Description = "2020"), BenchmarkCategory(_parameterless)]
107-
public object CreateInstanceFromType_2020_D0()
105+
[BenchmarkCategory(_designTimeArgs, _parameterless)]
106+
[Benchmark(Description = "2020")]
107+
public object D_2020_CreateInstanceFromType_0()
108108
{
109109
return CreateInstanceFromType2020DesignTimeArgs
110110
.GetInstance(typeof(Parameterless));
111111
}
112112

113-
[Benchmark(Description = "2020"), BenchmarkCategory(_oneParamCtor)]
114-
public object CreateInstanceFromType_2020_D1()
113+
[BenchmarkCategory(_designTimeArgs, _oneParamCtor)]
114+
[Benchmark(Description = "2020")]
115+
public object D_2020_CreateInstanceFromType_1()
115116
{
116117
return CreateInstanceFromType2020DesignTimeArgs
117118
.GetInstance(typeof(OneParamCtor), "hello!");
118119
}
119120

120-
[Benchmark(Description = "2020"), BenchmarkCategory(_twoParamsCtor)]
121-
public object CreateInstanceFromType_2020_D2()
121+
[BenchmarkCategory(_designTimeArgs, _twoParamsCtor)]
122+
[Benchmark(Description = "2020")]
123+
public object D_2020_CreateInstanceFromType_2()
122124
{
123125
return CreateInstanceFromType2020DesignTimeArgs
124126
.GetInstance(typeof(TwoParamCtor), "hello!", 123);
125127
}
126128

127-
[Benchmark(Description = "2020"), BenchmarkCategory(_threeParamsCtor)]
128-
public object CreateInstanceFromType_2020_D3()
129+
[BenchmarkCategory(_designTimeArgs, _threeParamsCtor)]
130+
[Benchmark(Description = "2020")]
131+
public object D_2020_CreateInstanceFromType_3()
129132
{
130133
return CreateInstanceFromType2020DesignTimeArgs
131134
.GetInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
132135
}
133136

134137
#endregion
135138

136-
#region 2020 Runtime Args
139+
#region Activator.CreateInstance (Runtime Args)
137140

138-
[Benchmark(Description = "2020"), BenchmarkCategory(_parameterless)]
139-
public object CreateInstanceFromType_2020_R0()
141+
[BenchmarkCategory(_runtimeArgs, _parameterless)]
142+
[Benchmark(Baseline = true, Description = "Activator")]
143+
public object R_ActivatorCreateInstance_0()
140144
{
141-
return CreateInstanceFromType2020RuntimeArgs
142-
.GetInstance(typeof(Parameterless));
145+
return Activator.CreateInstance(typeof(Parameterless));
143146
}
144147

145-
[Benchmark(Description = "2020"), BenchmarkCategory(_oneParamCtor)]
146-
public object CreateInstanceFromType_2020_R1()
148+
[BenchmarkCategory(_runtimeArgs, _oneParamCtor)]
149+
[Benchmark(Baseline = true, Description = "Activator")]
150+
public object R_ActivatorCreateInstance_1()
147151
{
148-
return CreateInstanceFromType2020RuntimeArgs
149-
.GetInstance(typeof(OneParamCtor), "hello!");
152+
return Activator.CreateInstance(typeof(OneParamCtor), "hello!");
150153
}
151154

152-
[Benchmark(Description = "2020"), BenchmarkCategory(_twoParamsCtor)]
153-
public object CreateInstanceFromType_2020_R2()
155+
[BenchmarkCategory(_runtimeArgs, _twoParamsCtor)]
156+
[Benchmark(Baseline = true, Description = "Activator")]
157+
public object R_ActivatorCreateInstance_2()
154158
{
155-
return CreateInstanceFromType2020RuntimeArgs
156-
.GetInstance(typeof(TwoParamCtor), "hello!", 123);
159+
return Activator.CreateInstance(typeof(TwoParamCtor), "hello!", 123);
157160
}
158161

159-
[Benchmark(Description = "2020"), BenchmarkCategory(_threeParamsCtor)]
160-
public object CreateInstanceFromType_2020_R3()
162+
[BenchmarkCategory(_runtimeArgs, _threeParamsCtor)]
163+
[Benchmark(Baseline = true, Description = "Activator")]
164+
public object R_ActivatorCreateInstance_3()
161165
{
162-
return CreateInstanceFromType2020RuntimeArgs
163-
.GetInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
166+
return Activator.CreateInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
164167
}
165168

166169
#endregion
167-
}
168170

169-
[MemoryDiagnoser]
170-
public class CreateInstanceFromTypeOneParamCtor
171-
{
172-
[Benchmark(Baseline = true)]
173-
public object New()
171+
#region 2020 Runtime Args
172+
173+
[BenchmarkCategory(_runtimeArgs, _parameterless)]
174+
[Benchmark(Description = "2020")]
175+
public object R_2020_CreateInstanceFromType_0()
174176
{
175-
return new OneParamCtor("Hello!");
177+
return CreateInstanceFromType2020RuntimeArgs
178+
.GetInstance(typeof(Parameterless));
176179
}
177180

178-
[Benchmark]
179-
public object ActivatorCreateInstance()
181+
[BenchmarkCategory(_runtimeArgs, _oneParamCtor)]
182+
[Benchmark(Description = "2020")]
183+
public object R_2020_CreateInstanceFromType_1()
180184
{
181-
return Activator
182-
.CreateInstance(typeof(OneParamCtor), "Hello!");
185+
return CreateInstanceFromType2020RuntimeArgs
186+
.GetInstance(typeof(OneParamCtor), "hello!");
183187
}
184188

185-
[Benchmark]
186-
public object CreateInstanceFromType_2012()
189+
[BenchmarkCategory(_runtimeArgs, _twoParamsCtor)]
190+
[Benchmark(Description = "2020")]
191+
public object R_2020_CreateInstanceFromType_2()
187192
{
188-
return CreateInstanceFromType2012
189-
.GetInstance(typeof(OneParamCtor), "Hello!");
193+
return CreateInstanceFromType2020RuntimeArgs
194+
.GetInstance(typeof(TwoParamCtor), "hello!", 123);
190195
}
191196

192-
[Benchmark]
193-
public object CreateInstanceFromType_2020()
197+
[BenchmarkCategory(_runtimeArgs, _threeParamsCtor)]
198+
[Benchmark(Description = "2020")]
199+
public object R_2020_CreateInstanceFromType_3()
194200
{
195201
return CreateInstanceFromType2020RuntimeArgs
196-
.GetInstance(typeof(OneParamCtor), "Hello!");
202+
.GetInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
197203
}
204+
205+
#endregion
198206
}
199207

200208
public static void Main(string[] args)

CreateInstanceFromType.Tests/InstanceFromTypeCreationTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class InstanceFromTypeCreationTests
1010
[Fact]
1111
public void ShouldUseAParameterlessCtor()
1212
{
13-
var instance = (Parameterless)CreateInstanceFromType2020RuntimeArgs
13+
var instance = (Parameterless)CreateInstanceFromType2020DesignTimeArgs
1414
.GetInstance(typeof(Parameterless));
1515

1616
Assert.NotNull(instance);
@@ -19,7 +19,7 @@ public void ShouldUseAParameterlessCtor()
1919
[Fact]
2020
public void ShouldUseASingleParameterCtor()
2121
{
22-
var instance = (OneParamCtor)CreateInstanceFromType2020RuntimeArgs
22+
var instance = (OneParamCtor)CreateInstanceFromType2020DesignTimeArgs
2323
.GetInstance(typeof(OneParamCtor), "hello!");
2424

2525
Assert.NotNull(instance);
@@ -29,7 +29,7 @@ public void ShouldUseASingleParameterCtor()
2929
[Fact]
3030
public void ShouldUseATwoParameterCtor()
3131
{
32-
var instance = (TwoParamCtor)CreateInstanceFromType2020RuntimeArgs
32+
var instance = (TwoParamCtor)CreateInstanceFromType2020DesignTimeArgs
3333
.GetInstance(typeof(TwoParamCtor), "hello again!", 123);
3434

3535
Assert.NotNull(instance);
@@ -40,23 +40,23 @@ public void ShouldUseATwoParameterCtor()
4040
[Fact]
4141
public void ShouldSelectACtorFromArguments()
4242
{
43-
var twoParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
43+
var twoParamInstance = (MultiCtor)CreateInstanceFromType2020DesignTimeArgs
4444
.GetInstance(typeof(MultiCtor), "hello there!", 456);
4545

4646
Assert.NotNull(twoParamInstance);
4747
Assert.Equal("hello there!", twoParamInstance.StringValue);
4848
Assert.Equal(456, twoParamInstance.IntValue);
4949
Assert.Equal(default, twoParamInstance.DateValue);
5050

51-
var oneParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
51+
var oneParamInstance = (MultiCtor)CreateInstanceFromType2020DesignTimeArgs
5252
.GetInstance(typeof(MultiCtor), "hello you!");
5353

5454
Assert.NotNull(oneParamInstance);
5555
Assert.Equal("hello you!", oneParamInstance.StringValue);
5656
Assert.Equal(default, oneParamInstance.IntValue);
5757
Assert.Equal(default, twoParamInstance.DateValue);
5858

59-
var threeParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
59+
var threeParamInstance = (MultiCtor)CreateInstanceFromType2020DesignTimeArgs
6060
.GetInstance(typeof(MultiCtor), "hello blah!", 999, DateTime.MinValue);
6161

6262
Assert.NotNull(threeParamInstance);

CreateInstanceFromType/CreateInstanceOfType2020DesignTimeArgs.cs renamed to CreateInstanceFromType/CreateInstanceFromType2020DesignTimeArgs.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public static Func<TArg1, TArg2, TArg3, object> GetFactoryFor(Type type)
171171
var ctor = t.GetConstructor(new[] { arg1Type, arg2Type, arg3Type });
172172
var argument1 = Expression.Parameter(arg1Type, "param1");
173173
var argument2 = Expression.Parameter(arg2Type, "param2");
174-
var argument3 = Expression.Parameter(arg2Type, "param3");
174+
var argument3 = Expression.Parameter(arg3Type, "param3");
175175

176176
var instanceCreation = Expression
177177
.New(ctor, argument1, argument2, argument3);

0 commit comments

Comments
 (0)