1
- // Copyright (c) 2019-2020 ReactiveUI Association Incorporated. All rights reserved.
1
+ // Copyright (c) 2019-2020 ReactiveUI Association Incorporated. All rights reserved.
2
2
// ReactiveUI Association Incorporated licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for full license information.
4
4
5
+ using System ;
5
6
using BenchmarkDotNet . Attributes ;
6
7
using BenchmarkDotNet . Configs ;
7
8
using BenchmarkDotNet . Jobs ;
9
+
8
10
using ReactiveMarbles . PropertyChanged . Benchmarks . Moqs ;
9
- using System ;
10
- using System . Diagnostics . CodeAnalysis ;
11
- using System . Linq . Expressions ;
12
- using New = ReactiveMarbles . PropertyChanged . BindExtensions ;
13
- using Old = ReactiveMarbles . PropertyChanged . Benchmarks . Legacy . BindExtensions ;
14
11
using UI = ReactiveUI . PropertyBindingMixins ;
15
-
16
- #nullable disable
12
+ using Old = ReactiveMarbles . PropertyChanged . Benchmarks . Legacy . BindExtensions ;
13
+ using New = ReactiveMarbles . PropertyChanged . BindExtensions ;
17
14
18
15
namespace ReactiveMarbles . PropertyChanged . Benchmarks
19
16
{
20
17
/// <summary>
21
- /// Benchmarks for the property changed .
18
+ /// Benchmarks for binding .
22
19
/// </summary>
23
20
[ SimpleJob ( RuntimeMoniker . NetCoreApp31 ) ]
24
21
[ MemoryDiagnoser ]
25
22
[ MarkdownExporterAttribute . GitHub ]
26
23
[ GroupBenchmarksBy ( BenchmarkLogicalGroupRule . ByCategory ) ]
27
24
public class BindBenchmarks
28
25
{
26
+ private TestClass _from ;
27
+ private TestClass _to ;
28
+ private IDisposable _binding ;
29
29
30
30
/// <summary>
31
- /// The parameters for the tests .
31
+ /// The number mutations to perform .
32
32
/// </summary>
33
- [ SuppressMessage ( "Design" , "SA1401: field should be private" , Justification = "needed by benchmark" ) ]
34
- [ SuppressMessage ( "Design" , "CA1051: field should be private" , Justification = "needed by benchmark" ) ]
35
- [ Params ( 1 , 2 , 3 ) ]
36
- public int Depth ;
37
-
38
- [ SuppressMessage ( "Design" , "SA1401: field should be private" , Justification = "needed by benchmark" ) ]
39
- [ SuppressMessage ( "Design" , "CA1051: field should be private" , Justification = "needed by benchmark" ) ]
40
- [ Params ( 1 , 10 , 100 , 1000 ) ]
33
+ [ Params ( 1 , 10 , 100 , 1000 ) ]
41
34
public int Changes ;
42
-
43
- private TestClass _from ;
44
- private TestClass _to ;
45
- private IDisposable _binding ;
46
- private Expression < Func < TestClass , int > > _propertyExpression ;
47
35
48
- [ GlobalSetup ( Targets = new [ ] { nameof ( BindAndChangeUI ) , nameof ( BindAndChangeOld ) , nameof ( BindAndChangeNew ) , nameof ( NoBind ) } ) ]
49
- public void CommonSetup ( )
36
+ [ GlobalSetup ( Targets = new [ ] { "BindAndChange_Depth1_UI" , "BindAndChange_Depth1_Old" , "BindAndChange_Depth1_New" } ) ]
37
+ public void Depth1Setup ( )
38
+ {
39
+ _from = new TestClass ( 1 ) ;
40
+ _to = new TestClass ( 1 ) ;
41
+ }
42
+
43
+ [ GlobalSetup ( Targets = new [ ] { "BindAndChange_Depth2_UI" , "BindAndChange_Depth2_Old" , "BindAndChange_Depth2_New" } ) ]
44
+ public void Depth2Setup ( )
50
45
{
51
- _from = new TestClass ( Depth ) ;
52
- _to = new TestClass ( Depth ) ;
53
- _propertyExpression = TestClass . GetValuePropertyExpression ( Depth ) ;
46
+ _from = new TestClass ( 2 ) ;
47
+ _to = new TestClass ( 2 ) ;
54
48
}
55
49
56
- //[BenchmarkCategory("No Binding")]
57
- //[Benchmark(Baseline = true)]
58
- public void NoBind ( )
50
+ [ GlobalSetup ( Targets = new [ ] { "BindAndChange_Depth3_UI" , "BindAndChange_Depth3_Old" , "BindAndChange_Depth3_New" } ) ]
51
+ public void Depth3Setup ( )
59
52
{
60
- // We loop through the changes, alternating mutations to the source and destination
61
- // at every depth.
62
- var d2 = Depth * 2 ;
53
+ _from = new TestClass ( 3 ) ;
54
+ _to = new TestClass ( 3 ) ;
55
+ }
56
+
57
+ public void PerformMutations ( int depth )
58
+ {
59
+ // We loop through the changes, alternating mutations to the source and destination at every depth.
60
+ var d2 = depth * 2 ;
63
61
for ( var i = 0 ; i < Changes ; ++ i )
64
62
{
65
63
var a = i % d2 ;
@@ -68,80 +66,208 @@ public void NoBind()
68
66
}
69
67
}
70
68
71
- [ BenchmarkCategory ( "Bind and Change" ) ]
69
+ [ BenchmarkCategory ( "Bind and Change Depth 1" ) ]
70
+ [ Benchmark ( Baseline = true ) ]
71
+ public void BindAndChange_Depth1_UI ( )
72
+ {
73
+ using var binding = UI . Bind ( _from , _to , x => x . Value , x => x . Value ) ;
74
+ PerformMutations ( 1 ) ;
75
+ }
76
+
77
+ [ BenchmarkCategory ( "Bind and Change Depth 1" ) ]
78
+ [ Benchmark ]
79
+ public void BindAndChange_Depth1_Old ( )
80
+ {
81
+ using var binding = Old . Bind ( _from , _to , x => x . Value , x => x . Value ) ;
82
+ PerformMutations ( 1 ) ;
83
+ }
84
+
85
+ [ BenchmarkCategory ( "Bind and Change Depth 1" ) ]
86
+ [ Benchmark ]
87
+ public void BindAndChange_Depth1_New ( )
88
+ {
89
+ using var binding = New . Bind ( _from , _to , x => x . Value , x => x . Value ) ;
90
+ PerformMutations ( 1 ) ;
91
+ }
92
+
93
+ [ BenchmarkCategory ( "Bind and Change Depth 2" ) ]
94
+ [ Benchmark ( Baseline = true ) ]
95
+ public void BindAndChange_Depth2_UI ( )
96
+ {
97
+ using var binding = UI . Bind ( _from , _to , x => x . Child . Value , x => x . Child . Value ) ;
98
+ PerformMutations ( 2 ) ;
99
+ }
100
+
101
+ [ BenchmarkCategory ( "Bind and Change Depth 2" ) ]
102
+ [ Benchmark ]
103
+ public void BindAndChange_Depth2_Old ( )
104
+ {
105
+ using var binding = Old . Bind ( _from , _to , x => x . Child . Value , x => x . Child . Value ) ;
106
+ PerformMutations ( 2 ) ;
107
+ }
108
+
109
+ [ BenchmarkCategory ( "Bind and Change Depth 2" ) ]
110
+ [ Benchmark ]
111
+ public void BindAndChange_Depth2_New ( )
112
+ {
113
+ using var binding = New . Bind ( _from , _to , x => x . Child . Value , x => x . Child . Value ) ;
114
+ PerformMutations ( 2 ) ;
115
+ }
116
+
117
+ [ BenchmarkCategory ( "Bind and Change Depth 3" ) ]
72
118
[ Benchmark ( Baseline = true ) ]
73
- public void BindAndChangeUI ( )
119
+ public void BindAndChange_Depth3_UI ( )
120
+ {
121
+ using var binding = UI . Bind ( _from , _to , x => x . Child . Child . Value , x => x . Child . Child . Value ) ;
122
+ PerformMutations ( 3 ) ;
123
+ }
124
+
125
+ [ BenchmarkCategory ( "Bind and Change Depth 3" ) ]
126
+ [ Benchmark ]
127
+ public void BindAndChange_Depth3_Old ( )
128
+ {
129
+ using var binding = Old . Bind ( _from , _to , x => x . Child . Child . Value , x => x . Child . Child . Value ) ;
130
+ PerformMutations ( 3 ) ;
131
+ }
132
+
133
+ [ BenchmarkCategory ( "Bind and Change Depth 3" ) ]
134
+ [ Benchmark ]
135
+ public void BindAndChange_Depth3_New ( )
136
+ {
137
+ using var binding = New . Bind ( _from , _to , x => x . Child . Child . Value , x => x . Child . Child . Value ) ;
138
+ PerformMutations ( 3 ) ;
139
+ }
140
+
141
+ [ GlobalSetup ( Target = "Change_Depth1_UI" ) ]
142
+ public void Change_Depth1_UISetup ( )
143
+ {
144
+ Depth1Setup ( ) ;
145
+ _binding = UI . Bind ( _from , _to , x => x . Value , x => x . Value ) ;
146
+ }
147
+
148
+ [ BenchmarkCategory ( "Change Depth 1" ) ]
149
+ [ Benchmark ( Baseline = true ) ]
150
+ public void Change_Depth1_UI ( )
151
+ {
152
+ PerformMutations ( 1 ) ;
153
+ }
154
+
155
+ [ GlobalSetup ( Target = "Change_Depth1_Old" ) ]
156
+ public void Change_Depth1_OldSetup ( )
157
+ {
158
+ Depth1Setup ( ) ;
159
+ _binding = Old . Bind ( _from , _to , x => x . Value , x => x . Value ) ;
160
+ }
161
+
162
+ [ BenchmarkCategory ( "Change Depth 1" ) ]
163
+ [ Benchmark ]
164
+ public void Change_Depth1_Old ( )
74
165
{
75
- using var binding = UI . Bind ( _from , _to , _propertyExpression , _propertyExpression ) ;
166
+ PerformMutations ( 1 ) ;
167
+ }
76
168
77
- NoBind ( ) ;
169
+ [ GlobalSetup ( Target = "Change_Depth1_New" ) ]
170
+ public void Change_Depth1_NewSetup ( )
171
+ {
172
+ Depth1Setup ( ) ;
173
+ _binding = New . Bind ( _from , _to , x => x . Value , x => x . Value ) ;
78
174
}
79
175
80
- [ BenchmarkCategory ( "Bind and Change " ) ]
176
+ [ BenchmarkCategory ( "Change Depth 1 " ) ]
81
177
[ Benchmark ]
82
- public void BindAndChangeOld ( )
178
+ public void Change_Depth1_New ( )
83
179
{
84
- using var binding = Old . Bind ( _from , _to , _propertyExpression , _propertyExpression ) ;
85
-
86
- NoBind ( ) ;
180
+ PerformMutations ( 1 ) ;
87
181
}
88
182
89
- [ BenchmarkCategory ( "Bind and Change" ) ]
183
+ [ GlobalSetup ( Target = "Change_Depth2_UI" ) ]
184
+ public void Change_Depth2_UISetup ( )
185
+ {
186
+ Depth2Setup ( ) ;
187
+ _binding = UI . Bind ( _from , _to , x => x . Child . Value , x => x . Child . Value ) ;
188
+ }
189
+
190
+ [ BenchmarkCategory ( "Change Depth 2" ) ]
191
+ [ Benchmark ( Baseline = true ) ]
192
+ public void Change_Depth2_UI ( )
193
+ {
194
+ PerformMutations ( 2 ) ;
195
+ }
196
+
197
+ [ GlobalSetup ( Target = "Change_Depth2_Old" ) ]
198
+ public void Change_Depth2_OldSetup ( )
199
+ {
200
+ Depth2Setup ( ) ;
201
+ _binding = Old . Bind ( _from , _to , x => x . Child . Value , x => x . Child . Value ) ;
202
+ }
203
+
204
+ [ BenchmarkCategory ( "Change Depth 2" ) ]
90
205
[ Benchmark ]
91
- public void BindAndChangeNew ( )
206
+ public void Change_Depth2_Old ( )
207
+ {
208
+ PerformMutations ( 2 ) ;
209
+ }
210
+
211
+ [ GlobalSetup ( Target = "Change_Depth2_New" ) ]
212
+ public void Change_Depth2_NewSetup ( )
92
213
{
93
- using var binding = New . Bind ( _from , _to , _propertyExpression , _propertyExpression ) ;
214
+ Depth2Setup ( ) ;
215
+ _binding = New . Bind ( _from , _to , x => x . Child . Value , x => x . Child . Value ) ;
216
+ }
94
217
95
- NoBind ( ) ;
218
+ [ BenchmarkCategory ( "Change Depth 2" ) ]
219
+ [ Benchmark ]
220
+ public void Change_Depth2_New ( )
221
+ {
222
+ PerformMutations ( 2 ) ;
96
223
}
97
224
98
- [ GlobalSetup ( Target = nameof ( ChangeUI ) ) ]
99
- public void ChangUISetup ( )
225
+ [ GlobalSetup ( Target = "Change_Depth3_UI" ) ]
226
+ public void Change_Depth3_UISetup ( )
100
227
{
101
- CommonSetup ( ) ;
102
- _binding = UI . Bind ( _from , _to , _propertyExpression , _propertyExpression ) ;
228
+ Depth3Setup ( ) ;
229
+ _binding = UI . Bind ( _from , _to , x => x . Child . Child . Value , x => x . Child . Child . Value ) ;
103
230
}
104
231
105
- [ BenchmarkCategory ( "Change" ) ]
232
+ [ BenchmarkCategory ( "Change Depth 3 " ) ]
106
233
[ Benchmark ( Baseline = true ) ]
107
- public void ChangeUI ( )
234
+ public void Change_Depth3_UI ( )
108
235
{
109
- NoBind ( ) ;
236
+ PerformMutations ( 3 ) ;
110
237
}
111
238
112
- [ GlobalSetup ( Target = nameof ( ChangeOld ) ) ]
113
- public void ChangOldSetup ( )
239
+ [ GlobalSetup ( Target = "Change_Depth3_Old" ) ]
240
+ public void Change_Depth3_OldSetup ( )
114
241
{
115
- CommonSetup ( ) ;
116
- _binding = Old . Bind ( _from , _to , _propertyExpression , _propertyExpression ) ;
242
+ Depth3Setup ( ) ;
243
+ _binding = Old . Bind ( _from , _to , x => x . Child . Child . Value , x => x . Child . Child . Value ) ;
117
244
}
118
245
119
- [ BenchmarkCategory ( "Change" ) ]
246
+ [ BenchmarkCategory ( "Change Depth 3 " ) ]
120
247
[ Benchmark ]
121
- public void ChangeOld ( )
248
+ public void Change_Depth3_Old ( )
122
249
{
123
- NoBind ( ) ;
250
+ PerformMutations ( 3 ) ;
124
251
}
125
252
126
- [ GlobalSetup ( Target = nameof ( ChangeNew ) ) ]
127
- public void ChangNewSetup ( )
253
+ [ GlobalSetup ( Target = "Change_Depth3_New" ) ]
254
+ public void Change_Depth3_NewSetup ( )
128
255
{
129
- CommonSetup ( ) ;
130
- _binding = New . Bind ( _from , _to , _propertyExpression , _propertyExpression ) ;
256
+ Depth3Setup ( ) ;
257
+ _binding = New . Bind ( _from , _to , x => x . Child . Child . Value , x => x . Child . Child . Value ) ;
131
258
}
132
259
133
- [ BenchmarkCategory ( "Change" ) ]
260
+ [ BenchmarkCategory ( "Change Depth 3 " ) ]
134
261
[ Benchmark ]
135
- public void ChangeNew ( )
262
+ public void Change_Depth3_New ( )
136
263
{
137
- NoBind ( ) ;
264
+ PerformMutations ( 3 ) ;
138
265
}
139
266
140
- [ GlobalCleanup ( Targets = new [ ] { nameof ( ChangeUI ) , nameof ( ChangeOld ) , nameof ( ChangeNew ) } ) ]
267
+ [ GlobalCleanup ( Targets = new [ ] { "Change_Depth1_UI" , "Change_Depth1_Old" , "Change_Depth1_New" , "Change_Depth2_UI" , "Change_Depth2_Old" , "Change_Depth2_New" , "Change_Depth3_UI" , "Change_Depth3_Old" , "Change_Depth3_New" } ) ]
141
268
public void GlobalCleanup ( )
142
269
{
143
- // Disposing logic
144
- _binding ! . Dispose ( ) ;
270
+ _binding . Dispose ( ) ;
145
271
}
146
272
}
147
- }
273
+ }
0 commit comments