Created
November 26, 2020 12:36
-
-
Save benaadams/3f7ba86da58d253f90fc1891c43f9c64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Reflection; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using System.Runtime.CompilerServices; | |
public class Abstract | |
{ | |
} | |
[Import] | |
public class Solid : Abstract | |
{ | |
[Import] | |
public Solid() | |
{ | |
} | |
[Import] | |
public int field; | |
[Import] | |
public int Property { get; set; } | |
[Import] | |
public void Method() { } | |
[Import] | |
public event WaitCallback Event; | |
} | |
[MemoryDiagnoser] | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkSwitcher.FromAssembly(Assembly.GetExecutingAssembly()).Run(args); | |
} | |
public enum Variant | |
{ | |
GetCustomAttributes, | |
GetCustomAttributes_Type, | |
GetCustomAttribute | |
} | |
[Params(false, true)] | |
public bool Inherit { get; set; } | |
[Params(Variant.GetCustomAttributes, Variant.GetCustomAttributes_Type, Variant.GetCustomAttribute)] | |
public Variant CustomAttributeVariant { get; set; } | |
private PropertyInfo property = typeof(Solid).GetProperty(nameof(Solid.Property)); | |
private MethodInfo method = typeof(Solid).GetMethod(nameof(Solid.Method)); | |
private ConstructorInfo constructor = typeof(Solid).GetConstructors()[0]; | |
private EventInfo eventInfo = typeof(Solid).GetEvent(nameof(Solid.Event)); | |
private FieldInfo fieldInfo = typeof(Solid).GetField(nameof(Solid.field)); | |
private Type type = typeof(Solid); | |
[Benchmark] | |
public object PropertyInfo() | |
{ | |
return CustomAttributeVariant switch | |
{ | |
Variant.GetCustomAttributes => property.GetCustomAttributes(Inherit), | |
Variant.GetCustomAttributes_Type => property.GetCustomAttributes(typeof(ImportAttribute), Inherit), | |
_ => property.GetCustomAttribute(typeof(ImportAttribute), Inherit) | |
}; | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
[Benchmark] | |
public object Type() | |
{ | |
return CustomAttributeVariant switch | |
{ | |
Variant.GetCustomAttributes => type.GetCustomAttributes(Inherit), | |
Variant.GetCustomAttributes_Type => type.GetCustomAttributes(typeof(ImportAttribute), Inherit), | |
_ => type.GetCustomAttribute(typeof(ImportAttribute), Inherit) | |
}; | |
} | |
[Benchmark] | |
public object ConstructorInfo() | |
{ | |
return CustomAttributeVariant switch | |
{ | |
Variant.GetCustomAttributes => constructor.GetCustomAttributes(Inherit), | |
Variant.GetCustomAttributes_Type => constructor.GetCustomAttributes(typeof(ImportAttribute), Inherit), | |
_ => constructor.GetCustomAttribute(typeof(ImportAttribute), Inherit) | |
}; | |
} | |
[Benchmark] | |
public object MethodInfo() | |
{ | |
return CustomAttributeVariant switch | |
{ | |
Variant.GetCustomAttributes => method.GetCustomAttributes(Inherit), | |
Variant.GetCustomAttributes_Type => method.GetCustomAttributes(typeof(ImportAttribute), Inherit), | |
_ => method.GetCustomAttribute(typeof(ImportAttribute), Inherit) | |
}; | |
} | |
[Benchmark] | |
public object FieldInfo() | |
{ | |
return CustomAttributeVariant switch | |
{ | |
Variant.GetCustomAttributes => fieldInfo.GetCustomAttributes(Inherit), | |
Variant.GetCustomAttributes_Type => fieldInfo.GetCustomAttributes(typeof(ImportAttribute), Inherit), | |
_ => fieldInfo.GetCustomAttribute(typeof(ImportAttribute), Inherit) | |
}; | |
} | |
[Benchmark] | |
public object EventInfo() | |
{ | |
return CustomAttributeVariant switch | |
{ | |
Variant.GetCustomAttributes => eventInfo.GetCustomAttributes(Inherit), | |
Variant.GetCustomAttributes_Type => eventInfo.GetCustomAttributes(typeof(ImportAttribute), Inherit), | |
_ => eventInfo.GetCustomAttribute(typeof(ImportAttribute), Inherit) | |
}; | |
} | |
} | |
internal class ImportAttribute : Attribute | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment