|
| 1 | +using System.Threading.Tasks; |
| 2 | +using BenchmarkDotNet.Attributes; |
| 3 | +using BenchmarkDotNet.Configs; |
| 4 | +using LazyCache.Providers; |
| 5 | +using Microsoft.Extensions.Caching.Memory; |
| 6 | + |
| 7 | +namespace LazyCache.Benchmarks |
| 8 | +{ |
| 9 | + [Config(typeof(BenchmarkConfig))] |
| 10 | + [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 11 | + public class MemoryCacheBenchmarks |
| 12 | + { |
| 13 | + public const string CacheKey = nameof(CacheKey); |
| 14 | + |
| 15 | + public IMemoryCache MemCache; |
| 16 | + public IMemoryCache PopulatedMemCache; |
| 17 | + public IAppCache AppCache; |
| 18 | + public IAppCache PopulatedAppCache; |
| 19 | + public ComplexObject ComplexObject; |
| 20 | + |
| 21 | + [GlobalSetup] |
| 22 | + public void Setup() |
| 23 | + { |
| 24 | + ComplexObject = new ComplexObject(); |
| 25 | + |
| 26 | + MemCache = new MemoryCache(new MemoryCacheOptions()); |
| 27 | + PopulatedMemCache = new MemoryCache(new MemoryCacheOptions()); |
| 28 | + |
| 29 | + AppCache = new CachingService(new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions()))); |
| 30 | + PopulatedAppCache = new CachingService(new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions()))); |
| 31 | + |
| 32 | + PopulatedAppCache.Add(CacheKey, ComplexObject); |
| 33 | + PopulatedMemCache.Set(CacheKey, ComplexObject); |
| 34 | + } |
| 35 | + |
| 36 | + [GlobalCleanup] |
| 37 | + public void Cleanup() => MemCache.Dispose(); |
| 38 | + |
| 39 | + /* |
| 40 | + * |
| 41 | + * Benchmark Cache Initialization |
| 42 | + * |
| 43 | + */ |
| 44 | + |
| 45 | + [Benchmark(Baseline = true), BenchmarkCategory("Init")] |
| 46 | + public MemoryCache DotNetMemoryCache_Init() => new MemoryCache(new MemoryCacheOptions()); |
| 47 | + |
| 48 | + [Benchmark, BenchmarkCategory("Init")] |
| 49 | + public CachingService LazyCache_Init() => new CachingService(new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions()))); |
| 50 | + |
| 51 | + /* |
| 52 | + * |
| 53 | + * Benchmark Add Methods |
| 54 | + * |
| 55 | + */ |
| 56 | + |
| 57 | + [Benchmark(Baseline = true), BenchmarkCategory(nameof(IAppCache.Add))] |
| 58 | + public void DotNetMemoryCache_Set() => MemCache.Set(CacheKey, ComplexObject); |
| 59 | + |
| 60 | + [Benchmark, BenchmarkCategory(nameof(IAppCache.Add))] |
| 61 | + public void LazyCache_Set() => AppCache.Add(CacheKey, ComplexObject); |
| 62 | + |
| 63 | + /* |
| 64 | + * |
| 65 | + * Benchmark Get Methods With a Cache Miss |
| 66 | + * |
| 67 | + */ |
| 68 | + |
| 69 | + [Benchmark(Baseline = true), BenchmarkCategory(nameof(IAppCache.Get) + "_Miss")] |
| 70 | + public ComplexObject DotNetMemoryCache_Get_Miss() => MemCache.Get<ComplexObject>(CacheKey); |
| 71 | + |
| 72 | + [Benchmark, BenchmarkCategory(nameof(IAppCache.Get) + "_Miss")] |
| 73 | + public ComplexObject LazyCache_Get_Miss() => AppCache.Get<ComplexObject>(CacheKey); |
| 74 | + |
| 75 | + /* |
| 76 | + * |
| 77 | + * Benchmark Get Methods With a Cache Hit |
| 78 | + * |
| 79 | + */ |
| 80 | + |
| 81 | + [Benchmark(Baseline = true), BenchmarkCategory(nameof(IAppCache.Get) + "_Hit")] |
| 82 | + public ComplexObject DotNetMemoryCache_Get_Hit() => PopulatedMemCache.Get<ComplexObject>(CacheKey); |
| 83 | + |
| 84 | + [Benchmark, BenchmarkCategory(nameof(IAppCache.Get) + "_Hit")] |
| 85 | + public ComplexObject LazyCache_Get_Hit() => PopulatedAppCache.Get<ComplexObject>(CacheKey); |
| 86 | + |
| 87 | + /* |
| 88 | + * |
| 89 | + * Benchmark GetOrAdd Methods With Cache Miss |
| 90 | + * |
| 91 | + */ |
| 92 | + |
| 93 | + [Benchmark(Baseline = true), BenchmarkCategory(nameof(IAppCache.GetOrAdd) + "_Miss")] |
| 94 | + public ComplexObject DotNetMemoryCache_GetOrAdd_Miss() => MemCache.GetOrCreate(CacheKey, entry => ComplexObject); |
| 95 | + |
| 96 | + [Benchmark, BenchmarkCategory(nameof(IAppCache.GetOrAdd) + "_Miss")] |
| 97 | + public ComplexObject LazyCache_GetOrAdd_Miss() => AppCache.GetOrAdd(CacheKey, entry => ComplexObject); |
| 98 | + |
| 99 | + /* |
| 100 | + * |
| 101 | + * Benchmark GetOrAdd Methods With Cache Hit |
| 102 | + * |
| 103 | + */ |
| 104 | + |
| 105 | + [Benchmark(Baseline = true), BenchmarkCategory(nameof(IAppCache.GetOrAdd) + "_Hit")] |
| 106 | + public ComplexObject DotNetMemoryCache_GetOrAdd_Hit() => PopulatedMemCache.GetOrCreate(CacheKey, entry => ComplexObject); |
| 107 | + |
| 108 | + [Benchmark, BenchmarkCategory(nameof(IAppCache.GetOrAdd) + "_Hit")] |
| 109 | + public ComplexObject LazyCache_GetOrAdd_Hit() => PopulatedAppCache.GetOrAdd(CacheKey, entry => ComplexObject); |
| 110 | + |
| 111 | + /* |
| 112 | + * |
| 113 | + * Benchmark GetOrAddAsync Methods With Cache Miss |
| 114 | + * |
| 115 | + */ |
| 116 | + |
| 117 | + |
| 118 | + [Benchmark(Baseline = true), BenchmarkCategory(nameof(IAppCache.GetOrAddAsync) + "_Miss")] |
| 119 | + public Task<ComplexObject> DotNetMemoryCache_GetOrAddAsync_Miss() => MemCache.GetOrCreateAsync(CacheKey, entry => Task.FromResult(ComplexObject)); |
| 120 | + |
| 121 | + [Benchmark, BenchmarkCategory(nameof(IAppCache.GetOrAddAsync) + "_Miss")] |
| 122 | + public Task<ComplexObject> LazyCache_GetOrAddAsync_Miss() => AppCache.GetOrAddAsync(CacheKey, entry => Task.FromResult(ComplexObject)); |
| 123 | + |
| 124 | + /* |
| 125 | + * |
| 126 | + * Benchmark GetOrAddAsync Methods With Cache Hit |
| 127 | + * |
| 128 | + */ |
| 129 | + |
| 130 | + [Benchmark(Baseline = true), BenchmarkCategory(nameof(IAppCache.GetOrAddAsync) + "_Hit")] |
| 131 | + public Task<ComplexObject> DotNetMemoryCache_GetOrAddAsync_Hit() => PopulatedMemCache.GetOrCreateAsync(CacheKey, entry => Task.FromResult(ComplexObject)); |
| 132 | + |
| 133 | + [Benchmark, BenchmarkCategory(nameof(IAppCache.GetOrAddAsync) + "_Hit")] |
| 134 | + public Task<ComplexObject> LazyCache_GetOrAddAsync_Hit() => PopulatedAppCache.GetOrAddAsync(CacheKey, entry => Task.FromResult(ComplexObject)); |
| 135 | + } |
| 136 | +} |
0 commit comments