-
Notifications
You must be signed in to change notification settings - Fork 5
/
EfficientInvokerTests.cs
289 lines (232 loc) · 9 KB
/
EfficientInvokerTests.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace Tact.Tests.Reflection
{
public class EfficientInvokerTests
{
public const int Iterations = 1000000;
private static readonly object[] Args = { 1, true };
private readonly TestClass _obj = new TestClass();
private readonly ITestOutputHelper _output;
public EfficientInvokerTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void NoResultDelegate()
{
var count = 0;
Action<int> func = c => Interlocked.Add(ref count, c);
var invoker = func.GetInvoker();
invoker.Invoke(func, 2);
invoker.Invoke(func, 3);
Assert.Equal(5, count);
}
[Fact]
public void DelegateComparison()
{
var count = 0;
Func<int, int, bool> func = (a, b) =>
{
Interlocked.Increment(ref count);
return a == b;
};
var ticks0 = DelegateDynamicInvoke(func);
Assert.Equal(Iterations, count);
var ticks1 = DelegateEfficientInvoke(func);
Assert.Equal(Iterations * 2, count);
Assert.True(ticks1 * 10 < ticks0);
}
private long DelegateDynamicInvoke(Delegate d)
{
var sw0 = Stopwatch.StartNew();
d.DynamicInvoke(1, 1);
sw0.Stop();
_output.WriteLine($"DelegateDynamicInvoke - First - MS: {sw0.ElapsedMilliseconds}");
var sw1 = Stopwatch.StartNew();
for (var i = 1; i < Iterations; i++)
{
d.DynamicInvoke(i, i);
}
sw1.Stop();
_output.WriteLine($"DelegateDynamicInvoke - Rest - MS: {sw1.ElapsedMilliseconds}");
return sw1.ElapsedTicks;
}
private long DelegateEfficientInvoke(Delegate d)
{
var sw0 = Stopwatch.StartNew();
var x = d.GetInvoker();
x.Invoke(d, 1, 1);
sw0.Stop();
_output.WriteLine($"DelegateEfficientInvoke - First - MS: {sw0.ElapsedMilliseconds}");
var sw1 = Stopwatch.StartNew();
for (var i = 1; i < Iterations; i++)
{
x.Invoke(d, i, i);
}
sw1.Stop();
_output.WriteLine($"DelegateEfficientInvoke - Rest - MS: {sw1.ElapsedMilliseconds}");
return sw1.ElapsedTicks;
}
[Fact]
public void MethodComparison()
{
var ticks0 = MethodInfoInvoke();
Assert.Equal(Iterations, _obj.Count);
var ticks1 = CachedMethodInfoInvoke();
Assert.Equal(Iterations * 2, _obj.Count);
var ticks2 = MethodEfficientInvoker();
Assert.Equal(Iterations * 3, _obj.Count);
Assert.True(ticks1 < ticks0);
Assert.True(ticks2 * 8 < ticks0);
}
private long MethodInfoInvoke()
{
var sw0 = Stopwatch.StartNew();
_obj.GetType().GetTypeInfo().GetMethod("TestMethod").Invoke(_obj, Args);
sw0.Stop();
_output.WriteLine($"MethodInfoInvoke - First - MS: {sw0.ElapsedMilliseconds}");
var sw1 = Stopwatch.StartNew();
for (var i = 1; i < Iterations; i++)
{
_obj.GetType().GetTypeInfo().GetMethod("TestMethod").Invoke(_obj, Args);
}
sw1.Stop();
_output.WriteLine($"MethodInfoInvoke - Rest - MS: {sw1.ElapsedMilliseconds}");
return sw1.ElapsedTicks;
}
private long CachedMethodInfoInvoke()
{
var map = new ConcurrentDictionary<Type, MethodInfo>();
var sw0 = Stopwatch.StartNew();
map.GetOrAdd(_obj.GetType(), type => type.GetTypeInfo().GetMethod("TestMethod")).Invoke(_obj, Args);
sw0.Stop();
_output.WriteLine($"CachedMethodInfoInvoke - First - MS: {sw0.ElapsedMilliseconds}");
var sw1 = Stopwatch.StartNew();
for (var i = 1; i < Iterations; i++)
{
map.GetOrAdd(_obj.GetType(), type => type.GetTypeInfo().GetMethod("TestMethod")).Invoke(_obj, Args);
}
sw1.Stop();
_output.WriteLine($"CachedMethodInfoInvoke - Rest - MS: {sw1.ElapsedMilliseconds}");
return sw1.ElapsedTicks;
}
private long MethodEfficientInvoker()
{
var sw0 = Stopwatch.StartNew();
var x = _obj.GetType().GetMethodInvoker("TestMethod");
x.Invoke(_obj, Args);
sw0.Stop();
_output.WriteLine($"MethodEfficientInvoker - First - MS: {sw0.ElapsedMilliseconds}");
var sw1 = Stopwatch.StartNew();
for (var i = 1; i < Iterations; i++)
{
x.Invoke(_obj, Args);
}
sw1.Stop();
_output.WriteLine($"MethodEfficientInvoker - Rest - MS: {sw1.ElapsedMilliseconds}");
return sw1.ElapsedTicks;
}
[Fact]
public void PropertyComparison()
{
var ticks0 = PropertyInfoInvoke();
Assert.Equal(Iterations, _obj.Count);
var ticks1 = CachedPropertyInfoInvoke();
Assert.Equal(Iterations * 2, _obj.Count);
var ticks2 = PropertyEfficientInvoker();
Assert.Equal(Iterations * 3, _obj.Count);
Assert.True(ticks1 < ticks0);
Assert.True(ticks2 * 6 < ticks0);
}
private long PropertyInfoInvoke()
{
var sw0 = Stopwatch.StartNew();
var a = _obj.GetType().GetRuntimeProperty("TestProperty").GetValue(_obj);
sw0.Stop();
Assert.Equal('a', a);
_output.WriteLine($"PropertyInfoInvoke - First - MS: {sw0.ElapsedMilliseconds}");
var sw1 = Stopwatch.StartNew();
for (var i = 1; i < Iterations; i++)
{
_obj.GetType().GetRuntimeProperty("TestProperty").GetValue(_obj);
}
sw1.Stop();
_output.WriteLine($"PropertyInfoInvoke - Rest - MS: {sw1.ElapsedMilliseconds}");
return sw1.ElapsedTicks;
}
private long CachedPropertyInfoInvoke()
{
var map = new ConcurrentDictionary<Type, PropertyInfo>();
var sw0 = Stopwatch.StartNew();
var a = map.GetOrAdd(_obj.GetType(), type => type.GetRuntimeProperty("TestProperty")).GetValue(_obj);
sw0.Stop();
Assert.Equal('a', a);
_output.WriteLine($"CachedPropertyInfoInvoke - First - MS: {sw0.ElapsedMilliseconds}");
var sw1 = Stopwatch.StartNew();
for (var i = 1; i < Iterations; i++)
{
map.GetOrAdd(_obj.GetType(), type => type.GetRuntimeProperty("TestProperty")).GetValue(_obj);
}
sw1.Stop();
_output.WriteLine($"CachedPropertyInfoInvoke - Rest - MS: {sw1.ElapsedMilliseconds}");
return sw1.ElapsedTicks;
}
private long PropertyEfficientInvoker()
{
var sw0 = Stopwatch.StartNew();
var x = _obj.GetType().GetPropertyInvoker("TestProperty");
x.Invoke(_obj);
sw0.Stop();
_output.WriteLine($"PropertyEfficientInvoker - First - MS: {sw0.ElapsedMilliseconds}");
var sw1 = Stopwatch.StartNew();
for (var i = 1; i < Iterations; i++)
{
x.Invoke(_obj);
}
sw1.Stop();
_output.WriteLine($"PropertyEfficientInvoker - Rest - MS: {sw1.ElapsedMilliseconds}");
return sw1.ElapsedTicks;
}
[Fact]
public async Task InvokeAsync()
{
var invoker = _obj.GetType().GetMethodInvoker("TestMethodAsync");
var result1 = await invoker.InvokeAsync(_obj, 1).ConfigureAwait(false);
var result2 = await invoker.InvokeAsync(_obj, 2).ConfigureAwait(false);
Assert.Equal(false, result1);
Assert.Equal(true, result2);
}
private class TestClass
{
public int Count;
// ReSharper disable once UnusedMember.Local
public int TestMethod(int i, bool b)
{
Interlocked.Increment(ref Count);
return i + (b ? 1 : 2);
}
// ReSharper disable once UnusedMember.Local
public async Task<bool> TestMethodAsync(int i)
{
await Task.Delay(16).ConfigureAwait(false);
return i % 2 == 0;
}
// ReSharper disable once UnusedMember.Local
public char TestProperty
{
get
{
Interlocked.Increment(ref Count);
return 'a';
}
}
}
}
}