Differentiating between design-time and runtime args methods
This commit is contained in:
Родитель
295f4a5de3
Коммит
b65eee0540
|
@ -1,99 +1,98 @@
|
|||
using System;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Configs;
|
||||
using BenchmarkDotNet.Running;
|
||||
using CreateInstanceFromType.Tests.TestClasses;
|
||||
|
||||
namespace CreateInstanceFromType.Benchmark
|
||||
namespace CreateInstanceFromType.Benchmark
|
||||
{
|
||||
using System;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Columns;
|
||||
using BenchmarkDotNet.Configs;
|
||||
using BenchmarkDotNet.Diagnosers;
|
||||
using BenchmarkDotNet.Running;
|
||||
using Tests.TestClasses;
|
||||
|
||||
public class Program
|
||||
{
|
||||
[MemoryDiagnoser]
|
||||
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
|
||||
[Config(typeof(Config))]
|
||||
public class CreateInstanceFromTypeBenchmark
|
||||
{
|
||||
private class Config : ManualConfig
|
||||
{
|
||||
public Config()
|
||||
{
|
||||
AddDiagnoser(MemoryDiagnoser.Default);
|
||||
AddLogicalGroupRules(BenchmarkLogicalGroupRule.ByCategory);
|
||||
AddColumn(new TagColumn("Params", name => name.Substring(name.LastIndexOf('_') + 1)));
|
||||
AddColumn(new TagColumn("Args", name => name[0] == 'R' ? "Runtime" : "Design-time"));
|
||||
}
|
||||
}
|
||||
|
||||
private const string _parameterless = "Parameterless";
|
||||
private const string _oneParamCtor = "One Param";
|
||||
private const string _twoParamsCtor = "Two Params";
|
||||
private const string _threeParamsCtor = "Three Params";
|
||||
|
||||
[Benchmark(Baseline = true, Description = "New"), BenchmarkCategory(_parameterless)]
|
||||
public object New_0()
|
||||
private const string _designTimeArgs = "Design-time args";
|
||||
private const string _runtimeArgs = "Runtime args";
|
||||
|
||||
#region New (Design-time Args)
|
||||
|
||||
[BenchmarkCategory(_designTimeArgs, _parameterless)]
|
||||
[Benchmark(Baseline = true, Description = "New")]
|
||||
public object D_New_0()
|
||||
{
|
||||
return new Parameterless();
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true, Description = "New"), BenchmarkCategory(_oneParamCtor)]
|
||||
public object New_1()
|
||||
[BenchmarkCategory(_designTimeArgs, _oneParamCtor)]
|
||||
[Benchmark(Baseline = true, Description = "New")]
|
||||
public object D_New_1()
|
||||
{
|
||||
return new OneParamCtor("hello!");
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true, Description = "New"), BenchmarkCategory(_twoParamsCtor)]
|
||||
public object New_2()
|
||||
[BenchmarkCategory(_designTimeArgs, _twoParamsCtor)]
|
||||
[Benchmark(Baseline = true, Description = "New")]
|
||||
public object D_New_2()
|
||||
{
|
||||
return new TwoParamCtor("hello!", 123);
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true, Description = "New"), BenchmarkCategory(_threeParamsCtor)]
|
||||
public object New_3()
|
||||
[BenchmarkCategory(_designTimeArgs, _threeParamsCtor)]
|
||||
[Benchmark(Baseline = true, Description = "New")]
|
||||
public object D_New_3()
|
||||
{
|
||||
return new MultiCtor("hello!", 123, DateTime.MinValue);
|
||||
}
|
||||
|
||||
#region Activator.CreateInstance
|
||||
|
||||
[Benchmark(Description = "Activator"), BenchmarkCategory(_parameterless)]
|
||||
public object ActivatorCreateInstance_0()
|
||||
{
|
||||
return Activator.CreateInstance(typeof(Parameterless));
|
||||
}
|
||||
|
||||
[Benchmark(Description = "Activator"), BenchmarkCategory(_oneParamCtor)]
|
||||
public object ActivatorCreateInstance_1()
|
||||
{
|
||||
return Activator.CreateInstance(typeof(OneParamCtor), "hello!");
|
||||
}
|
||||
|
||||
[Benchmark(Description = "Activator"), BenchmarkCategory(_twoParamsCtor)]
|
||||
public object ActivatorCreateInstance_2()
|
||||
{
|
||||
return Activator.CreateInstance(typeof(TwoParamCtor), "hello!", 123);
|
||||
}
|
||||
|
||||
[Benchmark(Description = "Activator"), BenchmarkCategory(_threeParamsCtor)]
|
||||
public object ActivatorCreateInstance_3()
|
||||
{
|
||||
return Activator.CreateInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 2012
|
||||
#region 2012 (Design-time Args)
|
||||
|
||||
[Benchmark(Description = "2012"), BenchmarkCategory(_parameterless)]
|
||||
public object CreateInstanceFromType_2012_0()
|
||||
[BenchmarkCategory(_designTimeArgs, _parameterless)]
|
||||
[Benchmark(Description = "2012")]
|
||||
public object D_2012_CreateInstanceFromType_0()
|
||||
{
|
||||
return CreateInstanceFromType2012
|
||||
.GetInstance(typeof(Parameterless));
|
||||
}
|
||||
|
||||
[Benchmark(Description = "2012"), BenchmarkCategory(_oneParamCtor)]
|
||||
public object CreateInstanceFromType_2012_1()
|
||||
[BenchmarkCategory(_designTimeArgs, _oneParamCtor)]
|
||||
[Benchmark(Description = "2012")]
|
||||
public object D_2012_CreateInstanceFromType_1()
|
||||
{
|
||||
return CreateInstanceFromType2012
|
||||
.GetInstance(typeof(OneParamCtor), "hello!");
|
||||
}
|
||||
|
||||
[Benchmark(Description = "2012"), BenchmarkCategory(_twoParamsCtor)]
|
||||
public object CreateInstanceFromType_2012_2()
|
||||
[BenchmarkCategory(_designTimeArgs, _twoParamsCtor)]
|
||||
[Benchmark(Description = "2012")]
|
||||
public object D_2012_CreateInstanceFromType_2()
|
||||
{
|
||||
return CreateInstanceFromType2012
|
||||
.GetInstance(typeof(TwoParamCtor), "hello!", 123);
|
||||
}
|
||||
|
||||
[Benchmark(Description = "2012"), BenchmarkCategory(_threeParamsCtor)]
|
||||
public object CreateInstanceFromType_2012_3()
|
||||
[BenchmarkCategory(_designTimeArgs, _threeParamsCtor)]
|
||||
[Benchmark(Description = "2012")]
|
||||
public object D_2012_CreateInstanceFromType_3()
|
||||
{
|
||||
return CreateInstanceFromType2012
|
||||
.GetInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
|
||||
|
@ -103,29 +102,33 @@ namespace CreateInstanceFromType.Benchmark
|
|||
|
||||
#region 2020 Design-Time Args
|
||||
|
||||
[Benchmark(Description = "2020"), BenchmarkCategory(_parameterless)]
|
||||
public object CreateInstanceFromType_2020_D0()
|
||||
[BenchmarkCategory(_designTimeArgs, _parameterless)]
|
||||
[Benchmark(Description = "2020")]
|
||||
public object D_2020_CreateInstanceFromType_0()
|
||||
{
|
||||
return CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(Parameterless));
|
||||
}
|
||||
|
||||
[Benchmark(Description = "2020"), BenchmarkCategory(_oneParamCtor)]
|
||||
public object CreateInstanceFromType_2020_D1()
|
||||
[BenchmarkCategory(_designTimeArgs, _oneParamCtor)]
|
||||
[Benchmark(Description = "2020")]
|
||||
public object D_2020_CreateInstanceFromType_1()
|
||||
{
|
||||
return CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(OneParamCtor), "hello!");
|
||||
}
|
||||
|
||||
[Benchmark(Description = "2020"), BenchmarkCategory(_twoParamsCtor)]
|
||||
public object CreateInstanceFromType_2020_D2()
|
||||
[BenchmarkCategory(_designTimeArgs, _twoParamsCtor)]
|
||||
[Benchmark(Description = "2020")]
|
||||
public object D_2020_CreateInstanceFromType_2()
|
||||
{
|
||||
return CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(TwoParamCtor), "hello!", 123);
|
||||
}
|
||||
|
||||
[Benchmark(Description = "2020"), BenchmarkCategory(_threeParamsCtor)]
|
||||
public object CreateInstanceFromType_2020_D3()
|
||||
[BenchmarkCategory(_designTimeArgs, _threeParamsCtor)]
|
||||
[Benchmark(Description = "2020")]
|
||||
public object D_2020_CreateInstanceFromType_3()
|
||||
{
|
||||
return CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
|
||||
|
@ -133,31 +136,67 @@ namespace CreateInstanceFromType.Benchmark
|
|||
|
||||
#endregion
|
||||
|
||||
#region Activator.CreateInstance (Runtime Args)
|
||||
|
||||
[BenchmarkCategory(_runtimeArgs, _parameterless)]
|
||||
[Benchmark(Baseline = true, Description = "Activator")]
|
||||
public object R_ActivatorCreateInstance_0()
|
||||
{
|
||||
return Activator.CreateInstance(typeof(Parameterless));
|
||||
}
|
||||
|
||||
[BenchmarkCategory(_runtimeArgs, _oneParamCtor)]
|
||||
[Benchmark(Baseline = true, Description = "Activator")]
|
||||
public object R_ActivatorCreateInstance_1()
|
||||
{
|
||||
return Activator.CreateInstance(typeof(OneParamCtor), "hello!");
|
||||
}
|
||||
|
||||
[BenchmarkCategory(_runtimeArgs, _twoParamsCtor)]
|
||||
[Benchmark(Baseline = true, Description = "Activator")]
|
||||
public object R_ActivatorCreateInstance_2()
|
||||
{
|
||||
return Activator.CreateInstance(typeof(TwoParamCtor), "hello!", 123);
|
||||
}
|
||||
|
||||
[BenchmarkCategory(_runtimeArgs, _threeParamsCtor)]
|
||||
[Benchmark(Baseline = true, Description = "Activator")]
|
||||
public object R_ActivatorCreateInstance_3()
|
||||
{
|
||||
return Activator.CreateInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 2020 Runtime Args
|
||||
|
||||
[Benchmark(Description = "2020"), BenchmarkCategory(_parameterless)]
|
||||
public object CreateInstanceFromType_2020_R0()
|
||||
[BenchmarkCategory(_runtimeArgs, _parameterless)]
|
||||
[Benchmark(Description = "2020")]
|
||||
public object R_2020_CreateInstanceFromType_0()
|
||||
{
|
||||
return CreateInstanceFromType2020RuntimeArgs
|
||||
.GetInstance(typeof(Parameterless));
|
||||
}
|
||||
|
||||
[Benchmark(Description = "2020"), BenchmarkCategory(_oneParamCtor)]
|
||||
public object CreateInstanceFromType_2020_R1()
|
||||
[BenchmarkCategory(_runtimeArgs, _oneParamCtor)]
|
||||
[Benchmark(Description = "2020")]
|
||||
public object R_2020_CreateInstanceFromType_1()
|
||||
{
|
||||
return CreateInstanceFromType2020RuntimeArgs
|
||||
.GetInstance(typeof(OneParamCtor), "hello!");
|
||||
}
|
||||
|
||||
[Benchmark(Description = "2020"), BenchmarkCategory(_twoParamsCtor)]
|
||||
public object CreateInstanceFromType_2020_R2()
|
||||
[BenchmarkCategory(_runtimeArgs, _twoParamsCtor)]
|
||||
[Benchmark(Description = "2020")]
|
||||
public object R_2020_CreateInstanceFromType_2()
|
||||
{
|
||||
return CreateInstanceFromType2020RuntimeArgs
|
||||
.GetInstance(typeof(TwoParamCtor), "hello!", 123);
|
||||
}
|
||||
|
||||
[Benchmark(Description = "2020"), BenchmarkCategory(_threeParamsCtor)]
|
||||
public object CreateInstanceFromType_2020_R3()
|
||||
[BenchmarkCategory(_runtimeArgs, _threeParamsCtor)]
|
||||
[Benchmark(Description = "2020")]
|
||||
public object R_2020_CreateInstanceFromType_3()
|
||||
{
|
||||
return CreateInstanceFromType2020RuntimeArgs
|
||||
.GetInstance(typeof(MultiCtor), "hello!", 123, DateTime.MinValue);
|
||||
|
@ -166,37 +205,6 @@ namespace CreateInstanceFromType.Benchmark
|
|||
#endregion
|
||||
}
|
||||
|
||||
[MemoryDiagnoser]
|
||||
public class CreateInstanceFromTypeOneParamCtor
|
||||
{
|
||||
[Benchmark(Baseline = true)]
|
||||
public object New()
|
||||
{
|
||||
return new OneParamCtor("Hello!");
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public object ActivatorCreateInstance()
|
||||
{
|
||||
return Activator
|
||||
.CreateInstance(typeof(OneParamCtor), "Hello!");
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public object CreateInstanceFromType_2012()
|
||||
{
|
||||
return CreateInstanceFromType2012
|
||||
.GetInstance(typeof(OneParamCtor), "Hello!");
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public object CreateInstanceFromType_2020()
|
||||
{
|
||||
return CreateInstanceFromType2020RuntimeArgs
|
||||
.GetInstance(typeof(OneParamCtor), "Hello!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
BenchmarkRunner.Run<CreateInstanceFromTypeBenchmark>();
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace CreateInstanceFromType.Tests
|
|||
[Fact]
|
||||
public void ShouldUseAParameterlessCtor()
|
||||
{
|
||||
var instance = (Parameterless)CreateInstanceFromType2020RuntimeArgs
|
||||
var instance = (Parameterless)CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(Parameterless));
|
||||
|
||||
Assert.NotNull(instance);
|
||||
|
@ -19,7 +19,7 @@ namespace CreateInstanceFromType.Tests
|
|||
[Fact]
|
||||
public void ShouldUseASingleParameterCtor()
|
||||
{
|
||||
var instance = (OneParamCtor)CreateInstanceFromType2020RuntimeArgs
|
||||
var instance = (OneParamCtor)CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(OneParamCtor), "hello!");
|
||||
|
||||
Assert.NotNull(instance);
|
||||
|
@ -29,7 +29,7 @@ namespace CreateInstanceFromType.Tests
|
|||
[Fact]
|
||||
public void ShouldUseATwoParameterCtor()
|
||||
{
|
||||
var instance = (TwoParamCtor)CreateInstanceFromType2020RuntimeArgs
|
||||
var instance = (TwoParamCtor)CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(TwoParamCtor), "hello again!", 123);
|
||||
|
||||
Assert.NotNull(instance);
|
||||
|
@ -40,7 +40,7 @@ namespace CreateInstanceFromType.Tests
|
|||
[Fact]
|
||||
public void ShouldSelectACtorFromArguments()
|
||||
{
|
||||
var twoParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
|
||||
var twoParamInstance = (MultiCtor)CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(MultiCtor), "hello there!", 456);
|
||||
|
||||
Assert.NotNull(twoParamInstance);
|
||||
|
@ -48,7 +48,7 @@ namespace CreateInstanceFromType.Tests
|
|||
Assert.Equal(456, twoParamInstance.IntValue);
|
||||
Assert.Equal(default, twoParamInstance.DateValue);
|
||||
|
||||
var oneParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
|
||||
var oneParamInstance = (MultiCtor)CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(MultiCtor), "hello you!");
|
||||
|
||||
Assert.NotNull(oneParamInstance);
|
||||
|
@ -56,7 +56,7 @@ namespace CreateInstanceFromType.Tests
|
|||
Assert.Equal(default, oneParamInstance.IntValue);
|
||||
Assert.Equal(default, twoParamInstance.DateValue);
|
||||
|
||||
var threeParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
|
||||
var threeParamInstance = (MultiCtor)CreateInstanceFromType2020DesignTimeArgs
|
||||
.GetInstance(typeof(MultiCtor), "hello blah!", 999, DateTime.MinValue);
|
||||
|
||||
Assert.NotNull(threeParamInstance);
|
||||
|
|
|
@ -171,7 +171,7 @@ namespace CreateInstanceFromType
|
|||
var ctor = t.GetConstructor(new[] { arg1Type, arg2Type, arg3Type });
|
||||
var argument1 = Expression.Parameter(arg1Type, "param1");
|
||||
var argument2 = Expression.Parameter(arg2Type, "param2");
|
||||
var argument3 = Expression.Parameter(arg2Type, "param3");
|
||||
var argument3 = Expression.Parameter(arg3Type, "param3");
|
||||
|
||||
var instanceCreation = Expression
|
||||
.New(ctor, argument1, argument2, argument3);
|
Загрузка…
Ссылка в новой задаче