-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Polly.Core.Benchmark (#1061)
- Loading branch information
Showing
13 changed files
with
5,585 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project> | ||
|
||
<Import Project="..\Version.props" /> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<Project> | ||
<Import Project="$(MsBuildThisFileDirectory)../eng/Common.targets" /> | ||
<Import Project="$(MsBuildThisFileDirectory)../eng/Test.targets" Condition="$(ProjectType) == 'Test'" /> | ||
<Import Project="$(MsBuildThisFileDirectory)../eng/Library.targets" Condition="$(ProjectType) == 'Library'" /> | ||
<Import Project="$(MsBuildThisFileDirectory)../eng/$(ProjectType).targets" Condition="$(ProjectType) != ''" /> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using Polly; | ||
|
||
namespace Polly.Core.Benchmarks; | ||
|
||
public class PipelineBenchmark | ||
{ | ||
private object? _strategyV7; | ||
private object? _strategyV8; | ||
private object? _strategyV8Delegating; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_strategyV7 = Helper.CreatePipeline(PollyVersion.V7, Components, delegating: false); | ||
_strategyV8 = Helper.CreatePipeline(PollyVersion.V8, Components, delegating: false); | ||
_strategyV8Delegating = Helper.CreatePipeline(PollyVersion.V8, Components, delegating: true); | ||
} | ||
|
||
[Params(1, 2, 5, 10)] | ||
public int Components { get; set; } | ||
|
||
[Benchmark(Baseline = true)] | ||
public ValueTask ExecutePipeline_V7() => _strategyV7!.ExecuteAsync(PollyVersion.V7); | ||
|
||
[Benchmark] | ||
public ValueTask ExecutePipeline_V8() => _strategyV8!.ExecuteAsync(PollyVersion.V8); | ||
|
||
[Benchmark] | ||
public ValueTask ExecutePipeline_V8Delegating() => _strategyV8Delegating!.ExecuteAsync(PollyVersion.V8); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Polly.Core.Benchmarks/Internals/EmptyDelegatingResilienceStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Polly.Core.Benchmarks; | ||
|
||
internal class EmptyDelegatingResilienceStrategy : DelegatingResilienceStrategy | ||
{ | ||
protected override ValueTask<TResult> ExecuteCoreAsync<TResult, TState>(Func<ResilienceContext, TState, ValueTask<TResult>> callback, ResilienceContext context, TState state) | ||
{ | ||
return base.ExecuteCoreAsync(callback, context, state); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Polly.Core.Benchmarks/Internals/EmptyResilienceStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Polly; | ||
|
||
namespace Polly.Core.Benchmarks; | ||
|
||
internal class EmptyResilienceStrategy : ResilienceStrategy | ||
{ | ||
protected override ValueTask<TResult> ExecuteCoreAsync<TResult, TState>(Func<ResilienceContext, TState, ValueTask<TResult>> callback, ResilienceContext context, TState state) | ||
{ | ||
return callback(context, state); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Polly; | ||
|
||
namespace Polly.Core.Benchmarks; | ||
|
||
internal static partial class Helper | ||
{ | ||
public static object CreatePipeline(PollyVersion technology, int count, bool delegating) => technology switch | ||
{ | ||
PollyVersion.V7 => count == 1 ? Policy.NoOpAsync<int>() : Policy.WrapAsync(Enumerable.Repeat(0, count).Select(_ => Policy.NoOpAsync<int>()).ToArray()), | ||
|
||
PollyVersion.V8 => CreateStrategy(builder => | ||
{ | ||
for (var i = 0; i < count; i++) | ||
{ | ||
builder.AddStrategy(delegating ? new EmptyDelegatingResilienceStrategy() : new EmptyResilienceStrategy()); | ||
} | ||
}), | ||
_ => throw new NotSupportedException() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Polly; | ||
using Polly.Builder; | ||
|
||
#pragma warning disable S4225 // Extension methods should not extend "object" | ||
|
||
namespace Polly.Core.Benchmarks; | ||
|
||
internal static partial class Helper | ||
{ | ||
public static async ValueTask ExecuteAsync(this object obj, PollyVersion version) | ||
{ | ||
switch (version) | ||
{ | ||
case PollyVersion.V7: | ||
await ((IAsyncPolicy<int>)obj).ExecuteAsync(static _ => Task.FromResult(999), CancellationToken.None).ConfigureAwait(false); | ||
return; | ||
case PollyVersion.V8: | ||
await ((ResilienceStrategy)obj).ExecuteValueTaskAsync(static _ => new ValueTask<int>(999), CancellationToken.None).ConfigureAwait(false); | ||
return; | ||
} | ||
|
||
throw new NotSupportedException(); | ||
} | ||
|
||
private static ResilienceStrategy CreateStrategy(Action<ResilienceStrategyBuilder> configure) | ||
{ | ||
var builder = new ResilienceStrategyBuilder(); | ||
configure(builder); | ||
return builder.Build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net7.0</TargetFrameworks> | ||
<RootNamespace>Polly</RootNamespace> | ||
<ImplicitUsings>true</ImplicitUsings> | ||
<ProjectType>Benchmark</ProjectType> | ||
<Nullable>enable</Nullable> | ||
<OutputType>Exe</OutputType> | ||
<UseDefaultAnalyzers>true</UseDefaultAnalyzers> | ||
<NoWarn>$(NoWarn);SA1600</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Polly.Core\Polly.Core.csproj" /> | ||
<ProjectReference Include="..\Polly\Polly.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Polly.Core.Benchmarks; | ||
|
||
public enum PollyVersion | ||
{ | ||
V7, | ||
V8 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Diagnosers; | ||
using BenchmarkDotNet.Jobs; | ||
using BenchmarkDotNet.Running; | ||
using BenchmarkDotNet.Toolchains.InProcess.Emit; | ||
using Polly.Core.Benchmarks; | ||
|
||
var config = ManualConfig | ||
.Create(DefaultConfig.Instance) | ||
.AddJob(Job.MediumRun.WithToolchain(InProcessEmitToolchain.Instance)) | ||
.AddDiagnoser(MemoryDiagnoser.Default); | ||
|
||
BenchmarkRunner.Run(typeof(PollyVersion).Assembly, config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Benchmark results | ||
|
||
```text | ||
BenchmarkDotNet=v0.13.3, OS=Windows 11 (10.0.22621.1413) | ||
Intel Core i9-10885H CPU 2.40GHz, 1 CPU, 16 logical and 8 physical cores | ||
.NET SDK=7.0.202 | ||
[Host] : .NET 7.0.4 (7.0.423.11508), X64 RyuJIT AVX2 | ||
Job=MediumRun Toolchain=InProcessEmitToolchain IterationCount=15 | ||
LaunchCount=2 WarmupCount=10 | ||
``` | ||
|
||
## PIPELINES | ||
|
||
| Method | Components | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | | ||
|----------------------------- |----------- |------------:|----------:|----------:|------------:|------:|--------:|-------:|----------:|------------:| | ||
| ExecutePipeline_V7 | 1 | 74.84 ns | 1.279 ns | 1.835 ns | 75.81 ns | 1.00 | 0.00 | 0.0362 | 304 B | 1.00 | | ||
| ExecutePipeline_V8 | 1 | 72.61 ns | 0.584 ns | 0.819 ns | 72.28 ns | 0.97 | 0.02 | 0.0048 | 40 B | 0.13 | | ||
| ExecutePipeline_V8Delegating | 1 | 90.04 ns | 0.753 ns | 1.104 ns | 89.76 ns | 1.20 | 0.02 | 0.0048 | 40 B | 0.13 | | ||
| | | | | | | | | | | | | ||
| ExecutePipeline_V7 | 2 | 156.67 ns | 2.810 ns | 4.119 ns | 154.12 ns | 1.00 | 0.00 | 0.0658 | 552 B | 1.00 | | ||
| ExecutePipeline_V8 | 2 | 185.87 ns | 1.932 ns | 2.580 ns | 185.02 ns | 1.19 | 0.04 | 0.0048 | 40 B | 0.07 | | ||
| ExecutePipeline_V8Delegating | 2 | 127.92 ns | 1.264 ns | 1.772 ns | 129.00 ns | 0.82 | 0.02 | 0.0048 | 40 B | 0.07 | | ||
| | | | | | | | | | | | | ||
| ExecutePipeline_V7 | 5 | 543.85 ns | 19.540 ns | 29.247 ns | 530.00 ns | 1.00 | 0.00 | 0.1545 | 1296 B | 1.00 | | ||
| ExecutePipeline_V8 | 5 | 335.67 ns | 3.903 ns | 5.598 ns | 336.22 ns | 0.62 | 0.04 | 0.0048 | 40 B | 0.03 | | ||
| ExecutePipeline_V8Delegating | 5 | 189.99 ns | 4.045 ns | 5.929 ns | 189.05 ns | 0.35 | 0.02 | 0.0048 | 40 B | 0.03 | | ||
| | | | | | | | | | | | | ||
| ExecutePipeline_V7 | 10 | 1,112.84 ns | 8.585 ns | 12.036 ns | 1,111.96 ns | 1.00 | 0.00 | 0.3014 | 2536 B | 1.00 | | ||
| ExecutePipeline_V8 | 10 | 543.85 ns | 6.205 ns | 9.095 ns | 545.30 ns | 0.49 | 0.01 | 0.0048 | 40 B | 0.02 | | ||
| ExecutePipeline_V8Delegating | 10 | 258.11 ns | 0.881 ns | 1.318 ns | 258.30 ns | 0.23 | 0.00 | 0.0048 | 40 B | 0.02 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters