|
| 1 | +using Benchly; |
| 2 | +using BenchmarkDotNet.Attributes; |
| 3 | +using BenchmarkDotNet.Diagnosers; |
| 4 | +using BenchmarkDotNet.Jobs; |
| 5 | +using BitFaster.Caching.Lfu; |
| 6 | +using BitFaster.Caching.Lru; |
| 7 | +using BitFaster.Caching.Scheduler; |
| 8 | +using Microsoft.Extensions.Caching.Memory; |
| 9 | +using System; |
| 10 | +using System.Collections.Concurrent; |
| 11 | +using System.Collections.Generic; |
| 12 | + |
| 13 | +namespace BitFaster.Caching.Benchmarks |
| 14 | +{ |
| 15 | + |
| 16 | +#if Windows |
| 17 | + [DisassemblyDiagnoser(printSource: true, maxDepth: 5)] |
| 18 | + [SimpleJob(RuntimeMoniker.Net48)] |
| 19 | +#endif |
| 20 | + [SimpleJob(RuntimeMoniker.Net60)] |
| 21 | + [MemoryDiagnoser(displayGenColumns: false)] |
| 22 | + // [HardwareCounters(HardwareCounter.LlcMisses, HardwareCounter.CacheMisses)] // Requires Admin https://adamsitnik.com/Hardware-Counters-Diagnoser/ |
| 23 | + // [ThreadingDiagnoser] // Requires .NET Core |
| 24 | + [HideColumns("Job", "Median", "RatioSD", "Alloc Ratio")] |
| 25 | + [ColumnChart(Title= "Lookup Latency ({JOB})", Output = OutputMode.PerJob, Colors = "darkslategray,royalblue,royalblue,#ffbf00,indianred,indianred")] |
| 26 | + public class LruJustGetOrAddGuid |
| 27 | + { |
| 28 | + private static readonly ConcurrentDictionary<int, Guid> dictionary = new ConcurrentDictionary<int, Guid>(8, 9, EqualityComparer<int>.Default); |
| 29 | + |
| 30 | + private static readonly ConcurrentLru<int, Guid> concurrentLru = new ConcurrentLru<int, Guid>(8, 9, EqualityComparer<int>.Default); |
| 31 | + private static readonly FastConcurrentLru<int, Guid> fastConcurrentLru = new FastConcurrentLru<int, Guid>(8, 9, EqualityComparer<int>.Default); |
| 32 | + |
| 33 | + |
| 34 | + private static readonly BackgroundThreadScheduler background = new BackgroundThreadScheduler(); |
| 35 | + private static readonly ConcurrentLfu<int, Guid> concurrentLfu = new ConcurrentLfu<int, Guid>(1, 9, background, EqualityComparer<int>.Default); |
| 36 | + |
| 37 | + private static readonly int key = 1; |
| 38 | + private static System.Runtime.Caching.MemoryCache memoryCache = System.Runtime.Caching.MemoryCache.Default; |
| 39 | + |
| 40 | + Microsoft.Extensions.Caching.Memory.MemoryCache exMemoryCache |
| 41 | + = new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptionsAccessor()); |
| 42 | + |
| 43 | + private static readonly byte[] b = new byte[8]; |
| 44 | + |
| 45 | + [GlobalSetup] |
| 46 | + public void GlobalSetup() |
| 47 | + { |
| 48 | + memoryCache.Set(key.ToString(), new Guid(key, 0, 0, b), new System.Runtime.Caching.CacheItemPolicy()); |
| 49 | + exMemoryCache.Set(key, new Guid(key, 0, 0, b)); |
| 50 | + } |
| 51 | + |
| 52 | + [GlobalCleanup] |
| 53 | + public void GlobalCleanup() |
| 54 | + { |
| 55 | + background.Dispose(); |
| 56 | + } |
| 57 | + |
| 58 | + [Benchmark(Baseline = true)] |
| 59 | + public Guid ConcurrentDictionary() |
| 60 | + { |
| 61 | + Func<int, Guid> func = x => new Guid(x, 0, 0, b); |
| 62 | + return dictionary.GetOrAdd(1, func); |
| 63 | + } |
| 64 | + |
| 65 | + [Benchmark()] |
| 66 | + public Guid FastConcurrentLru() |
| 67 | + { |
| 68 | + Func<int, Guid> func = x => new Guid(x, 0, 0, b); |
| 69 | + return fastConcurrentLru.GetOrAdd(1, func); |
| 70 | + } |
| 71 | + |
| 72 | + [Benchmark()] |
| 73 | + public Guid ConcurrentLru() |
| 74 | + { |
| 75 | + Func<int, Guid> func = x => new Guid(x, 0, 0, b); |
| 76 | + return concurrentLru.GetOrAdd(1, func); |
| 77 | + } |
| 78 | + |
| 79 | + [Benchmark()] |
| 80 | + public Guid ConcurrentLfu() |
| 81 | + { |
| 82 | + Func<int, Guid> func = x => new Guid(x, 0, 0, b); |
| 83 | + return concurrentLfu.GetOrAdd(1, func); |
| 84 | + } |
| 85 | + |
| 86 | + [Benchmark()] |
| 87 | + public Guid RuntimeMemoryCacheGet() |
| 88 | + { |
| 89 | + return (Guid)memoryCache.Get("1"); |
| 90 | + } |
| 91 | + |
| 92 | + [Benchmark()] |
| 93 | + public Guid ExtensionsMemoryCacheGet() |
| 94 | + { |
| 95 | + return (Guid)exMemoryCache.Get(1); |
| 96 | + } |
| 97 | + |
| 98 | + public class MemoryCacheOptionsAccessor |
| 99 | + : Microsoft.Extensions.Options.IOptions<MemoryCacheOptions> |
| 100 | + { |
| 101 | + private readonly MemoryCacheOptions options = new MemoryCacheOptions(); |
| 102 | + |
| 103 | + public MemoryCacheOptions Value => this.options; |
| 104 | + |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments