Skip to content

Commit e1a6827

Browse files
committed
Add benchmarks
1 parent 8c0a5c1 commit e1a6827

9 files changed

+548
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ TestResults/
2525
docs/_site/
2626
docs/Gemfile.lock
2727
docs/_build/
28+
src/FluentValidation.Tests.Benchmarks/BenchmarkDotNet.Artifacts/

FluentValidation.sln

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentValidation.Tests.AspN
1919
EndProject
2020
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentValidation.DependencyInjectionExtensions", "src\FluentValidation.DependencyInjectionExtensions\FluentValidation.DependencyInjectionExtensions.csproj", "{A6E791BA-E0AA-4852-95C3-255B3D62980F}"
2121
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentValidation.Tests.Benchmarks", "src\FluentValidation.Tests.Benchmarks\FluentValidation.Tests.Benchmarks.csproj", "{BC2CC9AB-C044-420C-9AD3-A4854F8338F0}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{A6E791BA-E0AA-4852-95C3-255B3D62980F}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{A6E791BA-E0AA-4852-95C3-255B3D62980F}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{A6E791BA-E0AA-4852-95C3-255B3D62980F}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{BC2CC9AB-C044-420C-9AD3-A4854F8338F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{BC2CC9AB-C044-420C-9AD3-A4854F8338F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{BC2CC9AB-C044-420C-9AD3-A4854F8338F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{BC2CC9AB-C044-420C-9AD3-A4854F8338F0}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#region License
2+
3+
// Copyright (c) .NET Foundation and contributors.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
// The latest version of this file can be found at https://github.com/FluentValidation/FluentValidation
18+
19+
#endregion
20+
21+
namespace FluentValidation.Tests.Benchmarks {
22+
using System;
23+
using System.Collections.Generic;
24+
using System.Linq;
25+
using Bogus;
26+
using Bogus.Extensions;
27+
28+
public static class DataSet {
29+
static DataSet() {
30+
void SetupFullModelManyErrorsFaker() {
31+
var nestedModelManyErrorsFaker = new Faker<NestedModel>()
32+
.RuleFor(m => m.Number1, m => m.Random.Int(0, 5))
33+
.RuleFor(m => m.Number2, m => m.Random.Int(0, 15))
34+
.RuleFor(m => m.Text1, m => m.Lorem.Word().OrNull(m, 0.1f))
35+
.RuleFor(m => m.Text2, m => m.Lorem.Word().OrNull(m, 0.15f))
36+
.RuleFor(m => m.SuperNumber1, m => m.Random.Number(0, 20).OrNull(m, 0.20f))
37+
.RuleFor(m => m.SuperNumber2, m => m.Random.Number(0, 20).OrNull(m, 0.25f));
38+
39+
FullModelManyErrorsFaker = new Faker<FullModel>()
40+
.RuleFor(m => m.Text1, m => m.Lorem.Word().OrNull(m, 0.1f))
41+
.RuleFor(m => m.Text2, m => m.Lorem.Word().OrNull(m, 0.15f))
42+
.RuleFor(m => m.Text3, m => m.Lorem.Word().OrNull(m, 0.2f))
43+
.RuleFor(m => m.Text4, m => m.Lorem.Word().OrNull(m, 0.25f))
44+
.RuleFor(m => m.Text5, m => m.Lorem.Word().OrNull(m, 0.3f))
45+
.RuleFor(m => m.Number1, m => m.Random.Int(0, 5))
46+
.RuleFor(m => m.Number2, m => m.Random.Int(0, 10))
47+
.RuleFor(m => m.Number3, m => m.Random.Int(0, 15))
48+
.RuleFor(m => m.Number4, m => m.Random.Int(0, 20))
49+
.RuleFor(m => m.Number5, m => m.Random.Int(0, 25))
50+
.RuleFor(m => m.SuperNumber1, m => m.Random.Decimal(0, 20).OrNull(m, 0.20f))
51+
.RuleFor(m => m.SuperNumber2, m => m.Random.Decimal(0, 20).OrNull(m, 0.25f))
52+
.RuleFor(m => m.SuperNumber2, m => m.Random.Decimal(0, 20).OrNull(m, 0.30f))
53+
.RuleFor(m => m.NestedModel1, m => nestedModelManyErrorsFaker.Generate())
54+
.RuleFor(m => m.NestedModel2, m => nestedModelManyErrorsFaker.Generate())
55+
.RuleFor(m => m.ModelCollection, m => nestedModelManyErrorsFaker.GenerateBetween(0, 20).ToList().OrNull(m, 0.7f))
56+
.RuleFor(m => m.StructCollection, m => Enumerable.Range(1, m.Random.Int(1, 20)).Select(_ => m.Random.Number(0, 20)).ToList().OrNull(m, 0.7f));
57+
}
58+
59+
void SetupFullModelNoErrorsFaker() {
60+
var nestedModelNoErrorsFaker = new Faker<NestedModel>()
61+
.RuleFor(m => m.Number1, m => m.Random.Int(0, 9))
62+
.RuleFor(m => m.Number2, m => m.Random.Int(0, 9))
63+
.RuleFor(m => m.Text1, m => m.Lorem.Word() + "a")
64+
.RuleFor(m => m.Text2, m => m.Lorem.Word() + "b")
65+
.RuleFor(m => m.SuperNumber1, m => m.Random.Number(0, 9))
66+
.RuleFor(m => m.SuperNumber2, m => m.Random.Number(0, 9));
67+
68+
FullModelNoErrorsFaker = new Faker<FullModel>()
69+
.RuleFor(m => m.Text1, m => m.Lorem.Word() + "a")
70+
.RuleFor(m => m.Text2, m => m.Lorem.Word() + "b")
71+
.RuleFor(m => m.Text3, m => m.Lorem.Word() + "c")
72+
.RuleFor(m => m.Text4, m => m.Lorem.Word() + "d")
73+
.RuleFor(m => m.Text5, m => m.Lorem.Word() + "e")
74+
.RuleFor(m => m.Number1, m => m.Random.Int(0, 9))
75+
.RuleFor(m => m.Number2, m => m.Random.Int(0, 9))
76+
.RuleFor(m => m.Number3, m => m.Random.Int(0, 9))
77+
.RuleFor(m => m.Number4, m => m.Random.Int(0, 9))
78+
.RuleFor(m => m.Number5, m => m.Random.Int(0, 9))
79+
.RuleFor(m => m.SuperNumber1, m => m.Random.Decimal(0, 9))
80+
.RuleFor(m => m.SuperNumber2, m => m.Random.Decimal(0, 9))
81+
.RuleFor(m => m.SuperNumber3, m => m.Random.Decimal(0, 9))
82+
.RuleFor(m => m.NestedModel1, m => nestedModelNoErrorsFaker.Generate())
83+
.RuleFor(m => m.NestedModel2, m => nestedModelNoErrorsFaker.Generate())
84+
.RuleFor(m => m.ModelCollection, m => nestedModelNoErrorsFaker.GenerateBetween(0, 9).ToList())
85+
.RuleFor(m => m.StructCollection, m => Enumerable.Range(1, m.Random.Int(1, 9)).Select(_ => m.Random.Number(0, 9)).ToList());
86+
}
87+
88+
void SetupFullModelHalfErrorsFaker() {
89+
var nestedModelsHalfErrorsFaker = new Faker<NestedModel>()
90+
.RuleFor(m => m.Number1, m => m.Random.Int(0, 9))
91+
.RuleFor(m => m.Number2, m => m.Random.Int(0, 9))
92+
.RuleFor(m => m.Text1, m => string.Join(" ", m.Lorem.Words(20)))
93+
.RuleFor(m => m.Text2, m => string.Join("b", m.Lorem.Words(15)))
94+
.RuleFor(m => m.SuperNumber1, m => m.Random.Number(0, 10))
95+
.RuleFor(m => m.SuperNumber2, m => m.Random.Number(0, 9).OrNull(m, 0.01f));
96+
97+
FullModelHalfErrorsFaker = new Faker<FullModel>()
98+
.RuleFor(m => m.Text1, m => string.Join("a", m.Lorem.Words(10)))
99+
.RuleFor(m => m.Text2, m => string.Join("b", m.Lorem.Words(10)))
100+
.RuleFor(m => m.Text3, m => string.Join("c", m.Lorem.Words(10)))
101+
.RuleFor(m => m.Text4, m => string.Join("d", m.Lorem.Words(10)))
102+
.RuleFor(m => m.Text5, m => string.Join(" ", m.Lorem.Words(20)))
103+
.RuleFor(m => m.Number1, m => m.Random.Int(0, 9))
104+
.RuleFor(m => m.Number2, m => m.Random.Int(0, 9))
105+
.RuleFor(m => m.Number3, m => m.Random.Int(0, 9))
106+
.RuleFor(m => m.Number4, m => m.Random.Int(0, 9))
107+
.RuleFor(m => m.Number5, m => m.Random.Int(0, 10))
108+
.RuleFor(m => m.SuperNumber1, m => m.Random.Decimal(0, 9))
109+
.RuleFor(m => m.SuperNumber2, m => m.Random.Decimal(0, 9))
110+
.RuleFor(m => m.SuperNumber3, m => m.Random.Decimal(0, 10).OrNull(m, 0.01f))
111+
.RuleFor(m => m.NestedModel1, m => nestedModelsHalfErrorsFaker.Generate())
112+
.RuleFor(m => m.NestedModel2, m => nestedModelsHalfErrorsFaker.Generate())
113+
.RuleFor(m => m.ModelCollection, m => nestedModelsHalfErrorsFaker.GenerateBetween(0, 9).ToList())
114+
.RuleFor(m => m.StructCollection, m => Enumerable.Range(1, m.Random.Int(1, 11)).Select(_ => m.Random.Number(0, 9)).ToList());
115+
}
116+
117+
SetupFullModelHalfErrorsFaker();
118+
SetupFullModelManyErrorsFaker();
119+
SetupFullModelNoErrorsFaker();
120+
121+
Size = 10_000;
122+
123+
Randomizer.Seed = new Random(666);
124+
125+
ManyErrorsDataSet = FullModelManyErrorsFaker.GenerateLazy(Size).ToList();
126+
HalfErrorsDataSet = FullModelHalfErrorsFaker.GenerateLazy(Size).ToList();
127+
NoErrorsDataSet = FullModelNoErrorsFaker.GenerateLazy(Size).ToList();
128+
129+
DataSets = new Dictionary<string, IReadOnlyList<FullModel>>(3) {
130+
["ManyErrors"] = ManyErrorsDataSet,
131+
["HalfErrors"] = HalfErrorsDataSet,
132+
["NoErrors"] = NoErrorsDataSet
133+
};
134+
}
135+
136+
public static int Size { get; }
137+
138+
public static IReadOnlyList<FullModel> ManyErrorsDataSet { get; }
139+
140+
public static IReadOnlyList<FullModel> HalfErrorsDataSet { get; }
141+
142+
public static IReadOnlyList<FullModel> NoErrorsDataSet { get; }
143+
144+
public static IReadOnlyDictionary<string, IReadOnlyList<FullModel>> DataSets { get; }
145+
146+
public static Faker<FullModel> FullModelManyErrorsFaker { get; private set; }
147+
148+
public static Faker<FullModel> FullModelNoErrorsFaker { get; private set; }
149+
150+
public static Faker<FullModel> FullModelHalfErrorsFaker { get; private set; }
151+
}
152+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#region License
2+
3+
// Copyright (c) .NET Foundation and contributors.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
// The latest version of this file can be found at https://github.com/FluentValidation/FluentValidation
18+
19+
#endregion
20+
21+
namespace FluentValidation.Tests.Benchmarks {
22+
using System.Collections.Generic;
23+
using System.Linq;
24+
using BenchmarkDotNet.Attributes;
25+
26+
[MemoryDiagnoser]
27+
public class EngineOnlyBenchmark {
28+
private IReadOnlyList<VoidModel> _noLogicModels;
29+
30+
private NoLogicModelSingleRuleValidator _fluentValidationSingleRuleValidator;
31+
32+
private NoLogicModelTenRulesValidator _fluentValidationTenRulesValidator;
33+
34+
public class VoidModel {
35+
public object Member { get; set; }
36+
}
37+
38+
public class NoLogicModelSingleRuleValidator : AbstractValidator<VoidModel> {
39+
public NoLogicModelSingleRuleValidator() {
40+
RuleFor(m => m.Member).Must(o => true);
41+
}
42+
}
43+
44+
public class NoLogicModelTenRulesValidator : AbstractValidator<VoidModel> {
45+
public NoLogicModelTenRulesValidator() {
46+
RuleFor(m => m.Member).Must(o => true);
47+
RuleFor(m => m.Member).Must(o => true);
48+
RuleFor(m => m.Member).Must(o => true);
49+
RuleFor(m => m.Member).Must(o => true);
50+
RuleFor(m => m.Member).Must(o => true);
51+
RuleFor(m => m.Member).Must(o => true);
52+
RuleFor(m => m.Member).Must(o => true);
53+
RuleFor(m => m.Member).Must(o => true);
54+
RuleFor(m => m.Member).Must(o => true);
55+
RuleFor(m => m.Member).Must(o => true);
56+
}
57+
}
58+
59+
[Params(10000)]
60+
public int N { get; set; }
61+
62+
[GlobalSetup]
63+
public void GlobalSetup() {
64+
_fluentValidationSingleRuleValidator = new NoLogicModelSingleRuleValidator();
65+
_fluentValidationTenRulesValidator = new NoLogicModelTenRulesValidator();
66+
_noLogicModels = Enumerable.Range(0, N).Select(m => new VoidModel() {Member = new object()}).ToList();
67+
}
68+
69+
[Benchmark]
70+
public object Validate_SingleRule() {
71+
object t = null;
72+
73+
for (var i = 0; i < N; ++i) {
74+
t = _fluentValidationSingleRuleValidator.Validate(_noLogicModels[i]);
75+
}
76+
77+
return t;
78+
}
79+
80+
[Benchmark]
81+
public object Validate_TenRules() {
82+
object t = null;
83+
84+
for (var i = 0; i < N; ++i) {
85+
t = _fluentValidationTenRulesValidator.Validate(_noLogicModels[i]);
86+
}
87+
88+
return t;
89+
}
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
11+
<PackageReference Include="Bogus" Version="30.0.2" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\FluentValidation\FluentValidation.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#region License
2+
3+
// Copyright (c) .NET Foundation and contributors.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
// The latest version of this file can be found at https://github.com/FluentValidation/FluentValidation
18+
19+
#endregion
20+
21+
namespace FluentValidation.Tests.Benchmarks {
22+
using BenchmarkDotNet.Attributes;
23+
24+
[MemoryDiagnoser]
25+
public class InitializationBenchmark {
26+
[Benchmark]
27+
public object Initialization_using_cache() {
28+
return new FullModelValidator();
29+
}
30+
31+
[Benchmark(Baseline = true)]
32+
public object Initialization_without_cache() {
33+
ValidatorOptions.Global.DisableAccessorCache = true;
34+
var validator = new FullModelValidator();
35+
ValidatorOptions.Global.DisableAccessorCache = false;
36+
return validator;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)