Skip to content

Commit

Permalink
Bump FluentValidation, refactor benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyshaykhullin committed Feb 1, 2021
1 parent 6176ca3 commit 3d8e45f
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 244 deletions.
2 changes: 1 addition & 1 deletion src/AppAny.HotChocolate.FluentValidation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup Label="Packages">
<PackageReference Include="FluentValidation" Version="9.4.0" />
<PackageReference Include="FluentValidation" Version="9.5.0" />
<PackageReference Include="HotChocolate.Execution" Version="11.0.9" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.1" />
<PackageReference Include="FairyBread" Version="4.0.0" />
<PackageReference Include="FluentChoco" Version="2.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using FairyBread;
using FluentValidation;

namespace AppAny.HotChocolate.FluentValidation.Benchmarks
{
public class TestInputValidator : AbstractValidator<TestInput>
public class TestInputValidator : AbstractValidator<TestInput>, IRequiresOwnScopeValidator
{
public TestInputValidator()
{
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ namespace AppAny.HotChocolate.FluentValidation.Benchmarks
{
internal class Program
{
private static void Main(string[] args)
private static void Main()
{
BenchmarkSwitcher
.FromAssembly(typeof(Program).Assembly)
.Run(args
//, DefaultConfig.Instance.AddDiagnoser(new EtwProfiler())
);
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).RunAllJoined();
}
}
}
94 changes: 23 additions & 71 deletions tests/AppAny.HotChocolate.FluentValidation.Benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,26 @@ Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical c
DefaultJob : .NET Core 5.0.2 (CoreCLR 5.0.220.61120, CoreFX 5.0.220.61120), X64 RyuJIT
```

## ValidationBenchmarks

| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------------------------- |----------:|----------:|----------:|------:|--------:|-------:|-------:|------:|----------:|
| RunWithoutValidation | 8.193 μs | 0.0568 μs | 0.0531 μs | 0.30 | 0.01 | 1.2512 | 0.0153 | - | 7.66 KB |
| RunWithValidation | 27.554 μs | 0.5321 μs | 0.5464 μs | 1.00 | 0.00 | 1.7395 | 0.0305 | - | 10.72 KB |
| RunWithFairyBreadValidation | 24.991 μs | 0.2246 μs | 0.2101 μs | 0.91 | 0.02 | 1.8311 | 0.0305 | - | 11.23 KB |

## ExplicitValidationBenchmarks

| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------------------------- |----------:|----------:|----------:|------:|-------:|-------:|------:|----------:|
| RunWithoutValidation | 7.879 μs | 0.0936 μs | 0.0876 μs | 0.33 | 1.2512 | 0.0153 | - | 7.66 KB |
| RunWithValidation | 24.049 μs | 0.1411 μs | 0.1320 μs | 1.00 | 1.7090 | 0.0305 | - | 10.53 KB |
| RunWithFairyBreadValidation | 24.614 μs | 0.3174 μs | 0.2813 μs | 1.02 | 1.8311 | 0.0305 | - | 11.23 KB |

## InputValidatorBenchmarks

| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------------------- |---------:|--------:|--------:|------:|-------:|-------:|------:|----------:|
| Validation | 876.5 ns | 3.24 ns | 2.88 ns | 1.00 | 0.3157 | 0.0010 | - | 1.94 KB |
| InputValidatorValidation | 968.3 ns | 1.22 ns | 1.08 ns | 1.10 | 0.3529 | - | - | 2.17 KB |

## EmptyInputsValidationBenchmarks

| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------------------------- |---------:|----------:|----------:|------:|-------:|-------:|------:|----------:|
| RunWithoutValidation | 7.592 μs | 0.0515 μs | 0.0481 μs | 0.97 | 1.2512 | 0.0229 | - | 7.64 KB |
| RunWithValidation | 7.853 μs | 0.0529 μs | 0.0469 μs | 1.00 | 1.2512 | 0.0153 | - | 7.64 KB |
| RunWithFairyBreadValidation | 8.551 μs | 0.1089 μs | 0.1019 μs | 1.09 | 1.2665 | 0.0153 | - | 7.8 KB |

## EmptyInputsExplicitValidationBenchmarks

| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------------------------- |---------:|----------:|----------:|------:|-------:|-------:|------:|----------:|
| RunWithoutValidation | 7.865 μs | 0.0535 μs | 0.0475 μs | 1.00 | 1.2512 | 0.0153 | - | 7.64 KB |
| RunWithValidation | 7.863 μs | 0.0420 μs | 0.0393 μs | 1.00 | 1.2512 | 0.0153 | - | 7.64 KB |
| RunWithFairyBreadValidation | 8.831 μs | 0.0397 μs | 0.0371 μs | 1.12 | 1.2665 | 0.0153 | - | 7.8 KB |

## NullInputsValidationBenchmarks

| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------------------------- |---------:|----------:|----------:|------:|-------:|-------:|------:|----------:|
| RunWithoutValidation | 7.735 μs | 0.0474 μs | 0.0420 μs | 0.94 | 1.2512 | 0.0153 | - | 7.65 KB |
| RunWithValidation | 8.208 μs | 0.0355 μs | 0.0314 μs | 1.00 | 1.2512 | 0.0153 | - | 7.65 KB |
| RunWithFairyBreadValidation | 8.867 μs | 0.0889 μs | 0.0831 μs | 1.08 | 1.2665 | 0.0153 | - | 7.8 KB |

## NullInputsExplicitValidationBenchmarks

| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------------------------- |---------:|----------:|----------:|------:|--------:|-------:|-------:|------:|----------:|
| RunWithoutValidation | 7.857 μs | 0.0565 μs | 0.0529 μs | 0.93 | 0.01 | 1.2512 | 0.0153 | - | 7.65 KB |
| RunWithValidation | 8.459 μs | 0.0569 μs | 0.0532 μs | 1.00 | 0.00 | 1.2512 | 0.0153 | - | 7.65 KB |
| RunWithFairyBreadValidation | 8.640 μs | 0.1048 μs | 0.0980 μs | 1.02 | 0.02 | 1.2665 | 0.0153 | - | 7.8 KB |

## ErrorMappersBenchmarks

| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|--------------------------------------------- |---------:|---------:|---------:|------:|--------:|-------:|-------:|------:|----------:|
| RunWithDefaultErrorMapper | 27.26 μs | 0.473 μs | 0.525 μs | 1.00 | 0.00 | 1.7395 | 0.0305 | - | 10.72 KB |
| RunWithDefaultErrorMapperWithDetails | 29.18 μs | 0.263 μs | 0.246 μs | 1.07 | 0.02 | 1.8311 | 0.0305 | - | 11.17 KB |
| RunWithDefaultErrorMapperWithExtendedDetails | 29.56 μs | 0.278 μs | 0.260 μs | 1.08 | 0.03 | 1.9226 | 0.0305 | - | 11.69 KB |

## MultipleArgumentsBenchmarks

| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------------------- |----------:|----------:|----------:|-------:|-------:|------:|----------:|
| RunWithoutSingleArgument | 7.690 μs | 0.0269 μs | 0.0225 μs | 1.2512 | 0.0153 | - | 7.66 KB |
| RunWithSingleArgument | 26.370 μs | 0.2195 μs | 0.1946 μs | 1.7395 | 0.0305 | - | 10.73 KB |
| RunWithoutFiveArguments | 7.859 μs | 0.0487 μs | 0.0456 μs | 1.3275 | 0.0153 | - | 8.15 KB |
| RunWithFiveArguments | 27.706 μs | 0.1452 μs | 0.1133 μs | 1.8311 | 0.0305 | - | 11.22 KB |
| Type | Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|-------------------------------- |--------------------------------------------- |------------:|----------:|----------:|------:|--------:|-------:|-------:|------:|----------:|
| SingletonValidationBenchmarks | RunWithoutValidation | 8,479.6 ns | 61.50 ns | 54.52 ns | 0.30 | 0.01 | 1.2512 | 0.0153 | - | 7.66 KB |
| SingletonValidationBenchmarks | RunWithValidation | 25,260.1 ns | 341.10 ns | 319.07 ns | 0.88 | 0.02 | 1.7090 | 0.0305 | - | 10.53 KB |
| SingletonValidationBenchmarks | RunWithFairyBreadValidation | 27,657.7 ns | 450.65 ns | 421.54 ns | 0.97 | 0.03 | 1.8616 | 0.0305 | - | 11.5 KB |
| ScopedValidationBenchmarks | RunWithoutValidation | 8,317.4 ns | 52.49 ns | 46.53 ns | 0.29 | 0.00 | 1.2512 | 0.0153 | - | 7.66 KB |
| ScopedValidationBenchmarks | RunWithValidation | 38,690.3 ns | 666.77 ns | 623.69 ns | 1.35 | 0.03 | 2.0752 | - | - | 12.76 KB |
| ScopedValidationBenchmarks | RunWithFairyBreadValidation | 43,161.2 ns | 285.90 ns | 267.44 ns | 1.51 | 0.03 | 2.6245 | 0.0610 | - | 16.23 KB |
| EmptyInputsValidationBenchmarks | RunWithoutValidation | 7,970.4 ns | 48.10 ns | 44.99 ns | 0.28 | 0.01 | 1.2512 | 0.0153 | - | 7.64 KB |
| EmptyInputsValidationBenchmarks | RunWithValidation | 8,395.0 ns | 63.72 ns | 53.21 ns | 0.29 | 0.01 | 1.2512 | 0.0153 | - | 7.64 KB |
| EmptyInputsValidationBenchmarks | RunWithFairyBreadValidation | 9,480.9 ns | 89.16 ns | 79.03 ns | 0.33 | 0.01 | 1.2970 | 0.0153 | - | 7.93 KB |
| NullInputsValidationBenchmarks | RunWithoutValidation | 8,288.5 ns | 99.49 ns | 93.06 ns | 0.29 | 0.00 | 1.2512 | 0.0153 | - | 7.65 KB |
| NullInputsValidationBenchmarks | RunWithValidation | 8,841.5 ns | 57.33 ns | 53.63 ns | 0.31 | 0.01 | 1.2512 | 0.0153 | - | 7.65 KB |
| NullInputsValidationBenchmarks | RunWithFairyBreadValidation | 9,611.5 ns | 56.74 ns | 44.30 ns | 0.34 | 0.00 | 1.2970 | 0.0153 | - | 7.94 KB |
| InputValidatorBenchmarks | Validation | 894.1 ns | 5.06 ns | 4.74 ns | 0.03 | 0.00 | 0.3157 | 0.0010 | - | 1.94 KB |
| InputValidatorBenchmarks | InputValidatorValidation | 1,025.9 ns | 11.01 ns | 10.30 ns | 0.04 | 0.00 | 0.3529 | - | - | 2.17 KB |
| ErrorMappersBenchmarks | RunWithDefaultErrorMapper | 28,636.1 ns | 542.63 ns | 507.58 ns | 1.00 | 0.00 | 1.7395 | 0.0305 | - | 10.72 KB |
| ErrorMappersBenchmarks | RunWithDefaultErrorMapperWithDetails | 29,424.7 ns | 393.17 ns | 367.78 ns | 1.03 | 0.02 | 1.8311 | 0.0305 | - | 11.16 KB |
| ErrorMappersBenchmarks | RunWithDefaultErrorMapperWithExtendedDetails | 31,723.2 ns | 386.73 ns | 361.75 ns | 1.11 | 0.02 | 1.8921 | - | - | 11.69 KB |
| MultipleArgumentsBenchmarks | RunWithoutSingleArgument | 8,399.1 ns | 59.77 ns | 52.99 ns | 0.29 | 0.01 | 1.2512 | 0.0153 | - | 7.66 KB |
| MultipleArgumentsBenchmarks | RunWithSingleArgument | 28,573.9 ns | 253.39 ns | 211.59 ns | 1.00 | 0.02 | 1.7395 | 0.0305 | - | 10.73 KB |
| MultipleArgumentsBenchmarks | RunWithoutFiveArguments | 8,305.3 ns | 76.11 ns | 71.20 ns | 0.29 | 0.01 | 1.3275 | 0.0153 | - | 8.15 KB |
| MultipleArgumentsBenchmarks | RunWithFiveArguments | 29,423.5 ns | 251.90 ns | 210.35 ns | 1.03 | 0.02 | 1.8311 | 0.0305 | - | 11.22 KB |
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using FairyBread;
using FluentChoco;
using FluentValidation;
using HotChocolate.Execution;
Expand All @@ -9,10 +8,10 @@
namespace AppAny.HotChocolate.FluentValidation.Benchmarks
{
[MemoryDiagnoser]
public class ExplicitValidationBenchmarks
public class ScopedValidationBenchmarks
{
private IRequestExecutor withoutValidation = default!;
private IRequestExecutor withExplicitValidation = default!;
private IRequestExecutor withValidation = default!;
private IRequestExecutor fluentChocoValidation = default!;
private IRequestExecutor fairyBreadValidation = default!;

Expand All @@ -23,23 +22,21 @@ public async Task GlobalSetup()
builder => builder.AddMutationType(new TestMutationType(field =>
field.Argument("input", arg => arg.Type<TestInputType>()))));

withExplicitValidation = await BenchmarkSetup.CreateRequestExecutor(
withValidation = await BenchmarkSetup.CreateRequestExecutor(
builder => builder.AddFluentValidation()
.AddMutationType(new TestMutationType(field => field
.Argument("input",
arg => arg.Type<TestInputType>().UseFluentValidation(opt => opt.UseValidator<IValidator<TestInput>>()))))
.Services.AddSingleton<IValidator<TestInput>, TestInputValidator>());
.AddMutationType(new TestMutationType(field => field.Argument("input", arg => arg
.Type<TestInputType>().UseFluentValidation(opt => opt.UseValidator<TestInputValidator>()))))
.Services.AddScoped<TestInputValidator>());

fluentChocoValidation = await BenchmarkSetup.CreateRequestExecutor(
builder => builder.UseFluentValidation()
.AddMutationType(new TestMutationType(field => field.Argument("input", arg => arg.Type<TestInputType>())))
.Services.AddSingleton<IValidator<TestInput>, TestInputValidator>());
.Services.AddScoped<IValidator<TestInput>, TestInputValidator>());

fairyBreadValidation = await BenchmarkSetup.CreateRequestExecutor(
builder => builder.AddFairyBread(opt => opt.AssembliesToScanForValidators = new[] { typeof(Program).Assembly })
.AddMutationType(new TestMutationType(field =>
field.Argument("input", arg => arg.Type<TestInputType>().UseValidation())))
.Services.AddSingleton<TestInputValidator>());
.AddMutationType(new TestMutationType(field => field.Argument("input", arg => arg.Type<TestInputType>())))
.Services.AddScoped<TestInputValidator>());
}

[Benchmark]
Expand All @@ -51,7 +48,7 @@ public Task RunWithoutValidation()
[Benchmark(Baseline = true)]
public Task RunWithValidation()
{
return withExplicitValidation.ExecuteAsync(BenchmarkSetup.Mutations.WithEmptyName);
return withValidation.ExecuteAsync(BenchmarkSetup.Mutations.WithEmptyName);
}

// [Benchmark(Description = "Broken since 11.0.8")]
Expand Down
Loading

0 comments on commit 3d8e45f

Please sign in to comment.