-
Notifications
You must be signed in to change notification settings - Fork 107
/
EventPerf.cs
194 lines (172 loc) · 6.23 KB
/
EventPerf.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
using BenchmarkComponent;
using BenchmarkDotNet.Attributes;
using System;
namespace Benchmarks
{
public class ManagedEvents : IEvents
{
public event EventHandler<double> DoublePropertyChanged;
public event EventHandler<int> IntPropertyChanged;
public void RaiseDoubleChanged()
{
DoublePropertyChanged.Invoke(this, 2.2);
}
public void RaiseIntChanged()
{
IntPropertyChanged.Invoke(this, 4);
}
}
[MemoryDiagnoser]
public class EventPerf
{
ClassWithMarshalingRoutines instance;
int z2;
ClassWithMarshalingRoutines instance2;
int z4;
ManagedEvents events;
EventOperations operations;
[GlobalSetup]
public void Setup()
{
instance = new ClassWithMarshalingRoutines();
System.EventHandler<int> s = (object sender, int value) => z2 = value;
instance.IntPropertyChanged += s;
instance2 = new ClassWithMarshalingRoutines();
System.EventHandler<int> s2 = (object sender, int value) =>
{
if (sender == this)
z4 = value;
else
z4 = value * 3;
};
instance2.IntPropertyChanged += s2;
events = new ManagedEvents();
operations = new EventOperations(events);
operations.AddIntEvent();
}
[Benchmark]
public object IntEventOverhead()
{
ClassWithMarshalingRoutines instance = new ClassWithMarshalingRoutines();
int z;
System.EventHandler<int> s = (object sender, int value) => z = value;
return instance;
GC.KeepAlive(s);
}
[Benchmark]
public object AddIntEventToNewEventSource()
{
ClassWithMarshalingRoutines instance = new ClassWithMarshalingRoutines();
int z;
System.EventHandler<int> s = (object sender, int value) => z = value;
instance.IntPropertyChanged += s;
return instance;
}
[Benchmark]
public object AddMultipleEventsToNewEventSource()
{
ClassWithMarshalingRoutines instance = new ClassWithMarshalingRoutines();
int z;
double y;
System.EventHandler<int> s = (object sender, int value) => z = value;
instance.IntPropertyChanged += s;
System.EventHandler<double> t = (object sender, double value) => y = value;
instance.DoublePropertyChanged += t;
return instance;
}
[Benchmark]
public object AddAndInvokeIntEventOnNewEventSource()
{
ClassWithMarshalingRoutines instance = new ClassWithMarshalingRoutines();
int z;
System.EventHandler<int> s = (object sender, int value) => z = value;
instance.IntPropertyChanged += s;
instance.RaiseIntChanged();
return instance;
}
[Benchmark]
public object AddAndInvokeMultipleIntEventsToSameEventSource()
{
ClassWithMarshalingRoutines instance = new ClassWithMarshalingRoutines();
int z;
System.EventHandler<int> s = (object sender, int value) => z = value;
System.EventHandler<int> t = (object sender, int value) => z = value * 2;
System.EventHandler<int> u = (object sender, int value) => z = value * 3;
instance.IntPropertyChanged += s;
instance.IntPropertyChanged += t;
instance.IntPropertyChanged += u;
instance.RaiseIntChanged();
return instance;
}
[Benchmark]
public int InvokeIntEvent()
{
instance.RaiseIntChanged();
return z2;
}
[Benchmark]
public int InvokeIntEventWithSenderCheck()
{
instance2.RaiseIntChanged();
return z4;
}
[Benchmark]
public object AddAndRemoveIntEventOnNewEventSource()
{
ClassWithMarshalingRoutines instance = new ClassWithMarshalingRoutines();
int z;
System.EventHandler<int> s = (object sender, int value) => z = value;
instance.IntPropertyChanged += s;
instance.IntPropertyChanged -= s;
return instance;
GC.KeepAlive(s);
}
[Benchmark]
public object NativeIntEventOverhead()
{
ManagedEvents events = new ManagedEvents();
EventOperations operations = new EventOperations(events);
return operations;
}
[Benchmark]
public object AddNativeIntEventToNewEventSource()
{
ManagedEvents events = new ManagedEvents();
EventOperations operations = new EventOperations(events);
operations.AddIntEvent();
return operations;
}
[Benchmark]
public object AddMultipleNativeEventsToNewEventSource()
{
ManagedEvents events = new ManagedEvents();
EventOperations operations = new EventOperations(events);
operations.AddIntEvent();
operations.AddDoubleEvent();
return operations;
}
[Benchmark]
public object AddAndInvokeNativeIntEventOnNewEventSource()
{
ManagedEvents events = new ManagedEvents();
EventOperations operations = new EventOperations(events);
operations.AddIntEvent();
operations.FireIntEvent();
return operations;
}
[Benchmark]
public void InvokeNativeIntEvent()
{
operations.FireIntEvent();
}
[Benchmark]
public object AddAndRemoveNativeIntEventOnNewEventSource()
{
ManagedEvents events = new ManagedEvents();
EventOperations operations = new EventOperations(events);
operations.AddIntEvent();
operations.RemoveIntEvent();
return operations;
}
}
}