Skip to content

Commit 610bf06

Browse files
feat: generate benchmarks via t4 (#105)
Co-authored-by: Glenn <[email protected]>
1 parent 9ba4791 commit 610bf06

6 files changed

+757
-218
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,63 @@
1-
// Copyright (c) 2019-2020 ReactiveUI Association Incorporated. All rights reserved.
1+
// Copyright (c) 2019-2020 ReactiveUI Association Incorporated. All rights reserved.
22
// ReactiveUI Association Incorporated licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for full license information.
44

5+
using System;
56
using BenchmarkDotNet.Attributes;
67
using BenchmarkDotNet.Configs;
78
using BenchmarkDotNet.Jobs;
9+
810
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;
1411
using UI = ReactiveUI.PropertyBindingMixins;
15-
16-
#nullable disable
12+
using Old = ReactiveMarbles.PropertyChanged.Benchmarks.Legacy.BindExtensions;
13+
using New = ReactiveMarbles.PropertyChanged.BindExtensions;
1714

1815
namespace ReactiveMarbles.PropertyChanged.Benchmarks
1916
{
2017
/// <summary>
21-
/// Benchmarks for the property changed.
18+
/// Benchmarks for binding.
2219
/// </summary>
2320
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
2421
[MemoryDiagnoser]
2522
[MarkdownExporterAttribute.GitHub]
2623
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
2724
public class BindBenchmarks
2825
{
26+
private TestClass _from;
27+
private TestClass _to;
28+
private IDisposable _binding;
2929

3030
/// <summary>
31-
/// The parameters for the tests.
31+
/// The number mutations to perform.
3232
/// </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)]
4134
public int Changes;
42-
43-
private TestClass _from;
44-
private TestClass _to;
45-
private IDisposable _binding;
46-
private Expression<Func<TestClass, int>> _propertyExpression;
4735

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()
5045
{
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);
5448
}
5549

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()
5952
{
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;
6361
for (var i = 0; i < Changes; ++i)
6462
{
6563
var a = i % d2;
@@ -68,80 +66,208 @@ public void NoBind()
6866
}
6967
}
7068

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")]
72118
[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()
74165
{
75-
using var binding = UI.Bind(_from, _to, _propertyExpression, _propertyExpression);
166+
PerformMutations(1);
167+
}
76168

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);
78174
}
79175

80-
[BenchmarkCategory("Bind and Change")]
176+
[BenchmarkCategory("Change Depth 1")]
81177
[Benchmark]
82-
public void BindAndChangeOld()
178+
public void Change_Depth1_New()
83179
{
84-
using var binding = Old.Bind(_from, _to, _propertyExpression, _propertyExpression);
85-
86-
NoBind();
180+
PerformMutations(1);
87181
}
88182

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")]
90205
[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()
92213
{
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+
}
94217

95-
NoBind();
218+
[BenchmarkCategory("Change Depth 2")]
219+
[Benchmark]
220+
public void Change_Depth2_New()
221+
{
222+
PerformMutations(2);
96223
}
97224

98-
[GlobalSetup(Target = nameof(ChangeUI))]
99-
public void ChangUISetup()
225+
[GlobalSetup(Target = "Change_Depth3_UI")]
226+
public void Change_Depth3_UISetup()
100227
{
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);
103230
}
104231

105-
[BenchmarkCategory("Change")]
232+
[BenchmarkCategory("Change Depth 3")]
106233
[Benchmark(Baseline = true)]
107-
public void ChangeUI()
234+
public void Change_Depth3_UI()
108235
{
109-
NoBind();
236+
PerformMutations(3);
110237
}
111238

112-
[GlobalSetup(Target = nameof(ChangeOld))]
113-
public void ChangOldSetup()
239+
[GlobalSetup(Target = "Change_Depth3_Old")]
240+
public void Change_Depth3_OldSetup()
114241
{
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);
117244
}
118245

119-
[BenchmarkCategory("Change")]
246+
[BenchmarkCategory("Change Depth 3")]
120247
[Benchmark]
121-
public void ChangeOld()
248+
public void Change_Depth3_Old()
122249
{
123-
NoBind();
250+
PerformMutations(3);
124251
}
125252

126-
[GlobalSetup(Target = nameof(ChangeNew))]
127-
public void ChangNewSetup()
253+
[GlobalSetup(Target = "Change_Depth3_New")]
254+
public void Change_Depth3_NewSetup()
128255
{
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);
131258
}
132259

133-
[BenchmarkCategory("Change")]
260+
[BenchmarkCategory("Change Depth 3")]
134261
[Benchmark]
135-
public void ChangeNew()
262+
public void Change_Depth3_New()
136263
{
137-
NoBind();
264+
PerformMutations(3);
138265
}
139266

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" })]
141268
public void GlobalCleanup()
142269
{
143-
// Disposing logic
144-
_binding!.Dispose();
270+
_binding.Dispose();
145271
}
146272
}
147-
}
273+
}

0 commit comments

Comments
 (0)