Skip to content

Commit

Permalink
file scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Dec 13, 2022
1 parent 87cf9c0 commit d05f172
Show file tree
Hide file tree
Showing 296 changed files with 49,432 additions and 49,756 deletions.
83 changes: 41 additions & 42 deletions src/Polly.Benchmarks/Bulkhead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,47 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;

namespace Polly.Benchmarks
namespace Polly.Benchmarks;

[Config(typeof(PollyConfig))]
public class Bulkhead
{
[Config(typeof(PollyConfig))]
public class Bulkhead
private static readonly Policy SyncPolicy = Policy.Bulkhead(2);
private static readonly AsyncPolicy AsyncPolicy = Policy.BulkheadAsync(2);

[Benchmark]
public void Bulkhead_Synchronous()
{
SyncPolicy.Execute(() => Workloads.Action());
}

[Benchmark]
public async Task Bulkhead_Asynchronous()
{
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
}

[Benchmark]
public async Task Bulkhead_Asynchronous_With_CancellationToken()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
public int Bulkhead_Synchronous_With_Result()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result_With_CancellationToken()
{
private static readonly Policy SyncPolicy = Policy.Bulkhead(2);
private static readonly AsyncPolicy AsyncPolicy = Policy.BulkheadAsync(2);

[Benchmark]
public void Bulkhead_Synchronous()
{
SyncPolicy.Execute(() => Workloads.Action());
}

[Benchmark]
public async Task Bulkhead_Asynchronous()
{
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
}

[Benchmark]
public async Task Bulkhead_Asynchronous_With_CancellationToken()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
public int Bulkhead_Synchronous_With_Result()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result_With_CancellationToken()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
}
}
147 changes: 73 additions & 74 deletions src/Polly.Benchmarks/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,107 +5,106 @@
using Microsoft.Extensions.Caching.Memory;
using Polly.Caching;

namespace Polly.Benchmarks
namespace Polly.Benchmarks;

