From ac01478ec86e13aa8832773eb676f6a304a7e92b Mon Sep 17 00:00:00 2001 From: Martin Costello Date: Mon, 17 Jan 2022 17:19:19 +0000 Subject: [PATCH] Add RateLimit benchmarks (#910) * Add RateLimit benchmarks Add benchmarks for the rate-limit policy. * Change NuGet baseline The latest release is 7.2.3, not 7.3.0. --- src/Polly.Benchmarks/PollyConfig.cs | 2 +- src/Polly.Benchmarks/RateLimit.cs | 37 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/Polly.Benchmarks/RateLimit.cs diff --git a/src/Polly.Benchmarks/PollyConfig.cs b/src/Polly.Benchmarks/PollyConfig.cs index d490ae4d09c..cad9e4148df 100644 --- a/src/Polly.Benchmarks/PollyConfig.cs +++ b/src/Polly.Benchmarks/PollyConfig.cs @@ -28,7 +28,7 @@ private static Job PollyJob(Job job, bool useNuGet) if (useNuGet) { - result = result.WithNuGet("Polly", "7.2.1"); + result = result.WithNuGet("Polly", "7.2.3"); } return result; diff --git a/src/Polly.Benchmarks/RateLimit.cs b/src/Polly.Benchmarks/RateLimit.cs new file mode 100644 index 00000000000..e883a5e9ba5 --- /dev/null +++ b/src/Polly.Benchmarks/RateLimit.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; + +namespace Polly.Benchmarks +{ + [Config(typeof(PollyConfig))] + public class RateLimit + { + private static readonly Policy SyncPolicy = Policy.RateLimit(20, TimeSpan.FromSeconds(1), int.MaxValue); + private static readonly AsyncPolicy AsyncPolicy = Policy.RateLimitAsync(20, TimeSpan.FromSeconds(1), int.MaxValue); + + [Benchmark] + public void RateLimit_Synchronous_Succeeds() + { + SyncPolicy.Execute(() => Workloads.Action()); + } + + [Benchmark] + public async Task RateLimit_Asynchronous_Succeeds() + { + await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync()); + } + + [Benchmark] + public int RateLimit_Synchronous_With_Result_Succeeds() + { + return SyncPolicy.Execute(() => Workloads.Func()); + } + + [Benchmark] + public async Task RateLimit_Asynchronous_With_Result_Succeeds() + { + return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync()); + } + } +}