This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
180 lines (149 loc) · 5.56 KB
/
Program.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using FastAndFaster.Tests.DummyClasses;
using System;
using System.Reflection;
namespace FastAndFaster.Benchmark
{
class Program
{
static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
[MemoryDiagnoser]
public class TestCreatorRunner
{
private Type _type;
private string _typeName;
Func<object[], object> _delegate;
[GlobalSetup]
public void Setup()
{
_type = typeof(MultipleConstructors);
_typeName = _type.AssemblyQualifiedName;
_delegate = Initializer.Create(_typeName);
}
[Benchmark]
public object Reflection_CreateInstance() => Activator.CreateInstance(_type);
[Benchmark]
public object DynamicGenerator_CreateInstance() => _delegate(null);
[Benchmark]
public object Static_CreateInstance() => new MultipleConstructors();
}
public class AbstractTestMethodRunner
{
protected Type _type;
protected string _typeName;
protected InvocationTarget _target;
protected string _methodName;
protected MethodInfo _methodInfo;
protected Type[] _argumentTypes;
protected object[] _arguments;
public virtual void Setup()
{
_type = typeof(InvocationTarget);
_typeName = _type.AssemblyQualifiedName;
_target = new InvocationTarget();
}
}
[MemoryDiagnoser]
public class TestActionRunner : AbstractTestMethodRunner
{
private Action<object, object[]> _delegate;
[GlobalSetup]
public override void Setup()
{
base.Setup();
_methodName = nameof(InvocationTarget.Action);
_methodInfo = _type.GetMethod(_methodName);
_argumentTypes = new[] { typeof(string), typeof(int) };
_arguments = new object[] { "Duong", 32 };
_delegate = Invocator.CreateAction(_typeName, _methodName, _argumentTypes);
}
[Benchmark]
public void Reflection_CallAction() => _methodInfo.Invoke(_target, _arguments);
[Benchmark]
public void DynamicGenerator_CallAction() => _delegate(_target, _arguments);
[Benchmark]
public void Static_CallAction() => _target.Action("Duong", 32);
}
[MemoryDiagnoser]
public class TestFuncRunner : AbstractTestMethodRunner
{
private Func<object, object[], object> _delegate;
[GlobalSetup]
public override void Setup()
{
base.Setup();
_methodName = nameof(InvocationTarget.FuncHandleVal);
_methodInfo = _type.GetMethod(_methodName);
_argumentTypes = new[] { typeof(int), typeof(int) };
_arguments = new object[] { 17, 10 };
_delegate = Invocator.CreateFunc(_typeName, _methodName, _argumentTypes);
}
[Benchmark]
public object Reflection_CallFunc() => _methodInfo.Invoke(_target, _arguments);
[Benchmark]
public object DynamicGenerator_CallFunc() => _delegate(_target, _arguments);
[Benchmark]
public object Static_CallFunc() => _target.FuncHandleVal(17, 10);
}
public class AbstractTestStaticMethodRunner
{
protected Type _type;
protected string _typeName;
protected string _methodName;
protected MethodInfo _methodInfo;
protected Type[] _argumentTypes;
protected object[] _arguments;
public virtual void Setup()
{
_type = typeof(InvocationStaticTarget);
_typeName = _type.AssemblyQualifiedName;
}
}
[MemoryDiagnoser]
public class TestStaticActionRunner : AbstractTestStaticMethodRunner
{
private Action<object, object[]> _delegate;
[GlobalSetup]
public override void Setup()
{
base.Setup();
_methodName = nameof(InvocationStaticTarget.ActionHandleVal);
_methodInfo = _type.GetMethod(_methodName);
_argumentTypes = new[] { typeof(int), typeof(int) };
_arguments = new object[] { 17, 10 };
_delegate = Invocator.CreateAction(_typeName, _methodName, _argumentTypes);
}
[Benchmark]
public void Reflection_CallStaticAction() => _methodInfo.Invoke(null, _arguments);
[Benchmark]
public void DynamicGenerator_CallStaticAction() => _delegate(null, _arguments);
[Benchmark]
public void Static_CallStaticAction() => InvocationStaticTarget.ActionHandleVal(17, 10);
}
[MemoryDiagnoser]
public class TestStaticFuncRunner : AbstractTestStaticMethodRunner
{
private Func<object, object[], object> _delegate;
[GlobalSetup]
public override void Setup()
{
base.Setup();
_methodName = nameof(InvocationStaticTarget.FuncHandleVal);
_methodInfo = _type.GetMethod(_methodName);
_argumentTypes = new[] { typeof(int), typeof(int) };
_arguments = new object[] { 17, 10 };
_delegate = Invocator.CreateFunc(_typeName, _methodName, _argumentTypes);
}
[Benchmark]
public object Reflection_CallStaticFunc() => _methodInfo.Invoke(null, _arguments);
[Benchmark]
public object DynamicGenerator_CallStaticFunc() => _delegate(null, _arguments);
[Benchmark]
public object Static_CallStaticFunc() => InvocationStaticTarget.FuncHandleVal(17, 10);
}
}