[Config(typeof(PollyConfig))]
public class Cache
{
[Config(typeof(PollyConfig))]
public class Cache
private static readonly MemoryCache MemoryCache = new MemoryCache(new MemoryCacheOptions());
private static readonly MemoryCacheProvider CacheProvider = new MemoryCacheProvider(MemoryCache);

private static readonly Policy SyncPolicyMiss = Policy.Cache(CacheProvider, TimeSpan.Zero);
private static readonly AsyncPolicy AsyncPolicyMiss = Policy.CacheAsync(CacheProvider, TimeSpan.Zero);

private static readonly Policy SyncPolicyHit = Policy.Cache(CacheProvider, TimeSpan.MaxValue);
private static readonly AsyncPolicy AsyncPolicyHit = Policy.CacheAsync(CacheProvider, TimeSpan.MaxValue);

private static readonly Context HitContext = new Context(nameof(HitContext));
private static readonly Context MissContext = new Context(nameof(MissContext));

[GlobalSetup]
public async Task GlobalSetup()
{
private static readonly MemoryCache MemoryCache = new MemoryCache(new MemoryCacheOptions());
private static readonly MemoryCacheProvider CacheProvider = new MemoryCacheProvider(MemoryCache);
SyncPolicyHit.Execute((context) => GetObject(), HitContext);
await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}

private static readonly Policy SyncPolicyMiss = Policy.Cache(CacheProvider, TimeSpan.Zero);
private static readonly AsyncPolicy AsyncPolicyMiss = Policy.CacheAsync(CacheProvider, TimeSpan.Zero);
[Benchmark]
public object Cache_Synchronous_Hit()
{
return SyncPolicyHit.Execute((context) => GetObject(), HitContext);
}

private static readonly Policy SyncPolicyHit = Policy.Cache(CacheProvider, TimeSpan.MaxValue);
private static readonly AsyncPolicy AsyncPolicyHit = Policy.CacheAsync(CacheProvider, TimeSpan.MaxValue);
[Benchmark]
public async Task<object> Cache_Asynchronous_Hit()
{
return await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}

private static readonly Context HitContext = new Context(nameof(HitContext));
private static readonly Context MissContext = new Context(nameof(MissContext));
[Benchmark]
public object Cache_Synchronous_Miss()
{
return SyncPolicyMiss.Execute((context) => GetObject(), MissContext);
}

[GlobalSetup]
public async Task GlobalSetup()
{
SyncPolicyHit.Execute((context) => GetObject(), HitContext);
await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}
[Benchmark]
public async Task<object> Cache_Asynchronous_Miss()
{
return await AsyncPolicyMiss.ExecuteAsync((context, token) => GetObjectAsync(token), MissContext, CancellationToken.None);
}

[Benchmark]
public object Cache_Synchronous_Hit()
{
return SyncPolicyHit.Execute((context) => GetObject(), HitContext);
}
private static object GetObject() => new object();

[Benchmark]
public async Task<object> Cache_Asynchronous_Hit()
{
return await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}
private static Task<object> GetObjectAsync(CancellationToken cancellationToken) => Task.FromResult(new object());

[Benchmark]
public object Cache_Synchronous_Miss()
private sealed class MemoryCacheProvider : ISyncCacheProvider, IAsyncCacheProvider
{
private readonly IMemoryCache _cache;

public MemoryCacheProvider(IMemoryCache memoryCache)
{
return SyncPolicyMiss.Execute((context) => GetObject(), MissContext);
_cache = memoryCache;
}

[Benchmark]
public async Task<object> Cache_Asynchronous_Miss()
public (bool, object) TryGet(string key)
{
return await AsyncPolicyMiss.ExecuteAsync((context, token) => GetObjectAsync(token), MissContext, CancellationToken.None);
var cacheHit = _cache.TryGetValue(key, out var value);
return (cacheHit, value);
}

private static object GetObject() => new object();

private static Task<object> GetObjectAsync(CancellationToken cancellationToken) => Task.FromResult(new object());

private sealed class MemoryCacheProvider : ISyncCacheProvider, IAsyncCacheProvider
public void Put(string key, object value, Ttl ttl)
{
private readonly IMemoryCache _cache;

public MemoryCacheProvider(IMemoryCache memoryCache)
{
_cache = memoryCache;
}
var remaining = DateTimeOffset.MaxValue - DateTimeOffset.UtcNow;
var options = new MemoryCacheEntryOptions();

public (bool, object) TryGet(string key)
if (ttl.SlidingExpiration)
{
var cacheHit = _cache.TryGetValue(key, out var value);
return (cacheHit, value);
options.SlidingExpiration = ttl.Timespan < remaining ? ttl.Timespan : remaining;
}

public void Put(string key, object value, Ttl ttl)
else
{
var remaining = DateTimeOffset.MaxValue - DateTimeOffset.UtcNow;
var options = new MemoryCacheEntryOptions();

if (ttl.SlidingExpiration)
if (ttl.Timespan == TimeSpan.MaxValue)
{
options.SlidingExpiration = ttl.Timespan < remaining ? ttl.Timespan : remaining;
options.AbsoluteExpiration = DateTimeOffset.MaxValue;
}
else
{
if (ttl.Timespan == TimeSpan.MaxValue)
{
options.AbsoluteExpiration = DateTimeOffset.MaxValue;
}
else
{
options.AbsoluteExpirationRelativeToNow = ttl.Timespan < remaining ? ttl.Timespan : remaining;
}
options.AbsoluteExpirationRelativeToNow = ttl.Timespan < remaining ? ttl.Timespan : remaining;
}

_cache.Set(key, value, options);
}

public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
return Task.FromResult(TryGet(key));
}
_cache.Set(key, value, options);
}

public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
Put(key, value, ttl);
return Task.CompletedTask;
}
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
return Task.FromResult(TryGet(key));
}

public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
Put(key, value, ttl);
return Task.CompletedTask;
}
}
}
}
53 changes: 26 additions & 27 deletions src/Polly.Benchmarks/CircuitBreaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;

namespace Polly.Benchmarks
namespace Polly.Benchmarks;

[Config(typeof(PollyConfig))]
public class CircuitBreaker
{
[Config(typeof(PollyConfig))]
public class CircuitBreaker
{
private static readonly Policy SyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreaker(2, TimeSpan.FromMinutes(1));
private static readonly AsyncPolicy AsyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreakerAsync(2, TimeSpan.FromMinutes(1));
private static readonly Policy SyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreaker(2, TimeSpan.FromMinutes(1));
private static readonly AsyncPolicy AsyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreakerAsync(2, TimeSpan.FromMinutes(1));

[Benchmark]
public void CircuitBreaker_Synchronous_Succeeds()
{
SyncPolicy.Execute(() => Workloads.Action());
}
[Benchmark]
public void CircuitBreaker_Synchronous_Succeeds()
{
SyncPolicy.Execute(() => Workloads.Action());
}

[Benchmark]
public async Task CircuitBreaker_Asynchronous_Succeeds()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}
[Benchmark]
public async Task CircuitBreaker_Asynchronous_Succeeds()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
public int CircuitBreaker_Synchronous_With_Result_Succeeds()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}
[Benchmark]
public int CircuitBreaker_Synchronous_With_Result_Succeeds()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}

[Benchmark]
public async Task<int> CircuitBreaker_Asynchronous_With_Result_Succeeds()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
[Benchmark]
public async Task<int> CircuitBreaker_Asynchronous_With_Result_Succeeds()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
}
}
Loading

0 comments on commit d05f172

Please sign in to comment.