Skip to content

Commit

Permalink
Improve MultipleStrategiesBenchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Aug 7, 2023
1 parent 3831b40 commit 782fcac
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
``` ini
```
BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.22621.1848/22H2/2022Update/SunValley2), VM=Hyper-V
BenchmarkDotNet v0.13.6, Windows 11 (10.0.22621.1992/22H2/2022Update/SunValley2) (Hyper-V)
Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK=7.0.304
[Host] : .NET 7.0.7 (7.0.723.27404), X64 RyuJIT AVX2
.NET SDK 7.0.306
[Host] : .NET 7.0.9 (7.0.923.32018), X64 RyuJIT AVX2
Job=MediumRun Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=2 WarmupCount=10
```
| Method | Mean | Error | StdDev | Ratio | Gen0 | Allocated | Alloc Ratio |
|-------------------------------------- |---------:|----------:|----------:|------:|-------:|----------:|------------:|
| ExecuteStrategyPipeline_V7 | 2.349 μs | 0.0232 μs | 0.0318 μs | 1.00 | 0.1106 | 2824 B | 1.00 |
| ExecuteStrategyPipeline_V8 | 1.968 μs | 0.0104 μs | 0.0149 μs | 0.84 | - | 40 B | 0.01 |
| ExecuteStrategyPipeline_Telemetry_V8 | 3.014 μs | 0.0137 μs | 0.0204 μs | 1.28 | - | 40 B | 0.01 |
| ExecuteStrategyPipeline_NonGeneric_V8 | 2.188 μs | 0.0107 μs | 0.0156 μs | 0.93 | - | 40 B | 0.01 |
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
|----------------------------------------------- |---------:|----------:|----------:|------:|--------:|-------:|----------:|------------:|
| ExecuteStrategyPipeline_Generic_V7 | 2.318 μs | 0.0253 μs | 0.0355 μs | 1.00 | 0.00 | 0.1106 | 2824 B | 1.00 |
| ExecuteStrategyPipeline_Generic_V8 | 2.044 μs | 0.0095 μs | 0.0140 μs | 0.88 | 0.02 | - | 72 B | 0.03 |
| ExecuteStrategyPipeline_GenericTelemetry_V8 | 2.950 μs | 0.0059 μs | 0.0089 μs | 1.27 | 0.02 | - | 72 B | 0.03 |
| ExecuteStrategyPipeline_NonGeneric_V8 | 2.295 μs | 0.0176 μs | 0.0264 μs | 0.99 | 0.01 | - | 72 B | 0.03 |
| ExecuteStrategyPipeline_NonGenericTelemetry_V8 | 3.153 μs | 0.0058 μs | 0.0085 μs | 1.36 | 0.02 | - | 72 B | 0.03 |
23 changes: 19 additions & 4 deletions bench/Polly.Core.Benchmarks/MultipleStrategiesBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class MultipleStrategiesBenchmark
private object? _strategyV8;
private object? _strategyTelemetryV8;
private ResilienceStrategy? _nonGeneric;
private ResilienceStrategy? _nonGenericTelemetry;

[GlobalSetup]
public void Setup()
Expand All @@ -17,20 +18,21 @@ public void Setup()
_strategyV7 = Helper.CreateStrategyPipeline(PollyVersion.V7, false);
_strategyV8 = Helper.CreateStrategyPipeline(PollyVersion.V8, false);
_strategyTelemetryV8 = Helper.CreateStrategyPipeline(PollyVersion.V8, true);
_nonGeneric = Helper.CreateNonGenericStrategyPipeline();
_nonGeneric = Helper.CreateNonGenericStrategyPipeline(telemetry: false);
_nonGenericTelemetry = Helper.CreateNonGenericStrategyPipeline(telemetry: true);
}

[GlobalCleanup]
public void Cleanup() => _meterListener?.Dispose();

[Benchmark(Baseline = true)]
public ValueTask ExecuteStrategyPipeline_V7() => _strategyV7!.ExecuteAsync(PollyVersion.V7);
public ValueTask ExecuteStrategyPipeline_Generic_V7() => _strategyV7!.ExecuteAsync(PollyVersion.V7);

[Benchmark]
public ValueTask ExecuteStrategyPipeline_V8() => _strategyV8!.ExecuteAsync(PollyVersion.V8);
public ValueTask ExecuteStrategyPipeline_Generic_V8() => _strategyV8!.ExecuteAsync(PollyVersion.V8);

[Benchmark]
public ValueTask ExecuteStrategyPipeline_Telemetry_V8() => _strategyTelemetryV8!.ExecuteAsync(PollyVersion.V8);
public ValueTask ExecuteStrategyPipeline_GenericTelemetry_V8() => _strategyTelemetryV8!.ExecuteAsync(PollyVersion.V8);

[Benchmark]
public async ValueTask ExecuteStrategyPipeline_NonGeneric_V8()
Expand All @@ -44,4 +46,17 @@ public async ValueTask ExecuteStrategyPipeline_NonGeneric_V8()

ResilienceContextPool.Shared.Return(context);
}

[Benchmark]
public async ValueTask ExecuteStrategyPipeline_NonGenericTelemetry_V8()
{
var context = ResilienceContextPool.Shared.Get();

await _nonGenericTelemetry!.ExecuteOutcomeAsync(
static (_, _) => new ValueTask<Outcome<string>>(Outcome.FromResult("dummy")),
context,
string.Empty).ConfigureAwait(false);

ResilienceContextPool.Shared.Return(context);
}
}
14 changes: 10 additions & 4 deletions bench/Polly.Core.Benchmarks/Utils/Helper.MultipleStrategies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ internal static partial class Helper
_ => throw new NotSupportedException()
};

public static ResilienceStrategy CreateNonGenericStrategyPipeline()
public static ResilienceStrategy CreateNonGenericStrategyPipeline(bool telemetry)
{
return new CompositeStrategyBuilder()
var builder = new CompositeStrategyBuilder()
.AddConcurrencyLimiter(new ConcurrencyLimiterOptions
{
QueueLimit = 10,
Expand Down Expand Up @@ -91,7 +91,13 @@ public static ResilienceStrategy CreateNonGenericStrategyPipeline()
{ Result: string result } when result == Failure => PredicateResult.True,
_ => PredicateResult.False
}
})
.Build();
});

if (telemetry)
{
builder.ConfigureTelemetry(NullLoggerFactory.Instance);
}

return builder.Build();
}
}

0 comments on commit 782fcac

Please sign in to comment.